C#中用ffmpeg截取视频使用要点

一、代码

string inputFile = "E:\\Test\\1\\5.mp4";
string outputFile = "E:\\Test\\1\\10.mp4";
int startTime = 5; // 开始时间(秒)
int endtime = 10; // 结束时间(秒)

Process p = new Process();
p.StartInfo.FileName = ".\\ffmpeg\\ffmpeg.exe";//ffmpeg.exe路径
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = $"-i {inputFile} -ss {startTime} -to {endtime} -c:v copy {outputFile} ";
p.StartInfo.UseShellExecute = false;//不使用系统外壳程序启动进程
p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中
p.StartInfo.UseShellExecute = false;
p.Start();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.BeginErrorReadLine();//开始异步读取   
p.WaitForExit();// 
p.Close(); // 关闭进程
p.Dispose(); // 释放资源

二、代码使用解释

       2.1 不使用原因 -c:v copy

             -c:v copy不进行重新编码,直接拷贝原视频中的视频片段,保存为截取视频。视频长度存在较大误差

             经测试,截取10秒长度视频,视频显示长度为10秒,但实际为原视频5秒长度(丢帧)

              截取视频长度过短,如小于5秒,会出现视频不显示画面问题

        2.2 使用原因 -c:v libx264 -crf 18

              指定使用H264标准 crf=18模式对原视频中的视频片段进行重新编码

                    

               

你可能感兴趣的:(ffmpeg,音视频)