C#使用ffmpeg產生Mp4檔案
ffmpeg是專門處理影音串流協定的程式,可以用來處理Rtsp協定或Rtmp協定。
這篇文章介紹如何在C#執行外部的ffmpeg.exe帶參數對串流協定來產生MP4檔案。
RTMP協定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private static string ffmpegPath = @".\ffmpeg\bin\ffmpeg.exe"; //開始轉檔 public static void StartRecordMp4FromRtmp(string recordServer, string directoryPath, string saveRecordPath, ref Process process) { try { if (!File.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (string.IsNullOrEmpty(recordServer)) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 recordServer is null"); } if (string.IsNullOrEmpty(saveRecordPath)) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 saveRecordPath is null"); } if (process == null) { process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); } var cmdParams = ffmpegCompletePath + string.Format(" -i {0} -f mp4 -c:v copy -c:a copy {1}", recordServer, saveRecordPath); process.StandardInput.WriteLine(cmdParams); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex)); LogUtil.logger.Fatal(Convert.ToString(ex)); } } //結束轉檔 public static void StopRecordMp4(ref Process process) { if (process == null) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 process is null"); return; } process.StandardInput.WriteLine("q"); process.StandardInput.WriteLine("exit"); process.Close(); process.Dispose(); process = null; }
|
RTSP協定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| private static string ffmpegPath = @".\ffmpeg\bin\ffmpeg.exe"; //開始轉檔 public static void StartRecordMp4FromRtsp(string recordServer, string directoryPath, string saveRecordPath, ref Process process) { try { if (!File.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } if (string.IsNullOrEmpty(recordServer)) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 recordServer is null"); } if (string.IsNullOrEmpty(saveRecordPath)) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 saveRecordPath is null"); } if (process == null) { process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); } //RTSP來源必須是H.264格式 var cmdParams = ffmpegCompletePath + string.Format(" -rtsp_transport tcp -i {0} -f mp4 -c:v copy -c:a copy -aspect 16:9 -movflags +faststart {1}", recordServer, saveRecordPath); process.StandardInput.WriteLine(cmdParams); } catch (Exception ex) { MessageBox.Show(Convert.ToString(ex)); LogUtil.logger.Fatal(Convert.ToString(ex)); } } //結束轉檔 public static void StopRecordMp4(ref Process process) { if (process == null) { LogUtil.logger.Warn("Method:ImageUtil.StartRecordMp4 process is null"); return; } //q指令下了之後,通常要等幾秒mp4的結尾才會寫入! //才可以對該mp4檔案操作,不然會發生檔案損毀0xc00d36c4錯誤。 process.StandardInput.WriteLine("q"); process.StandardInput.WriteLine("exit"); process.Close(); process.Dispose(); process = null; }
|