首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ffmpeg:transcoding.c问题

ffmpeg:transcoding.c问题
EN

Stack Overflow用户
提问于 2015-07-13 12:11:30
回答 1查看 823关注 0票数 2

我正在研究c++中的ffmpeg以查看ffmpeg的示例。

但是我不知道如何在transcoding.c中解决这个问题

这是我的问题

下面是代码

代码语言:javascript
复制
static int open_output_file(const char *filename)
{

    AVCodecContext *dec_ctx, *enc_ctx;
    AVCodec *encoder;
    AVStream *in_stream, *out_stream;
    int ret;
    unsigned int i;
    ofmt_ctx = NULL;

    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);

    if (!ofmt_ctx) {
        av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
        return AVERROR_UNKNOWN;
    }



    for (i = 0; i < ifmt_ctx->nb_streams; i++) {

        in_stream = ifmt_ctx->streams[i];
        out_stream = avformat_new_stream(ofmt_ctx,NULL);


        if (!out_stream) {
            av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
            return AVERROR_UNKNOWN;
        }


            dec_ctx = in_stream->codec;
            enc_ctx = out_stream->codec;


        printf("codec name : %s \n", avcodec_get_name(enc_ctx->codec_id));



        if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
            /* in this example, we choose transcoding to same codec */

            encoder = avcodec_find_encoder(dec_ctx->codec_id);

            if (!encoder) {
                av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
                return AVERROR_INVALIDDATA;
            }

            /* In this example, we transcode to same properties (picture size,
            * sample rate etc.). These properties can be changed for output
            * streams easily using filters */
            if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {

                enc_ctx->height = dec_ctx->height;
                enc_ctx->width = dec_ctx->width;



                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;


                /* take first format from list of supported formats */
                enc_ctx->pix_fmt = encoder->pix_fmts[0];

                /* video time_base can be set to whatever is handy and supported by encoder */
                enc_ctx->time_base = dec_ctx->time_base;
            }

            else {
                enc_ctx->sample_rate = dec_ctx->sample_rate;
                enc_ctx->channel_layout = dec_ctx->channel_layout;
                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);

                /* take first format from list of supported formats */
                enc_ctx->sample_fmt = encoder->sample_fmts[0];
                enc_ctx->time_base.num = 1;
                enc_ctx->time_base.den = enc_ctx->sample_rate;
            }



            /* Third parameter can be used to pass settings to encoder */
            ret = avcodec_open2(enc_ctx, encoder, NULL);

            if (ret < 0) {
                cout << "ret<0" << endl;
                av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
                return ret;
            }
        }
        else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
            av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
            return AVERROR_INVALIDDATA;
        }

        else {
            /* if this stream must be remuxed */
            ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
                ifmt_ctx->streams[i]->codec);
            if (ret < 0) {
                av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
                return ret;
            }
        }

        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;

    }

    av_dump_format(ofmt_ctx, 0, filename, 1);

    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
            return ret;
        }
    }
    /* init muxer, write output file header */
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
        return ret;
    }
    return 0;

}

我在open_output_file函数中发现了这个问题...

但是我不能解决这个问题..

我该如何处理这个问题..

我需要你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2016-11-25 17:47:13

将此添加到if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {之后

代码语言:javascript
复制
enc_ctx->qmin = 3;
enc_ctx->qmax = 30;
enc_ctx->qcompress = 1;

这对我有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31375302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档