FFmpeg是一套可以用來記錄、轉換數字音頻、視頻,并能將其轉化為流的開源計算機程式,
官網:http://ffmpeg.org/
本文介紹Windows環境C# 呼叫ffmpeg.exe 來轉換視頻
public class ProcessUtil { private bool outPut; public string ProcessOutPut = ""; public ProcessUtil(bool outPut = false) { this.outPut = outPut; } private void Output(object sendProcess, DataReceivedEventArgs output) { ProcessOutPut += output.Data; Process p = sendProcess as Process; if (p.HasExited && p.ExitCode == 1) { // throw new Exception("視頻處理失敗"); } } public bool RunExe(string exePath, string argStr) #region { //ProcessOutPut = ""; bool isok = false; Process p = new Process();//建立外部呼叫執行緒 p.StartInfo.FileName = exePath; p.StartInfo.Arguments = argStr; //引數(這里就是FFMPEG的引數了) p.StartInfo.UseShellExecute = false;//不使用作業系統外殼程式啟動執行緒(一定為FALSE,詳細的請看MSDN) p.StartInfo.RedirectStandardError = true;//把外部程式錯誤輸出寫到StandardError流中(這個一定要注意,FFMPEG的所有輸出資訊,都為錯誤輸出流,用StandardOutput是捕獲不到任何訊息的 p.StartInfo.CreateNoWindow = false;//不創建行程視窗 try { if (this.outPut) { p.StartInfo.StandardErrorEncoding = Encoding.UTF8; p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程式(這里是FFMPEG)輸出流時候產生的事件,這里是把流的處理程序轉移到下面的方法中,詳細請查閱MSDN } p.Start();//啟動執行緒 p.BeginErrorReadLine();//開始異步讀取 p.WaitForExit();//阻塞等待行程結束 //ProcessOutPut = this.ProcessOutPut; } catch (Exception exp) { //Console.WriteLine(exp.Message); return isok; } finally { if (p.HasExited && p.ExitCode == 1) // 發生錯誤 isok = false; else isok = true; p.Close(); p.Dispose(); } return isok; } #endregion }
呼叫代碼:
string processPath = @"E:\ffmpeg\bin\ffmpeg.exe"; string originalVideoFile = @"E:\local\video.mp4"; string targetFile = $@"E:\local\名稱{Guid.NewGuid().ToString("N")}.mp4"; //ffmpeg -i input_file -vcodec h264 output_file //直接轉換為h264格式 string argStr = $"-y -i {originalVideoFile} -s 720X480 -b:v 1800k -r 30 -f mp4 " + targetFile; //引數(這里就是FFMPEG的引數了) "; string output = ""; var p = new FFexe.ProcessUtil(true); bool re = p.RunExe(processPath, argStr); output = p.ProcessOutPut;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481988.html
標籤:.NET技术
上一篇:它把RabbitMQ的復雜全屏蔽了,我朋友用它后被老板一夜晉升為.NET架構師
下一篇:返回列表