完整的核心函数示例如下:
public int AddStream(AVStream* in_stream, bool isAudioDemuxer = false)
{
int ret = -1;
if (in_stream == null || (in_stream->codecpar->codec_type != AVMEDIA_TYPE_VIDEO && in_stream->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)) return ret;
AVStream *out_stream;
var in_codecpar = in_stream->codecpar;
out_stream = avformat_new_stream(fmtCtx, null);
if (out_stream == null) return -1;
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0) return ret;
AVDictionaryEntry* b = null;
while (true)
{
b = av_dict_get(in_stream->metadata, "", b, AV_DICT_IGNORE_SUFFIX);
if (b == null) break;
if (Utils.BytePtrToStringUTF8(b->key).ToLower() == "language" || Utils.BytePtrToStringUTF8(b->key).ToLower() == "lang")
av_dict_set(&out_stream->metadata, Utils.BytePtrToStringUTF8(b->key), Utils.BytePtrToStringUTF8(b->value), 0);
}
out_stream->codecpar->codec_tag = 0;
if (isAudioDemuxer)
{
mapInOutStreams2.Add((IntPtr)in_stream, (IntPtr)out_stream);
mapInInStream2.Add(in_stream->index, (IntPtr)in_stream);
}
else
{
mapInOutStreams.Add((IntPtr)in_stream, (IntPtr)out_stream);
mapInInStream.Add(in_stream->index, (IntPtr)in_stream);
}
return 0;
}
avformat_new_stream:Add a new stream to a media file.
avcodec_parameters_copy:ccopy the contents of src to dst any allocated fields in dst are freed and replaced with newly allocated duplicates of the corresponding fields in src.
av_dict_get:Get a dictionary entry with matching key.
av_dict_set:Set the given entry in *pm, overwriting an existing entry.