【C#】使用ffmpeg image2pipe将图片保存为mp4视频
生活随笔
收集整理的這篇文章主要介紹了
【C#】使用ffmpeg image2pipe将图片保存为mp4视频
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 需求
- 實現
需求
在正式開始之前,先介紹下我的需求是怎么樣的,基于此需求如何使用ffmpeg實現。僅供參考。
需求點:
實現
在使用原生ffmpeg之前,筆者使用了幾個第三方的nuget庫,如:FFmpeg.AutoGen、Xabe.FFmpeg、Accord.Video.FFMPEG。前兩個庫要么只支持將文件夾里現有的圖片保存為mp4,要么不支持設置每幀的PTS,導致生成的mp4播放速度太快。最后選用了Accord.Video.FFMPEG,這個庫能滿足上述的三個需求點。無奈此庫已長期不維護,當上游的FPS>15時,WriteVideoFrame方法拋出異常的頻率會大大提升,導致內存泄漏,而且當前幀也會被丟掉。
然后項目使用的是.net452,一時半會版本也升級不上去,這就過濾大多數的nuget庫。最后,只能使用的原生的ffmpeg了。
ffmpeg只是提供了一個exe,并沒有官方的API可供我們調用,只提供了一大堆的參數說明,真是令人頭大。經過不斷的看文檔和搜索調試之后,發現配置以下參數可以達到我們的需求。
以下對各參數做個簡單介紹:
- image2pipe:使用圖片管道,我們可以將圖片數據一直往管道里塞,ffmpeg會不斷將其添加到mp4文件中。用來滿足需求1和2.
- use_wallclock_as_timestamps 1:開啟此選項,ffmpeg就會將接收此圖片的時間作為該幀的timestamp。這樣生成的MP4播放速度才正常,滿足需求3.
- pix_fmt yuv420p:設置像素格式,解決生成的視頻無法使用windows media player播放的問題。
- -vsync passthrough:可以理解為動態幀率,根據上游的幀率來決定輸出mp4的幀率。默認有以下幾個選項:
- passthrough :使用幀原始的timestamp.
- cfr (1):根據輸出幀率的配置,進行自動插幀(上游幀率小于輸入幀率)或者丟幀(上游幀率大于輸入幀率)。
- vfr (2):類似于passthrough, 不過當兩幀具有timestamp時會丟棄其中一個。
- drop:類似于passthrough,只不過會丟棄幀原始的timstamp,然后重新生成符合幀率要求的timestamp。
- auto (-1):默認行為。在cfr和vfr之前自動選擇。
- maxrate:設置最大比特率
- 123.mp4:保存的文件名或者路徑,注意里面不要有空格。
最后的C#代碼如下,我們需要使用Process類來啟動ffmpeg.exe。
public class FfmpegToVideoService {private bool _isRunning = false;private int _fps;private readonly Process _proc;/// <summary>/// Bitmap保存為MP4/// </summary>/// <param name="filePath">mp4要保存的路徑,如:D:\\a\b\123.mp4</param>/// <param name="maxBitRate">最大比特率,單位K</param>public FfmpegToVideoService(string filePath,int maxBitRate = 5000){var formattedPath = Path.GetFullPath(filePath);_proc = new Process();//-pix_fmt yuv420p -movflags +faststart -r {30} -i pipe:.bmp -r {_fps} -timecode 00:00:00.000//-vsync cfr自動差值 vfr根據timestamp,重復的丟棄 passthrough根據timestamp重復的不丟 -vsync passthrough//-r 30 入幀出幀都是30_proc.StartInfo.FileName = @"ffmpeg.exe";_proc.StartInfo.Arguments = $"-f image2pipe -use_wallclock_as_timestamps 1 -i - -c:v libx264 -pix_fmt yuv420p -vsync passthrough -maxrate {maxBitRate}k -an -y {formattedPath}";_proc.StartInfo.WorkingDirectory = CommonFunctions.BasePath;_proc.StartInfo.UseShellExecute = false;_proc.StartInfo.RedirectStandardInput = true;_proc.StartInfo.RedirectStandardOutput = true;_proc.Start();}// 將Bitmap數據寫入管道private void SendToPipe(Bitmap bitmap){if (_proc.StartInfo.RedirectStandardInput){using (MemoryStream ms = new MemoryStream()){bitmap.Save(ms, ImageFormat.Png);ms.WriteTo(_proc.StandardInput.BaseStream);}}}/// <summary>/// 異步線程啟動服務/// </summary>public override void StartAsync(){_isRunning = true;}/// <summary>/// 停止服務/// </summary>public override void Stop(){_isRunning = false;try{_proc.StartInfo.RedirectStandardInput = false;_proc.StartInfo.RedirectStandardOutput = false;_proc.StandardInput.Close();_proc.StandardOutput.Close();_proc.Close();}catch (Exception ex){Log.Error(ex, "");}}/// <summary>/// 添加item/// </summary>/// <param name="item"></param>public override void Add(FrameInfo item){if(_isRunning){SendToPipe(item.Bitmap);}}}總結
以上是生活随笔為你收集整理的【C#】使用ffmpeg image2pipe将图片保存为mp4视频的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机实验室设计论文,实验室计算机网络论
- 下一篇: C# 与 Java前景,一切不以应用场景