FFMediaToolkit

2025-12-07 0 298

FFMediaToolkit

FFMediaToolkit是一个.NET库,用于创建和读取多媒体文件。它使用ffmpeg.autogen结合的天然FFMPEG文库。

特征

  • 以FFMPEG支持的许多格式解码/编码Audio-Video文件。
  • 将音频数据作为浮点阵列提取。
  • 访问时间戳的任何视频框架。
  • 使用元数据,像素格式,比特率,CRF,FPS,GOP,DIMENSIONS和其他编解码器设置创建图像创建视频。
  • 支持阅读多媒体章节和元数据。

代码样本

  • 将所有视频帧提取为PNG文件

     // Open video file
    using var file = MediaFile . Open ( @\"D:\\example\\movie.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:\\example\\frame_ { 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:\\videos\\movie.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:\\example\\video.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:\\example\\frame_ { i } .png\" ) ;
        // Encode the video frame
        file . Video . AddFrame ( new ImageData ( bmp . GetPixelSpan ( ) , ImagePixelFormat . Rgba32 , bmp . Width , bmp . Height ) ) ;
    } 

设置

从Nuget安装FFMediaToolkit软件包。

dotnet add package FFMediaToolkit

ffmpeg库不包括在软件包中。要使用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
  • MacOSiOSAndroid-不支持。

您需要设置FFMPEGLOADER.FFMPEGPATH,并使用FFMPEG库的完整路径。

在.NET框架项目中,您必须禁用构建– >在Visual Studio Project Properties中选择32位选项。

使用细节

FFMediaToolkit支持将视频帧解码到可以跨度<byte>,byte []或非管理内存的像素缓冲区中。您可以通过设置MediaOptions.videopixelformat属性来指定目标像素格式。默认格式为BGR24。

如果要处理或保存解码的帧,则可以将其传递到另一个图形库,如下所示。

  • 对于Skiasharp库:

    • 视频解码
       using var file = MediaFile . Open ( @\"D:\\example\\video.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:\\example\\frame.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:\\example\\image.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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 FFMediaToolkit https://www.zuozi.net/31669.html

常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务