Ejemplo n.º 1
0
        public void Seek(TimeSpan seekToIn)
        {
            long positionInBaseUnits = (long)(seekToIn.TotalSeconds * ffmpeg.AV_TIME_BASE);
            int  errorCode           = ffmpeg.av_seek_frame(this.avFormatContextPtr, -1, positionInBaseUnits, ffmpeg.AVSEEK_FLAG_BACKWARD);

            FFmpegWrapperException.ThrowInCaseOfError(errorCode);
        }
Ejemplo n.º 2
0
        public bool TryRead(out MediaPacket mediaPacketOut)
        {
            AVPacket *packetPtr = ffmpeg.av_packet_alloc();
            int       errorCode = ffmpeg.av_read_frame(this.avFormatContextPtr, packetPtr);

            if (errorCode == ffmpeg.AVERROR_EOF)
            {
                ffmpeg.av_packet_unref(packetPtr);
                ffmpeg.av_free(packetPtr);
                mediaPacketOut = null;
                return(false);
            }

            try
            {
                FFmpegWrapperException.ThrowInCaseOfError(errorCode);
            }
            catch
            {
                ffmpeg.av_packet_unref(packetPtr);
                ffmpeg.av_free(packetPtr);
                throw;
            }

            mediaPacketOut = new MediaPacket(packetPtr, Streams[packetPtr->stream_index]);

            return(true);
        }
Ejemplo n.º 3
0
        public InputMediaFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException($"The file ({filePath}) does not exists!", nameof(filePath));
            }

            int errorCode;

            fixed(AVFormatContext **formatContextPointerPointer = &this.avFormatContextPtr)
            {
                errorCode = ffmpeg.avformat_open_input(formatContextPointerPointer, filePath, null, null);
                FFmpegWrapperException.ThrowInCaseOfError(errorCode);
            }

            errorCode = ffmpeg.avformat_find_stream_info(this.avFormatContextPtr, null);
            FFmpegWrapperException.ThrowInCaseOfError(errorCode);

            Streams = CreateStreams();

            MediaStream firstVideoStream = Streams.FirstOrDefault(s => s.CodecType == MediaType.AVMEDIA_TYPE_VIDEO);

            if (firstVideoStream == null)
            {
                throw new NotSupportedException($"The video file ({filePath}) does not contain video stream!");
            }

            FrameTime = TimeSpan.FromSeconds(1 / firstVideoStream.FrameRate.Value);
        }
Ejemplo n.º 4
0
        public OutputMediaFile(string fileNameIn)
        {
            try
            {
                fixed(AVIOContext **avioContextPtrPtr = &this.avioContextPtr)
                {
                    int error = ffmpeg.avio_open(avioContextPtrPtr, fileNameIn, ffmpeg.AVIO_FLAG_WRITE);

                    FFmpegWrapperException.ThrowInCaseOfError(error);
                }

                fixed(AVFormatContext **formatContextPtrPtr = &this.avFormatContextPtr)
                {
                    int error = ffmpeg.avformat_alloc_output_context2(formatContextPtrPtr, null, null, fileNameIn);

                    FFmpegWrapperException.ThrowInCaseOfError(error);
                    this.avFormatContextPtr->pb = avioContextPtr;
                }
            }
            catch
            {
                ReleaseTheUnmanagedResources();
                throw;
            }
        }
Ejemplo n.º 5
0
        public void WriteHeader()
        {
            if (Streams == null)
            {
                throw new InvalidOperationException("Streams were not added yet!");
            }

            int error = ffmpeg.avformat_write_header(this.avFormatContextPtr, null);

            FFmpegWrapperException.ThrowInCaseOfError(error);
        }
Ejemplo n.º 6
0
        public void WriteTailer()
        {
            int error = ffmpeg.av_write_trailer(this.avFormatContextPtr);

            FFmpegWrapperException.ThrowInCaseOfError(error);
        }
Ejemplo n.º 7
0
        public void WritePacket(MediaPacket packetIn)
        {
            int error = ffmpeg.av_write_frame(this.avFormatContextPtr, packetIn.PacketPtr);

            FFmpegWrapperException.ThrowInCaseOfError(error);
        }