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 "";
}
}
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM