ffmpeg api替代转码和恢复相同文件

发布于 2025-02-09 09:32:34 字数 9309 浏览 3 评论 0 原文

上下文

你好!

我目前正在研究一个小型库的开发,允许在任何帧上剪切H.264视频,但不重新编码(转编码)整个视频。这个想法是仅重新编码我们要剪切的共和党,然后直接重写其他共和党。

avcut项目( https://github.com/anyc/avcut )允许这样做,但是需要对每个软件包进行系统的解码,并且似乎不适用于我可以进行的测试以及GitHub问题中最近的反馈中的FFMPEG版本。

作为初学者,我从ffmpeg文档中提供的代码示例开始,特别是:

问题遇到

的问题是,我无法同时进行转码和恢复正常工作。特别是,取决于我使用的方法来初始化 avcodecparameters , transcoding works, or remuxing works:

  • avcodec_parameters_copy works well for remuxing
  • avcodec_parameters_from_context works well for transcoding

In case I choose avcodec_parameters_from_context, the transcoded GOP are correctly由我的视频播放器阅读(假释),但没有读取remux的数据包,并且FFProbe不会显示/检测它们。

如果我选择 AVCODEC_PARAMETERS_FROM_CONTEXT ,我的视频播放器正确读取了remuxing GOP,但是转码key_frame被漏洞(我有一个印象,即B-frame和p-frame and p-frame还可以),并且<<代码> ffprobe -i 返回有关键框架NAL的错误:

[h264 @ 0x55ec8a079300] sps_id 32 out of range
[h264 @ 0x55ec8a079300] Invalid NAL unit size (1677727148 > 735).
[h264 @ 0x55ec8a079300] missing picture in access unit with size 744

我怀疑问题与数据包的外推有关。通过一些实验对输出的不同属性进行了 avcodecparameters 似乎是,它似乎是它似乎是 factradata factradata_size 属性负责一种方法或另一种方法的功能。

版本

FFMPEG开发分支机构于2022-05-17从 https://github.com/ffmpeg.com/ffmpeg/ffmpeg/ffmpeg 。

编译 - 启用libx264-- enable-gpl- enable-decoder = png -enable-engoder = png

代码>代码

我的代码写在C ++中,基于两个类:定义的类:输入文件( inputContexts )上的参数和方法和为输出文件定义它们的类( outputscontexts )。这两个类的代码在以下文件中定义:

问题是以下问题:

  • 流初始化
int OutputContexts::init(const char* out_filename, InputContexts* input_contexts){
    int ret;
    int stream_index = 0;

    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        return ret;
    }

    av_dump_format(ofmt_ctx, 0, out_filename, 1);
 
    encoders.resize(input_contexts->ifmt_ctx->nb_streams, nullptr);
    codecs.resize(input_contexts->ifmt_ctx->nb_streams, nullptr);
  
    // stream mapping
    for (int i = 0; i < input_contexts->ifmt_ctx->nb_streams; i++) {
        AVStream *out_stream;
        AVStream *in_stream = input_contexts->ifmt_ctx->streams[i];
        AVCodecContext* decoder_ctx = input_contexts->decoders[i];
 
        // add new stream to output context
        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            return ret;
        }

        // from avcut blog
        av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);

        out_stream->time_base = in_stream->time_base;

        // encoder
        if (decoder_ctx->codec_type == AVMEDIA_TYPE_VIDEO){
            ret = prepare_encoder_video(i, input_contexts);
            if (ret < 0){
                fprintf(stderr, "Error while preparing encoder for stream #%u\n", i);
                return ret;
            }

            // from avcut
            out_stream->sample_aspect_ratio = in_stream->sample_aspect_ratio;

            // works well for remuxing
            ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
            if (ret < 0) {
                fprintf(stderr, "Failed to copy codec parameters\n");
                return ret;
            }

            // works well for transcoding
            // ret = avcodec_parameters_from_context(out_stream->codecpar, encoders[i]);
            // if (ret < 0) {
            //     av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
            //     return ret;
            // }

        } else if (decoder_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            ...
        } else {
            ...
        }

        // TODO useful ???
        // set current stream position to 0
        // out_stream->codecpar->codec_tag = 0;
    }

    // opening output file in write mode with the ouput context
    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            return ret;
        }
    }
 
    // write headers from output context in output file
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        return ret;
    }

    return ret;
}
  • avCodeCcontext初始化编码器
int OutputContexts::prepare_encoder_video(int stream_index, InputContexts* input_contexts){
    int ret;
    const AVCodec* encoder;
    AVCodecContext* decoder_ctx = input_contexts->decoders[stream_index];
    AVCodecContext* encoder_ctx;

    if (video_index >= 0){
        fprintf(stderr, "Impossible to mark stream #%u as video, stream #%u is already registered as video stream.\n", 
                stream_index, video_index);
        return -1; //TODO change this value for correct error code
    }
    video_index = stream_index;

    if(decoder_ctx->codec_id == AV_CODEC_ID_H264){
        encoder = avcodec_find_encoder_by_name("libx264");
        if (!encoder) {
            av_log(NULL, AV_LOG_FATAL, "Encoder libx264 not found\n");
            return AVERROR_INVALIDDATA;
        }
        fmt::print("Encoder libx264 will be used for stream {}.\n", stream_index);
    } else {
        std::string s = fmt::format("No video encoder found for the given codec_id: {}\n", avcodec_get_name(decoder_ctx->codec_id));
        av_log(NULL, AV_LOG_FATAL, s.c_str());
        return AVERROR_INVALIDDATA;
    }
    
    encoder_ctx = avcodec_alloc_context3(encoder);
    if (!encoder_ctx) {
        av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
        return AVERROR(ENOMEM);
    }

    // from avcut
    encoder_ctx->time_base = decoder_ctx->time_base;
    encoder_ctx->ticks_per_frame = decoder_ctx->ticks_per_frame;
    encoder_ctx->delay = decoder_ctx->delay;
    encoder_ctx->width = decoder_ctx->width;
    encoder_ctx->height = decoder_ctx->height;
    encoder_ctx->pix_fmt = decoder_ctx->pix_fmt;
    encoder_ctx->sample_aspect_ratio = decoder_ctx->sample_aspect_ratio;
    encoder_ctx->color_primaries = decoder_ctx->color_primaries;
    encoder_ctx->color_trc = decoder_ctx->color_trc;
    encoder_ctx->colorspace = decoder_ctx->colorspace;
    encoder_ctx->color_range = decoder_ctx->color_range;
    encoder_ctx->chroma_sample_location = decoder_ctx->chroma_sample_location;
    encoder_ctx->profile = decoder_ctx->profile;
    encoder_ctx->level = decoder_ctx->level;

    encoder_ctx->thread_count = 1; // spawning more threads causes avcodec_close to free threads multiple times
    encoder_ctx->codec_tag = 0;
    
    // correct values ???
    encoder_ctx->qmin = 16;
    encoder_ctx->qmax = 26;
    encoder_ctx->max_qdiff = 4;
    // end from avcut

    // according to avcut, should not be set
    // if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER){
    //     encoder_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    // }

    ret = avcodec_open2(encoder_ctx, encoder, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", stream_index);
        return ret;
    }
    
    codecs[stream_index] = encoder;
    encoders[stream_index] = encoder_ctx;

    return ret;
}

示例

以说明我的问题,我在这里使用两个类在文件中使用我的类中遇到的每个密钥框架进行转编码和ramux在thecrod和remux之间进行交替。

g++ -o trans_remux trans_remux.cpp contexts.cpp -D__STDC_CONSTANT_MACROS `pkg-config --libs libavfilter` -lfmt -g

sharing 使用 avcodec_parameters_copy contexts.cpp:333 ),因此它可以很好地恢复。如果要使用 avcodec_parameters_from_context 测试版本,请在 contexts.cpp 中从第333行到337的评论,以及第340至344号线至344和重新计算的不加评价。

