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代码:解码(未完成)
最新推荐文章于 2024-09-13 21:54:44 发布