FFMpeg copy streams without transcode

前端 未结 1 2003
情话喂你
情话喂你 2021-01-06 18:08

I\'m trying to copy all streams from several files into one file without transcoding streams. Something you usually do with ffmpeg utility by ffmpeg -i “f

1条回答
  •  礼貌的吻别
    2021-01-06 18:44

    The reason is because you didn't set the encoder's codec_tag correctly.

    You should set the correct codec_tag, or set the codec_tag to 0 and leave the muxer to handle for you.

    Following is a sample code how to do.

    AVFormatContext *ofmt_ctx; // todo
    AVCodec *oc; // todo
    
    AVStream *stream = avformat_new_stream(ofmt_ctx, oc);
    
    if (stream != NULL) {
        // ...
        unsigned int tag = 0;
        // for ffmpeg new api 3.x
        AVCodecParameters *parameters = stream->codecpar;
        if (av_codec_get_tag2(ofmt_ctx->oformat->codec_tag, oc->id, &tag) == 0) {
            av_log(NULL, AV_LOG_ERROR, "could not find codec tag for codec id %d, default to 0.\n", oc->id);
        }
        parameters->codec_tag = tag;
        stream->codec = avcodec_alloc_context3(oc);
        // more setting for stream->codec
        avcodec_parameters_to_context(stream->codec, parameters);
    
        // for old ffmpeg version
        stream->codec->codec_tag = tag;
        // ...
    }
    

    0 讨论(0)
提交回复
热议问题