Context

Hello !

I'm currently working on the development of a small library allowing to cut an h.264 video on any frame, but without re-encoding (transcoding) the whole video. The idea is to re-encode only the GOP on which we want to cut, and to rewrite (remux) directly the others GOP.

The avcut project (https://github.com/anyc/avcut) allows to do that, but requires a systematic decoding of each package, and seems to not work with the recent versions of ffmpeg from the tests I could do and from the recent feedbacks in the github issues.

As a beginner, I started from the code examples provided in the ffmpeg documentation, in particular: transcoding.c and remuxing.c.

Problem encountered

The problem I'm having is that I can't get both transcoding and remuxing to work properly at the same time. In particular, depending on the method I use to initialize the AVCodecParameters of the output video stream, transcoding works, or remuxing works:

  • avcodec_parameters_copy works well for remuxing
  • avcodec_parameters_from_context works well for transcoding

In case I choose avcodec_parameters_from_context, the transcoded GOP are correctly read by my video player (parole), but the remuxed packets are not read, and ffprobe does not show/detect them.

In case I choose avcodec_parameters_from_context, the remuxing GOP are correctly read by my video player, but the transcoding key_frame are bugged (I have the impression that the b-frame and p-frame are ok), and ffprobe -i return an error about the NAL of the key-frames:

[h264 @ 0x55ec8a079300] sps_id 32 out of range
[h264 @ 0x55ec8a079300] Invalid NAL unit size (1677727148 > 735).
[h264 @ 0x55ec8a079300] missing picture in access unit with size 744

I suspect that the problem is related to the extradata of the packets. Through some experiments on the different attributes of the output AVCodecParameters, it seems that it is the extradata and extradata_size attributes that are responsible for the functioning of one method or the other.

Version

ffmpeg development branch retrieved on 2022-05-17 from https://github.com/FFmpeg/FFmpeg.

Compiled with --enable-libx264 --enable-gpl --enable-decoder=png --enable-encoder=png

Code

My code is written in c++ and is based on two classes: a class defining the parameters and methods on the input file (InputContexts) and a class defining them for the output file (OutputContexts). The code of these two classes is defined in the following files:

The code normally involved in the problem is the following:

  • stream initialization
int OutputContexts::init(const char* out_filename, InputContexts* input_contexts){
    int ret;
    int stream_index = 0;

    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        return ret;
    }

    av_dump_format(ofmt_ctx, 0, out_filename, 1);
 
    encoders.resize(input_contexts->ifmt_ctx->nb_streams, nullptr);
    codecs.resize(input_contexts->ifmt_ctx->nb_streams, nullptr);
  
    // stream mapping
    for (int i = 0; i < input_contexts->ifmt_ctx->nb_streams; i++) {
        AVStream *out_stream;
        AVStream *in_stream = input_contexts->ifmt_ctx->streams[i];
        AVCodecContext* decoder_ctx = input_contexts->decoders[i];
 
        // add new stream to output context
        out_stream = avformat_new_stream(ofmt_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            return ret;
        }

        // from avcut blog
        av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);

        out_stream->time_base = in_stream->time_base;

        // encoder
        if (decoder_ctx->codec_type == AVMEDIA_TYPE_VIDEO){
            ret = prepare_encoder_video(i, input_contexts);
            if (ret < 0){
                fprintf(stderr, "Error while preparing encoder for stream #%u\n", i);
                return ret;
            }

            // from avcut
            out_stream->sample_aspect_ratio = in_stream->sample_aspect_ratio;

            // works well for remuxing
            ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
            if (ret < 0) {
                fprintf(stderr, "Failed to copy codec parameters\n");
                return ret;
            }

            // works well for transcoding
            // ret = avcodec_parameters_from_context(out_stream->codecpar, encoders[i]);
            // if (ret < 0) {
            //     av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
            //     return ret;
            // }

        } else if (decoder_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            ...
        } else {
            ...
        }

        // TODO useful ???
        // set current stream position to 0
        // out_stream->codecpar->codec_tag = 0;
    }

    // opening output file in write mode with the ouput context
    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            return ret;
        }
    }
 
    // write headers from output context in output file
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        return ret;
    }

    return ret;
}
  • AVCodecContext initialization for encoder
