利用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文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ospf序列号等问题
- 下一篇: 开发常见错误解决(1)注册.NET En