Exemplo n.º 1
0
        /// <summary>
        /// Yuv420P sample
        /// </summary>
        /// <param name="outputFile">output file</param>
        /// <param name="width">video width</param>
        /// <param name="height">video height</param>
        /// <param name="fps">video fps</param>
        public FillYuv420PSample(string outputFile, int width, int height, int fps)
        {
            var dir = Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(outputFile), Path.GetFileNameWithoutExtension(outputFile))).FullName;

            using (MediaWriter writer = new MediaWriter(outputFile))
            {
                writer.AddStream(MediaEncoder.CreateVideoEncode(writer.Format, width, height, fps));
                writer.Initialize();

                VideoFrame     srcframe       = new VideoFrame(width, height, FFmpeg.AutoGen.AVPixelFormat.AV_PIX_FMT_YUV420P);
                PixelConverter pixelConverter = new PixelConverter(writer[0].Codec);

                Random random = new Random();
                for (int i = 0; i < fps * 10; i++)
                {
                    // fill video frame
                    FillYuv420P(srcframe, i);

                    foreach (var dstframe in pixelConverter.Convert(srcframe))
                    {
                        dstframe.Pts = i;
                        SaveFrame(dstframe, Path.Combine(dir, $"{i}.bmp"));
                        foreach (var packet in writer[0].WriteFrame(dstframe))
                        {
                            writer.WritePacket(packet);
                        }
                    }
                }

                // flush cache
                writer.FlushMuxer();
            }
        }
Exemplo n.º 2
0
 public Mp4VideoWriter AddVideo(int width, int height, int fps)
 {
     if (writer.Where(_ => _.Codec.Type == AVMediaType.AVMEDIA_TYPE_VIDEO).Count() == 0)
     {
         Height = height;
         Width  = width;
         FPS    = fps;
         var st = writer.AddStream(MediaEncoder.CreateVideoEncode(writer.Format, width, height, fps));
         videoIndex     = writer.Count() - 1;
         pixelConverter = new PixelConverter(st.Codec);
     }
     return(this);
 }
Exemplo n.º 3
0
        /// <summary>
        /// create a 60 / <paramref name="fps"/> second video
        /// </summary>
        /// <param name="outputFile">video output</param>
        /// <param name="width">video width</param>
        /// <param name="height">video height</param>
        /// <param name="fps">video fps</param>
        public EncodeVideoByMat(string outputFile, int width, int height, int fps)
        {
            using (MediaWriter writer = new MediaWriter(outputFile))
            {
                writer.AddStream(MediaEncoder.CreateVideoEncode(writer.Format, width, height, fps));
                writer.Initialize();

                VideoFrame dstframe = VideoFrame.CreateFrameByCodec(writer[0].Codec);

                Random random = new Random();
                for (int i = 0; i < 61; i++)
                {
                    // create a video frame by Mat
                    byte b = (byte)random.Next(0, 255);
                    byte g = (byte)random.Next(0, 255);
                    byte r = (byte)random.Next(0, 255);
                    using (Image <Bgr, byte> image = new Image <Bgr, byte>(width, height, new Bgr(b, g, r)))
                    {
                        string line1 = $"pts = {i}, color = [{b,3},{g,3},{r,3}]";
                        string line2 = $"time = {DateTime.Now:HH:mm:ss.fff}";
                        image.Draw(line1, new System.Drawing.Point(30, 50), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1, new Bgr(255 - b, 255 - g, 255 - r));
                        image.Draw(line2, new System.Drawing.Point(30, 100), Emgu.CV.CvEnum.FontFace.HersheyDuplex, 1, new Bgr(255 - b, 255 - g, 255 - r));
                        dstframe = image.Mat.ToVideoFrame(AVPixelFormat.AV_PIX_FMT_YUV420P);
                    }

                    dstframe.Pts = i; // video pts = seconds from video start * fps (pts can only increase).
                    // write video frame, many cases: one frame more out packet, first frame no out packet, etc.
                    // so use IEnumerable.
                    foreach (var packet in writer[0].WriteFrame(dstframe))
                    {
                        writer.WritePacket(packet);
                    }
                }

                // flush cache
                writer.FlushMuxer();
            }
        }