FFMpeg.AutoGen(2)讲解官方example代码:解码(未完成)

  1         /// <summary>
  2         /// 编码 把解码出来的jpg文件,再编码成UV420P
  3         /// </summary>
  4         private static unsafe void EncodeImagesToH264()
  5         {
  6 
  7             //获取解码出来的文件队列
  8             var frameFiles = Directory.GetFiles(".", "frame.*.jpg").OrderBy(x => x).ToArray();
  9             //获取第一张帧图片
 10             var fistFrameImage = Image.FromFile(frameFiles.First());
 11 
 12             //设置导出媒体信息
 13             var outputFileName = "out.h264";
 14             var fps = 25;
 15             var sourceSize = fistFrameImage.Size;
 16             var sourcePixelFormat = AVPixelFormat.AV_PIX_FMT_BGR24;
 17             var destinationSize = sourceSize;
 18             var destinationPixelFormat = AVPixelFormat.AV_PIX_FMT_YUV420P;
 19             using (var vfc = new VideoFrameConverter(sourceSize, sourcePixelFormat, destinationSize, destinationPixelFormat))
 20             {
 21                 using (var fs = File.Open(outputFileName, FileMode.Create)) // be advise only ffmpeg based player (like ffplay or vlc) can play this file, for the others you need to go through muxing
 22                 {
 23                     using (var vse = new H264VideoStreamEncoder(fs, fps, destinationSize))
 24                     {
 25                         var frameNumber = 0;
 26                         foreach (var frameFile in frameFiles)
 27                         {
 28                             byte[] bitmapData;
 29 
 30                             using (var frameImage = Image.FromFile(frameFile))
 31                             using (var frameBitmap = frameImage is Bitmap bitmap ? bitmap : new Bitmap(frameImage))
 32                             {
 33                                 bitmapData = GetBitmapData(frameBitmap);
 34                             }
 35 
 36                             fixed (byte* pBitmapData = bitmapData)
 37                             {
 38                                 var data = new byte_ptrArray8 { [0] = pBitmapData };
 39                                 var linesize = new int_array8 { [0] = bitmapData.Length / sourceSize.Height };
 40                                 var frame = new AVFrame
 41                                 {
 42                                     data = data,
 43                                     linesize = linesize,
 44                                     height = sourceSize.Height
 45                                 };
 46                                 var convertedFrame = vfc.Convert(frame);
 47                                 convertedFrame.pts = frameNumber * fps;
 48                                 vse.Encode(convertedFrame);
 49                             }
 50 
 51                             Console.WriteLine($"frame: {frameNumber}");
 52                             frameNumber++;
 53                         }
 54                     }
 55                 }
 56             }
 57         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值