日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用ffmpeg转换mp4文件

發布時間:2025/1/21 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用ffmpeg转换mp4文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個是我今天整理項目的時候無意發現的,當時主要是用它來轉換flv文件在手機上使用,后來買了psp之后就沒有用了,記錄一下,以后寫這種類似的程序可以參考。

class VideoConverter : IDisposable
{
????public string InputFile { get; private set; }
????public string OutPutFile { get; private set; }
????public int Progress { get; private set; }
????public object Tag { get; set; }

????double totalTime;

????public VideoConverter(string input, string output)
????{
????????InputFile = input;
????????OutPutFile = output;
????}

????~VideoConverter()
????{
????????Dispose();
????}

????const string ffmpeg_exe = "ffmpeg.exe";

????/// <summary>
????///
這個是個阻塞調用,切勿在ui線程上調用
????/// </summary>
????public void Process()
????{
????????var sb = new StringBuilder();
????????ExcuteProcess(ffmpeg_exe, string.Format(" -i \"{0}\"", InputFile), (s, e) => sb.AppendLine(e.Data));
????????var totalT = SimpleMatch(sb.ToString(), @"Duration:\s+(\S+?)[,\s]");
????????var totalTs = TimeSpan.Parse(totalT);
????????totalTime = totalTs.TotalSeconds;
????????ExcuteProcess(ffmpeg_exe, string.Format(" -i \"{0}\" \"{1}\" ", InputFile, OutPutFile), (s, e) => ProcessOutPut(e.Data));????????
????????Progress = 100;
????????Console.WriteLine("-----------{0}---------", Progress);
????}

????string SimpleMatch(string data, string patten)
????{
????????Console.WriteLine(data);
????????var m = System.Text.RegularExpressions.Regex.Match(data, patten);
????????return m.Groups[1].Value;
????}

????void ExcuteProcess(string exe, string arg, DataReceivedEventHandler output)
????{
????????Console.WriteLine(exe + " " + arg);
????????using (var p = new Process())
????????{
????????????killHanlder = p.Kill;
????????????p.StartInfo.FileName = exe;
????????????p.StartInfo.Arguments = arg;

????????????p.StartInfo.UseShellExecute = false;????//
輸出信息重定向
????????????p.StartInfo.CreateNoWindow = true;
????????????p.StartInfo.RedirectStandardError = true;
????????????p.StartInfo.RedirectStandardOutput = true;

????????????p.OutputDataReceived += output;
????????????p.ErrorDataReceived += output;

????????????p.Start();????????????????????//
啟動線程
????????????p.BeginOutputReadLine();
????????????p.BeginErrorReadLine();
????????????p.WaitForExit();????????????//
等待進程結束
????????}

????????killHanlder = null;
????}

????void ProcessOutPut(string output)
????{
????????Console.WriteLine(output);
????????if (string.IsNullOrEmpty(output))
????????????return;

????????if (!output.Contains("time="))
????????????return;

????????var current = SimpleMatch(output, @"time=(\S+)");
????????var currentTime = double.Parse(current);

????????Progress = (int)(currentTime * 100 / totalTime);
????????if (Progress > 100)
????????????Progress = 100;

????????Console.WriteLine("-----------{0}---------", Progress);
????}

????#region IDisposable
成員

????Action killHanlder;
????public void Dispose()
????{
????????if (killHanlder == null)
????????????return;

????????killHanlder();
????????killHanlder = null;
????}

????#endregion
}

ffmpeg下載地址:http://ffdshow.faireal.net/mirror/ffmpeg/

總結

以上是生活随笔為你收集整理的利用ffmpeg转换mp4文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。