【C#】使用ffmpeg image2pipe将图片保存为mp4视频
文章目錄
- 需求
- 實(shí)現(xiàn)
需求
在正式開始之前,先介紹下我的需求是怎么樣的,基于此需求如何使用ffmpeg實(shí)現(xiàn)。僅供參考。
需求點(diǎn):
實(shí)現(xiàn)
在使用原生ffmpeg之前,筆者使用了幾個第三方的nuget庫,如:FFmpeg.AutoGen、Xabe.FFmpeg、Accord.Video.FFMPEG。前兩個庫要么只支持將文件夾里現(xiàn)有的圖片保存為mp4,要么不支持設(shè)置每幀的PTS,導(dǎo)致生成的mp4播放速度太快。最后選用了Accord.Video.FFMPEG,這個庫能滿足上述的三個需求點(diǎn)。無奈此庫已長期不維護(hù),當(dāng)上游的FPS>15時,WriteVideoFrame方法拋出異常的頻率會大大提升,導(dǎo)致內(nèi)存泄漏,而且當(dāng)前幀也會被丟掉。
然后項目使用的是.net452,一時半會版本也升級不上去,這就過濾大多數(shù)的nuget庫。最后,只能使用的原生的ffmpeg了。
ffmpeg只是提供了一個exe,并沒有官方的API可供我們調(diào)用,只提供了一大堆的參數(shù)說明,真是令人頭大。經(jīng)過不斷的看文檔和搜索調(diào)試之后,發(fā)現(xiàn)配置以下參數(shù)可以達(dá)到我們的需求。
以下對各參數(shù)做個簡單介紹:
- image2pipe:使用圖片管道,我們可以將圖片數(shù)據(jù)一直往管道里塞,ffmpeg會不斷將其添加到mp4文件中。用來滿足需求1和2.
- use_wallclock_as_timestamps 1:開啟此選項,ffmpeg就會將接收此圖片的時間作為該幀的timestamp。這樣生成的MP4播放速度才正常,滿足需求3.
- pix_fmt yuv420p:設(shè)置像素格式,解決生成的視頻無法使用windows media player播放的問題。
- -vsync passthrough:可以理解為動態(tài)幀率,根據(jù)上游的幀率來決定輸出mp4的幀率。默認(rèn)有以下幾個選項:
- passthrough :使用幀原始的timestamp.
- cfr (1):根據(jù)輸出幀率的配置,進(jìn)行自動插幀(上游幀率小于輸入幀率)或者丟幀(上游幀率大于輸入幀率)。
- vfr (2):類似于passthrough, 不過當(dāng)兩幀具有timestamp時會丟棄其中一個。
- drop:類似于passthrough,只不過會丟棄幀原始的timstamp,然后重新生成符合幀率要求的timestamp。
- auto (-1):默認(rèn)行為。在cfr和vfr之前自動選擇。
- maxrate:設(shè)置最大比特率
- 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根據(jù)timestamp,重復(fù)的丟棄 passthrough根據(jù)timestamp重復(fù)的不丟 -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數(shù)據(jù)寫入管道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>/// 異步線程啟動服務(wù)/// </summary>public override void StartAsync(){_isRunning = true;}/// <summary>/// 停止服務(wù)/// </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);}}}總結(jié)
以上是生活随笔為你收集整理的【C#】使用ffmpeg image2pipe将图片保存为mp4视频的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机实验室设计论文,实验室计算机网络论
- 下一篇: C# 与 Java前景,一切不以应用场景