视频切片
public void SegmentVideo(string filePath)
{
string path = Environment.CurrentDirectory + "\\ffmpeg.exe";
var fileName = Path.GetDirectoryName(filePath)+"\\"+Path.GetFileNameWithoutExtension(filePath);
try
{
Process p = new Process();//建立外部调用线程
p.StartInfo.FileName = path;//要调用外部程序的绝对路径
p.StartInfo.Arguments = @"-i " + filePath + " -y -vcodec copy -acodec copy -map 0 -f segment -segment_list " + fileName + ".m3u8 -segment_time 10 " + fileName + "-%03d.mp4";//后面的例子切片成.ts格式
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的)
p.StartInfo.CreateNoWindow = false;//不创建进程窗口
p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
catch (Exception)
{
throw;
}
}
private void Output(object sendProcess, DataReceivedEventArgs output)
{
if (!String.IsNullOrEmpty(output.Data))
{
//处理方法...
Console.WriteLine(output.Data.ToString());
}
}