/// <summary> /// Add stream by copy <see cref="ffmpeg.avcodec_parameters_copy(AVCodecParameters*, AVCodecParameters*)"/>, /// </summary> /// <param name="stream"></param> /// <param name="flags"></param> /// <returns></returns> public MediaStream AddStream(MediaStream stream, int flags = 0) { AVStream *pstream = ffmpeg.avformat_new_stream(pFormatContext, null); pstream->id = (int)(pFormatContext->nb_streams - 1); ffmpeg.avcodec_parameters_copy(pstream->codecpar, stream.Stream.codecpar); pstream->codecpar->codec_tag = 0; MediaCodec mediaCodec = null; if (stream.Codec != null) { mediaCodec = MediaEncode.CreateEncode(stream.Codec.AVCodecContext.codec_id, flags, _ => { AVCodecContext *pContext = _; AVCodecParameters *pParameters = ffmpeg.avcodec_parameters_alloc(); ffmpeg.avcodec_parameters_from_context(pParameters, stream.Codec).ThrowExceptionIfError(); ffmpeg.avcodec_parameters_to_context(pContext, pParameters); ffmpeg.avcodec_parameters_free(&pParameters); pContext->time_base = stream.Stream.r_frame_rate.ToInvert(); }); } streams.Add(new MediaStream(pstream) { TimeBase = stream.Stream.r_frame_rate.ToInvert(), Codec = mediaCodec }); return(streams.Last()); }
public static MediaEncode CreateEncode(string codecName, int flags, Action <MediaCodec> setBeforeOpen = null, MediaDictionary opts = null) { MediaEncode encode = new MediaEncode(codecName); encode.Initialize(setBeforeOpen, flags, opts); return(encode); }
/// <summary> /// Add stream by encode /// </summary> /// <param name="encode">Used to codec stream. /// set null to add a data stream but no encoder, /// then use <see cref="WritePacket(MediaPacket)"/> write data directly. /// </param> /// <returns></returns> public MediaStream AddStream(MediaEncode encode) { AVStream *stream = ffmpeg.avformat_new_stream(pFormatContext, null); stream->id = (int)(pFormatContext->nb_streams - 1); // keep the order if (encode != null) { ffmpeg.avcodec_parameters_from_context(stream->codecpar, encode); stream->time_base = encode.AVCodecContext.time_base; } streams.Add(new MediaStream(stream) { Codec = encode }); return(streams.Last()); }