示例#1
0
        public MediaReader(Stream inputStream) : base(inputStream)
        {
            try {
                if (!inputStream.CanRead)
                {
                    throw new ArgumentException($"流不能被读取,请确保Stream.CanRead为true");
                }

                int resultCode;
                fixed(AVFormatContext **pFormatContext = &formatContext)
                {
                    resultCode = FF.avformat_open_input(pFormatContext, null, null, null);
                }
                if (resultCode != 0)
                {
                    throw new FFmpegException(resultCode);
                }

                resultCode = FF.avformat_find_stream_info(formatContext, null);
                if (resultCode != 0)
                {
                    throw new FFmpegException(resultCode);
                }

                var decoders = new Decoder[StreamCount];
                for (int i = 0; i < StreamCount; i++)
                {
                    decoders[i] = Decoder.Create(formatContext->Streams[i]);
                    if (formatContext->Streams[i]->Codec->CodecType == AVMediaType.Video)
                    {
                        FramesPerSecond  = formatContext->Streams[i]->AvgFrameRate.Value;
                        VideoStreamIndex = i;
                        // defaultStreamIndex = i;
                    }
                    formatContext->Streams[i]->CurDts = 0;
                }
                Decoders = decoders;
                Duration = new Timestamp(formatContext->Duration, Timestamp.FFmpeg_AVTimeBase);
            } catch {
                Dispose();
                throw;
            }
        }