很多時候我們要讀寫視頻,C#讀視頻(對視頻解碼)網上的例子很多,然而寫視頻(對視頻編碼)的例子卻很少,也很少能搜索到有用的信息。下面是使用Aforge.Net寫視頻的簡單方案。
Aforge.Net 是一個 C# 版的圖像和計算機視覺庫,網站 http://www.aforgenet.com/ 。下載安裝。Aforge.Net 有一個子項目 AForge.Video.FFMPEG 對 ffmpeg 的視頻操作進行了封裝。
添加對 AForge.Video.FFMPEG.dll, AForge.Video.dll和 AForge.dll 三個 dll 的引用,Aforge.Net 的文檔中提供了個寫視頻的例子:
int width = 320;
int height = 240;// create instance of video writer
VideoFileWriter writer = new VideoFileWriter( );
// create new video file
writer.Open( "test.avi", width, height, 25, VideoCodec.MPEG4 );
// create a bitmap to save into the video file
Bitmap image = new Bitmap( width, height, PixelFormat.Format24bppRgb );
// write 1000 video frames
for ( int i = 0; i < 1000; i++ )
{
image.SetPixel( i % width, i % height, Color.Red );
writer.WriteVideoFrame( image );
}
writer.Close( );
由於 Aforge.Net 封裝的是 ffmpeg,因此需要將 ffmpeg 的幾個dll(AForge.NET\Framework\Externals\ffmpeg\bin路徑下的全部dll)放在執行路徑下。
如此簡單 …… </ p>
