码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-18 22:50:16      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:his   导出   媒体信息   size   sed   EOS   creat   director   dir   

  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         }

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

标签:his   导出   媒体信息   size   sed   EOS   creat   director   dir   

原文地址:https://www.cnblogs.com/edzjx/p/12913257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!