Beispiel #1
0
        /// <summary>
        /// <see cref="sws_getCachedContext(SwsContext*, int, int, AVPixelFormat, int, int, AVPixelFormat, int, SwsFilter*, SwsFilter*, double*)"/>
        /// <see cref="sws_scale(SwsContext*, byte*[], int[], int, int, byte*[], int[])"/>
        /// </summary>
        public static IEnumerable <Frame> ConvertFrames(this CodecContext c, IEnumerable <Frame> sourceFrames, ScaleFlag flags = ScaleFlag.Bilinear)
        {
            using var destFrame = c.CreateFrame();
            int pts = 0;

            if (c.Codec.Type == MediaType.Video)
            {
                using var frameConverter = new FrameConverter();
                foreach (var sourceFrame in sourceFrames)
                {
                    frameConverter.ConvertFrame(sourceFrame, destFrame, flags);
                    destFrame.Pts = pts++;
                    yield return(destFrame);
                }
            }
            else if (c.Codec.Type == MediaType.Audio)
            {
                using var frameConverter = new SampleConverter();
                foreach (var sourceFrame in sourceFrames)
                {
                    frameConverter.ConvertFrame(destFrame, sourceFrame);
                    destFrame.Pts = pts += c.FrameSize;
                    yield return(destFrame);
                }
            }
        }
        private static void WriteImageTo(Frame frame, FormatContext fc)
        {
            Codec codec       = Codec.FindEncoder(fc.OutputFormat.VideoCodec);
            var   mediaStream = new MediaStream(fc);

            using CodecContext codecContext = new CodecContext(codec)
                  {
                      PixelFormat = codec.PixelFormats.First(),
                      Width       = frame.Width,
                      Height      = frame.Height,
                      TimeBase    = new MediaRational(1, 25),
                      Flags       = fc.OutputFormat.Flags.HasFlag(FormatOutputFlag.Globalheader) ? CodecFlag.GlobalHeader : CodecFlag.None,
                  };
            codecContext.Open(codec);
            mediaStream.Codecpar.CopyFrom(codecContext);

            if ((PixelFormat)frame.Format == codecContext.PixelFormat)
            {
                WriteAction(frame, fc, codecContext);
            }
            else
            {
                using var tempFrame      = Frame.CreateWritableVideo(frame.Width, frame.Height, codecContext.PixelFormat);
                using var frameConverter = new FrameConverter();
                frameConverter.ConvertFrame(frame, tempFrame);
                WriteAction(tempFrame, fc, codecContext);
            }
        }