int OutputContexts::prepare_encoder_video(int stream_index, InputContexts* input_contexts){
    int ret;
    const AVCodec* encoder;
    AVCodecContext* decoder_ctx = input_contexts->decoders[stream_index];
    AVCodecContext* encoder_ctx;

    if (video_index >= 0){
        fprintf(stderr, "Impossible to mark stream #%u as video, stream #%u is already registered as video stream.\n", 
                stream_index, video_index);
        return -1; //TODO change this value for correct error code
    }
    video_index = stream_index;

    if(decoder_ctx->codec_id == AV_CODEC_ID_H264){
        encoder = avcodec_find_encoder_by_name("libx264");
        if (!encoder) {
            av_log(NULL, AV_LOG_FATAL, "Encoder libx264 not found\n");
            return AVERROR_INVALIDDATA;
        }
        fmt::print("Encoder libx264 will be used for stream {}.\n", stream_index);
    } else {
        std::string s = fmt::format("No video encoder found for the given codec_id: {}\n", avcodec_get_name(decoder_ctx->codec_id));
        av_log(NULL, AV_LOG_FATAL, s.c_str());
        return AVERROR_INVALIDDATA;
    }
    
    encoder_ctx = avcodec_alloc_context3(encoder);
    if (!encoder_ctx) {
        av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
        return AVERROR(ENOMEM);
    }

    // from avcut
    encoder_ctx->time_base = decoder_ctx->time_base;
    encoder_ctx->ticks_per_frame = decoder_ctx->ticks_per_frame;
    encoder_ctx->delay = decoder_ctx->delay;
    encoder_ctx->width = decoder_ctx->width;
    encoder_ctx->height = decoder_ctx->height;
    encoder_ctx->pix_fmt = decoder_ctx->pix_fmt;
    encoder_ctx->sample_aspect_ratio = decoder_ctx->sample_aspect_ratio;
    encoder_ctx->color_primaries = decoder_ctx->color_primaries;
    encoder_ctx->color_trc = decoder_ctx->color_trc;
    encoder_ctx->colorspace = decoder_ctx->colorspace;
    encoder_ctx->color_range = decoder_ctx->color_range;
    encoder_ctx->chroma_sample_location = decoder_ctx->chroma_sample_location;
    encoder_ctx->profile = decoder_ctx->profile;
    encoder_ctx->level = decoder_ctx->level;

    encoder_ctx->thread_count = 1; // spawning more threads causes avcodec_close to free threads multiple times
    encoder_ctx->codec_tag = 0;
    
    // correct values ???
    encoder_ctx->qmin = 16;
    encoder_ctx->qmax = 26;
    encoder_ctx->max_qdiff = 4;
    // end from avcut

    // according to avcut, should not be set
    // if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER){
    //     encoder_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    // }

    ret = avcodec_open2(encoder_ctx, encoder, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", stream_index);
        return ret;
    }
    
    codecs[stream_index] = encoder;
    encoders[stream_index] = encoder_ctx;

    return ret;
}

Example

To illustrate my problem, I provide here a test code using the two classes that alternates between transcoding and remuxing at each key-frame encountered in the file using my classes.

To compile the code:

g++ -o trans_remux trans_remux.cpp contexts.cpp -D__STDC_CONSTANT_MACROS `pkg-config --libs libavfilter` -lfmt -g

Currently the code is using avcodec_parameters_copy (contexts.cpp:333), so it works well for remuxing. If you want to test the version with avcodec_parameters_from_context, pls comment from line 333 to 337 in contexts.cpp and uncomment from line 340 to 344 and recompile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文