1. 程式人生 > >C#中使用FFMPEG切割、合併視訊。

C#中使用FFMPEG切割、合併視訊。

參考網址:https://blog.csdn.net/samwang_/article/details/70332924

使用前先確保電腦已經安裝了FFMPEG,並且配置好環境變數。檢測是否安裝配置好的方法:在cmd中輸入ffmpeg

 

class FFMEPG
{
//視訊切割
public string Cut(string OriginFile/*視訊原始檔*/, string DstFile/*生成的檔案*/, TimeSpan startTime/*開始時間*/, TimeSpan endTime/*結束時間*/)
{
string strCmd = "-ss 00:00:10 -i " + OriginFile + " -ss " +
startTime.ToString() + " -t " + endTime.ToString() + " -vcodec copy " + DstFile + " -y ";

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.exe";//要執行的程式名稱
p.StartInfo.Arguments = " " + strCmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;//可能接受來自呼叫程式的輸入資訊
p.StartInfo.RedirectStandardOutput = false;//由呼叫程式獲取輸出資訊
p.StartInfo.RedirectStandardError = false;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程式視窗


p.Start();//啟動程式


p.WaitForExit();//等待程式執行完退出程序

 

if (System.IO.File.Exists(DstFile))
{
return DstFile;
}
return "";
}
//視訊合併
public string Combine(string File1, string File2, string DstFile)
{
string strTmp1=File1+".ts";
string strTmp2 = File2 + ".ts";
string strCmd1 = " -i " + File1 + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + strTmp1 + " -y ";
string strCmd2 = " -i " + File2 + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + strTmp2 + " -y ";


string strCmd = " -i \"concat:" + strTmp1 + "|" +
strTmp2 + "\" -c copy -bsf:a aac_adtstoasc -movflags +faststart " + DstFile + " -y ";

 


//轉換檔案型別,由於不是所有型別的視訊檔案都支援直接合並,需要先轉換格式
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.exe";//要執行的程式名稱
p.StartInfo.Arguments = " " + strCmd1;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;//可能接受來自呼叫程式的輸入資訊
p.StartInfo.RedirectStandardOutput = false;//由呼叫程式獲取輸出資訊
p.StartInfo.RedirectStandardError = false;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程式視窗


p.Start();//啟動程式
p.WaitForExit();


//轉換檔案型別,由於不是所有型別的視訊檔案都支援直接合並,需要先轉換格式
p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.exe";//要執行的程式名稱
p.StartInfo.Arguments = " " + strCmd2;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;//可能接受來自呼叫程式的輸入資訊
p.StartInfo.RedirectStandardOutput = false;//由呼叫程式獲取輸出資訊
p.StartInfo.RedirectStandardError = false;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程式視窗


p.Start();//啟動程式
p.WaitForExit();

 


//合併
p = new System.Diagnostics.Process();
p.StartInfo.FileName = "ffmpeg.exe";//要執行的程式名稱
p.StartInfo.Arguments = " " + strCmd;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;//可能接受來自呼叫程式的輸入資訊
p.StartInfo.RedirectStandardOutput = false;//由呼叫程式獲取輸出資訊
p.StartInfo.RedirectStandardError = false;//重定向標準錯誤輸出
p.StartInfo.CreateNoWindow = false;//不顯示程式視窗


p.Start();//啟動程式


//向CMD視窗傳送輸入資訊:
// p.StandardInput.Write("ipconfig");


//string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();//等待程式執行完退出程序
//-ss表示搜尋到指定的時間 -i表示輸入的檔案 -y表示覆蓋輸出 -f表示強制使用的格式


if (System.IO.File.Exists(DstFile))
{
return DstFile;
}
return "";
}
}