public MediaReader(Stream stream, InFormat iformat = null, MediaDictionary options = null) { baseStream = stream; avio_Alloc_Context_Read_Packet = ReadFunc; avio_Alloc_Context_Seek = SeekFunc; pFormatContext = ffmpeg.avformat_alloc_context(); pFormatContext->pb = ffmpeg.avio_alloc_context((byte *)ffmpeg.av_malloc(bufferLength), bufferLength, 0, null, avio_Alloc_Context_Read_Packet, null, avio_Alloc_Context_Seek); fixed(AVFormatContext **ppFormatContext = &pFormatContext) { ffmpeg.avformat_open_input(ppFormatContext, null, iformat, options).ThrowExceptionIfError(); } ffmpeg.avformat_find_stream_info(pFormatContext, null).ThrowExceptionIfError(); base.Format = iformat ?? new InFormat(pFormatContext->iformat); for (int i = 0; i < pFormatContext->nb_streams; i++) { AVStream * pStream = pFormatContext->streams[i]; MediaDecode codec = MediaDecode.CreateDecode(pStream->codecpar->codec_id, _ => { ffmpeg.avcodec_parameters_to_context(_, pStream->codecpar); }); streams.Add(new MediaStream(pStream) { Codec = codec }); } }
/// <summary> /// init format demuxer by name /// </summary> /// <param name="name">e.g. mov,mp4 ...</param> public InFormat(string name) { unsafe { if (!string.IsNullOrEmpty(name)) { void * ifmtOpaque = null; AVInputFormat *iformat; while ((iformat = ffmpeg.av_demuxer_iterate(&ifmtOpaque)) != null) { InFormat format = new InFormat(iformat); // e.g. format.Name == "mov,mp4,m4a,3gp,3g2,mj2" string[] names = format.Name.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in names) { if (item == name.ToLower()) { pInputFormat = iformat; return; } } } } throw new FFmpegException(ffmpeg.AVERROR_DEMUXER_NOT_FOUND); } }
/// <summary> /// Load path /// </summary> /// <param name="url"></param> /// <param name="iformat"></param> /// <param name="options"></param> public MediaReader(string url, InFormat iformat = null, MediaDictionary options = null) { unsafe { fixed(AVFormatContext **ppFormatContext = &pFormatContext) { ffmpeg.avformat_open_input(ppFormatContext, url, iformat, options).ThrowExceptionIfError(); } ffmpeg.avformat_find_stream_info(pFormatContext, null).ThrowExceptionIfError(); base.Format = iformat ?? new InFormat(pFormatContext->iformat); for (int i = 0; i < pFormatContext->nb_streams; i++) { AVStream * pStream = pFormatContext->streams[i]; MediaDecode codec = MediaDecode.CreateDecode(pStream->codecpar->codec_id, _ => { ffmpeg.avcodec_parameters_to_context(_, pStream->codecpar); }); streams.Add(new MediaStream(pStream) { Codec = codec }); } } }
/// <summary> /// Load stream, default buffer size is 4096 /// </summary> /// <param name="stream"></param> /// <param name="iformat"></param> /// <param name="options"></param> public MediaReader(Stream stream, InFormat iformat = null, MediaDictionary options = null) : this(stream, 4096, iformat, options) { }