c# ffmpeg视频转换
c# ffmpeg视频转换
什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了!
今天工作中遇到这么个问题,这个问题很屌,我通过搜索引擎找了个遍,几乎没有找到理想的答案,于是被逼无奈之下,自己研究参数,于是就有了如下的成就,所以,遇到问题不要害怕,这才是成长最快的时刻。当问题解决了的时候,会有种无比的成就感,把自己的成果分享给他人,供他人参考,这也是种喜悦!
------------没有做不到的,只有想不到的。
转码进度公式=当前时间/总时间*100%;

1 public class FFmpeg 2 { 3 static int timeCount; 4 public static void Execute() 5 { 6 Process p = new Process(); 7 p.StartInfo.FileName = @"E:\张立平资料\Demo\FFmpeg_Demo\ffmpeg\ffmpeg.exe"; 8 p.StartInfo.UseShellExecute = false; 9 string srcFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videoold\test.mov"; 10 string destFileName = @"E:\张立平资料\Demo\FFmpeg_Demo\videonew\test.mp4"; 11 p.StartInfo.Arguments = "-i " + srcFileName + " -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 " + destFileName; //执行参数 12 //-i D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06.mov -y -ab 56 -ar 22050 -b 800 -r 29.97 -s 420x340 D:/vts-collector/test/swap/7a3c9800-2e2f-42fe-ba39-56b25f972e06_mov_stream.mp4 13 14 p.StartInfo.RedirectStandardInput = true; 15 p.StartInfo.RedirectStandardOutput = true; 16 p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中 17 p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 18 p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 19 20 using (p) 21 { 22 p.Start(); 23 p.BeginErrorReadLine();//开始异步读取 24 p.WaitForExit();//阻塞等待进程结束 25 p.Close();//关闭进程 26 } 27 } 28 29 private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) 30 { 31 32 var output = e.Data; 33 if (output != null) 34 { 35 if (output.Contains("Duration")) 36 { 37 Int32 indexOfDuration = output.IndexOf("Duration"); 38 Int32 indexOfBegin = output.IndexOf(":", indexOfDuration); 39 Int32 indexOfEnd = output.IndexOf(",", indexOfBegin); 40 var duration = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); 41 timeCount =ConvertStringtoSecond(duration); 42 } 43 if (output.Contains("time=")) 44 { 45 Int32 indexOfTime = output.IndexOf("time="); 46 Int32 indexOfBegin = output.IndexOf("=", indexOfTime); 47 Int32 indexOfEnd = output.IndexOf("bitrate", indexOfBegin); 48 string timeStr = output.Substring(indexOfBegin + 1, indexOfEnd - indexOfBegin - 1); 49 var time =Convert.ToDouble(timeStr); 50 var process = time / timeCount*100; 51 process = Math.Round(process); 52 Console.Write("{0}% ", process); 53 } 54 //Console.WriteLine(e.Data); 55 } 56 } 57 58 private static int ConvertStringtoSecond(string input) 59 { 60 int totalSecond = 0; 61 try 62 { 63 string[] split = input.Split(new char[] { ':', '.' }); 64 int hour = int.Parse(split[0]) * 3600; 65 int min = int.Parse(split[1]) * 60; 66 int second = int.Parse(split[2]); 67 int millisecond = int.Parse(split[3]) / 1000; 68 totalSecond = hour + min + second + millisecond; 69 } 70 catch (System.Exception ex) 71 { 72 Console.Write(ex.Message); 73 74 } 75 return totalSecond; 76 } 77 78 private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 79 { 80 Console.WriteLine(e.Data); 81 } 82 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构