FFMediaToolkit
FFMediaToolkit是一个.NET库,用于创建和读取多媒体文件。它使用ffmpeg.autogen结合的天然FFMPEG文库。
特征
- 以FFMPEG支持的许多格式解码/编码Audio-Video文件。
- 将音频数据作为浮点阵列提取。
- 访问时间戳的任何视频框架。
- 使用元数据,像素格式,比特率,CRF,FPS,GOP,DIMENSIONS和其他编解码器设置创建图像创建视频。
- 支持阅读多媒体章节和元数据。
代码样本
将所有视频帧提取为PNG文件
// Open video file using var file = MediaFile . Open ( @"D:examplemovie.mp4" , new MediaOptions ( ) { VideoPixelFormat = ImagePixelFormat . Rgba32 } ) ; // Get pixel buffer from SkiaSharp bitmap using var bitmap = new SKBitmap ( file . Video . Info . FrameSize . Width , file . Video . Info . FrameSize . Height , SKColorType . Rgba8888 , SKAlphaType . Unpremul ) ; var pixelBuffer = bitmap . GetPixelSpan ( ) ; int i = 0 ; // Iterate over all frames in the video - decoded frame will be written to the buffer while ( file . Video . TryGetNextFrame ( pixelBuffer ) ) { // Save image as PNG file using var fs = File . OpenWrite ( $@ "D:exampleframe_ { i ++ } .png" ) ; bitmap . Encode ( fs , SKEncodedImageFormat . Png , 100 ) ; }
此示例使用SkiaSharp保存解码的帧。有关其他图形库的示例,请参见使用详细信息部分。
视频解码
// Open a multimedia file // You can use the MediaOptions properties to set decoder options var file = MediaFile . Open ( @"C:videosmovie.mp4" ) ; // Get the frame at 5th second of the video var frame5s = file . Video . GetFrame ( TimeSpan . FromSeconds ( 5 ) ) ; // Print information about the video stream Console . WriteLine ( $ "Bitrate: { file . Info . Bitrate / 1000.0 } kb/s" ) ; var info = file . Video . Info ; Console . WriteLine ( $ "Duration: { info . Duration } " ) ; Console . WriteLine ( $ "Frames count: { info . NumberOfFrames ?? "N/A" } " ) ; var frameRateInfo = info . IsVariableFrameRate ? "average" : "constant" ; Console . WriteLine ( $ "Frame rate: { info . AvgFrameRate } fps ( { frameRateInfo } )" ) ; Console . WriteLine ( $ "Frame size: { info . FrameSize } " ) ; Console . WriteLine ( $ "Pixel format: { info . PixelFormat } " ) ; Console . WriteLine ( $ "Codec: { info . CodecName } " ) ; Console . WriteLine ( $ "Is interlaced: { info . IsInterlaced } " ) ;
从图像编码视频。
// You can set codec, bitrate, frame rate and many other options here var settings = new VideoEncoderSettings ( width : 1920 , height : 1080 , framerate : 30 , codec : VideoCodec . H264 ) { EncoderPreset = EncoderPreset . Fast , CRF = 17 , } ; // Create output file using var file = MediaBuilder . CreateContainer ( @"D:examplevideo.mp4" ) . WithVideo ( settings ) . Create ( ) ; for ( int i = 0 ; i < 300 ; i ++ ) { // Load image using SkiaSharp (other libraries are also supported if provide access to pixel buffer) using var bmp = SKBitmap . Decode ( $@ "D:exampleframe_ { i } .png" ) ; // Encode the video frame file . Video . AddFrame ( new ImageData ( bmp . GetPixelSpan ( ) , ImagePixelFormat . Rgba32 , bmp . Width , bmp . Height ) ) ; }
设置
从Nuget安装FFMediaToolkit软件包。
dotnet add package FFMediaToolkitffmpeg库不包括在软件包中。要使用FFMediaToolkit ,您需要FFMPEG共享的构建二进制文件:Avcodec(V61),Avformat(V61),Avutil(V59),Swresample(V5),SWSCALE(V8)。
支持的FFMPEG版本:7.x(共享构建)
- Windows-您可以从BTBN/ffmpeg -Builds或gyan.dev下载它。您只需要 *.dll文件从zip软件包的。 bin目录(非。 lib )。将二进制文件放入。
- Linux-使用软件包管理器下载FFMPEG。默认路径为/usr/lib/*-linux-gnu
- MacOS , iOS , Android-不支持。
您需要设置FFMPEGLOADER.FFMPEGPATH,并使用FFMPEG库的完整路径。
在.NET框架项目中,您必须禁用构建- >在Visual Studio Project Properties中选择32位选项。
使用细节
FFMediaToolkit支持将视频帧解码到可以跨度
如果要处理或保存解码的帧,则可以将其传递到另一个图形库,如下所示。
对于Skiasharp库:
- 视频解码
using var file = MediaFile . Open ( @"D:examplevideo.mp4" , new MediaOptions ( ) { StreamsToLoad = MediaMode . Video , VideoPixelFormat = ImagePixelFormat . Rgba32 } ) ; using var bitmap = new SKBitmap ( file . Video . Info . FrameSize . Width , file . Video . Info . FrameSize . Height , SKColorType . Rgba8888 , SKAlphaType . Unpremul ) ; var buffer = bitmap . GetPixelSpan ( ) ; while ( file . Video . TryGetNextFrame ( buffer ) ) { // do something }
- 视频编码
using var bmp = SKBitmap . Decode ( $@ "D:exampleframe.png" ) ; mediaFile . Video . AddFrame ( new ImageData ( bmp . GetPixelSpan ( ) , ImagePixelFormat . Rgba32 , bmp . Width , bmp . Height ) ) ;
- 视频解码
用于图像库库:
- 视频解码
var stride = ImageData . EstimateStride ( file . Video . Info . FrameSize . Width , ImagePixelFormat . Bgr24 ) ; var buffer = new byte [ stride * file . Video . Info . FrameSize . Height ] ; var bmp = Image . WrapMemory < Bgr24 > ( buffer , file . Video . Info . FrameSize . Width , file . Video . Info . FrameSize . Height ) ; while ( file . Video . TryGetNextFrame ( buffer ) ) { // do something }
- 视频解码
对于gdi+ system.drawing.bitmap(仅Windows):
- 视频解码
// Create bitmap once var rect = new Rectangle ( Point . Empty , file . Video . Info . FrameSize ) ; var bitmap = new Bitmap ( rect . Width , rect . Height , PixelFormat . Format24bppRgb ) ; // ... // Read next frame var bitLock = bitmap . LockBits ( rect , ImageLockMode . WriteOnly , PixelFormat . Format24bppRgb ) ; file . Video . TryGetNextFrame ( bitLock . Scan0 , bitLock . Stride ) ; bitmap . UnlockBits ( bitLock ) ;
- 视频编码
var rect = new Rectangle ( Point . Empty , bitmap . Size ) ; var bitLock = bitmap . LockBits ( rect , ImageLockMode . ReadOnly , PixelFormat . Format24bppRgb ) ; var bitmapData = ImageData . FromPointer ( bitLock . Scan0 , bitmap . Size , ImagePixelFormat . Bgr24 ) ; mediaFile . Video . AddFrame ( bitmapData ) ; // Encode the frame bitmap . UnlockBits ( bitLock ) ; // UnlockBits() must be called after encoding the frame
- 视频解码
对于带有WPF UI的桌面应用程序(仅Windows):
视频解码
using System . Windows . Media . Imaging ; // Create bitmap once var bmp = new WriteableBitmap ( media . Video . Info . FrameSize . Width , media . Video . Info . FrameSize . Height , 96 , 96 , PixelFormats . Bgr24 , null ) ; // ... // Read next frame bmp . Lock ( ) ; var success = media . Video . TryGetNextFrame ( bmp . BackBuffer , bmp . BackBufferStride ) ; if ( success ) { bmp . AddDirtyRect ( new Int32Rect ( 0 , 0 , media . Video . Info . FrameSize . Width , media . Video . Info . FrameSize . Height ) ) ; } bmp . Unlock ( ) ;
视频编码
var bitmapSource = new BitmapImage ( new Uri ( @"D:exampleimage.png" ) ) ; var wb = new WriteableBitmap ( bitmap ) ; mediaFile . Video . AddFrame ( ImageData . FromPointer ( wb . BackBuffer , ImagePixelFormat . Bgra32 , wb . PixelWidth , wb . PixelHeight ) ) ;
视觉基本用法
将解码的位图直接写入WPF WritableBitMap缓冲区:
Dim file As FileStream = New FileStream( "path to the video file" , FileMode.Open, FileAccess.Read)
Dim media As MediaFile = MediaFile.Load(file)
Dim bmp As WriteableBimap = New WriteableBitmap(media.Video.Info.FrameSize.Width, media.Video.Info.FrameSize.Height, 96 , 96 , PixelFormats.Bgr24, Nothing )
bmp.Lock()
Dim decoded As Boolean = media.Video.TryGetFrame(TimeSpan.FromMinutes( 1 ), bmp.BackBuffer, bmp.BackBufferStride)
If decoded Then
bmp.AddDirtyRect( New Int32Rect( 0 , 0 , media.Video.Info.FrameSize.Width, media.Video.Info.FrameSize.Height))
End If
bmp.Unlock()
imageBox.Source = bmp将Imagedata转换为字节数组:
Dim data() As Byte = media.Video.GetNextFrame().Data.ToArray() 许可
该项目已根据MIT许可获得许可。
下载源码
通过命令行克隆项目:
git clone https://github.com/radek-k/FFMediaToolkit.git