<div dir="ltr">Hello, <br><div>I want to transcode video with exactly the same properties of the input video in the output video.</div><div>I use the transcoding.cc code. In the static int open_output_file(const char *filename) function, I set  encoder parameters like this : </div><div><br></div><div>if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)<br>                        {<br>                               <span style="background-color:rgb(0,255,0)"> enc_ctx->height = dec_ctx->height;<br>                                enc_ctx->width = dec_ctx->width;<br>                                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;<br>                                enc_ctx->global_quality=dec_ctx->global_quality;<br>                                enc_ctx->gop_size=dec_ctx->gop_size;<br>                                enc_ctx->bit_rate=dec_ctx->bit_rate;<br>                                enc_ctx->time_base=dec_ctx->time_base;<br>                                enc_ctx->delay=dec_ctx->delay;<br>                                //**********************************************        <br>                                enc_ctx->rc_max_rate=dec_ctx->rc_max_rate;<br>                                enc_ctx->rc_min_rate=dec_ctx->rc_min_rate;</span><br>                                /* take first format from list of supported formats */<br>                                if (encoder->pix_fmts)<br>                                {<br>                                      <span style="background-color:rgb(0,255,0)">  enc_ctx->pix_fmt = encoder->pix_fmts[0];</span><br>                                }<br>                                else<br>                                {<br>                                        <span style="background-color:rgb(0,255,0)">enc_ctx->pix_fmt = dec_ctx->pix_fmt;</span><br>                                }<br>                                // video time_base can be set to whatever is handy and supported by encoder <br>                                // enc_ctx->time_base = av_inv_q(dec_ctx->framerate);<br>                        }<br>                        else<br>                        {<br>                               <span style="background-color:rgb(0,255,0)"> enc_ctx->sample_rate = dec_ctx->sample_rate;<br>                                enc_ctx->channel_layout = dec_ctx->channel_layout;<br>                                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);<br>                                /* take first format from list of supported formats */<br>                                enc_ctx->sample_fmt = encoder->sample_fmts[0];<br>                                enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};</span><br>                        }<br>                        /* Third parameter can be used to pass settings to encoder */<br>                        ret = avcodec_open2(enc_ctx, encoder, NULL);<br>                        if (ret < 0)<br>                        {<br>                                av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);<br>                                return ret;<br>                        }<br>                        ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);<br>                        if (ret < 0)<br>                        {<br>                                av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);<br>                                return ret;<br>                        }<br>                        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)<br>                                enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;<br><br>                       <span style="background-color:rgb(255,255,255)"> out_stream->time_base = enc_ctx->time_base;<br></span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div><span style="background-color:rgb(255,255,255)">But when I run exiftool on output  video and on input video, some values like video duration, encoder, bit rate,  are differente. </span></div><div><span style="background-color:rgb(255,255,255)">Can someone tells me how can I do to keep the same parameters (metada) of the input video in the output video, please?</span></div><div><span style="background-color:rgb(255,255,255)"><br></span></div><div>The entire open_output_file function is the following :</div><div><br></div><div>static int open_output_file(const char *filename)<br>{<br>         AVStream *out_stream;<br>        AVStream *in_stream;<br>        AVCodecContext *dec_ctx, *enc_ctx;<br>        AVCodec *encoder;<br>        int ret;<br>        unsigned int i;<br>        ofmt_ctx = NULL;<br>        avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);<br>        if (!ofmt_ctx)<br>        {<br>                av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");<br>                return AVERROR_UNKNOWN;<br>        }<br><br>        for (i = 0; i < ifmt_ctx->nb_streams; i++)<br>        {<br>                out_stream = avformat_new_stream(ofmt_ctx, NULL);<br>                if (!out_stream)<br>                {<br>                        av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");<br>                        return AVERROR_UNKNOWN;<br>                }<br><br>                in_stream = ifmt_ctx->streams[i];<br>                dec_ctx = stream_ctx[i].dec_ctx;<br><br>                if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)<br>                {<br>                        //AVCodecID codec_id = dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_CODEC_ID_MPEG4:dec_ctx->codec_id;<br>                        /* in this example, we choose transcoding to same codec */<br>                        //encoder = dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ? avcodec_find_encoder(AV_CODEC_ID_H264):avcodec_find_encoder(dec_ctx->codec_id);//(AV_CODEC_ID_MPEG4):avcodec_find_encoder(dec_ctx->codec_id);<br>                        encoder = dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ? avcodec_find_encoder(AV_CODEC_ID_MPEG4):avcodec_find_encoder(dec_ctx->codec_id);<br>                        // if(dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){encoder=avcodec_find_encoder(AV_CODEC_ID_MPEG4);}else{encoder=avcodec_find_encoder(dec_ctx->codec_id);}<br>                        //encoder = avcodec_find_encoder(dec_ctx->codec_id);<br>                        if (!encoder)<br>                        {<br>                                av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");<br>                                return AVERROR_INVALIDDATA;<br>                        }<br>                        enc_ctx = avcodec_alloc_context3(encoder);<br>                        if (!enc_ctx)<br>                        {<br>                                av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");<br>                                return AVERROR(ENOMEM);<br></div><div> }<br><br>                        /* In this example, we transcode to same properties (picture size,<br>                        * sample rate etc.). These properties can be changed for output<br>                        * streams easily using filters */<br>                        if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)<br>                        {<br>                                enc_ctx->height = dec_ctx->height;<br>                                enc_ctx->width = dec_ctx->width;<br>                                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;<br>                                enc_ctx->global_quality=dec_ctx->global_quality;<br>                                enc_ctx->gop_size=dec_ctx->gop_size;<br>                                enc_ctx->bit_rate=dec_ctx->bit_rate;<br>                                enc_ctx->time_base=dec_ctx->time_base;<br>                                enc_ctx->delay=dec_ctx->delay;<br>                                //**********************************************        <br>                                enc_ctx->rc_max_rate=dec_ctx->rc_max_rate;<br>                                enc_ctx->rc_min_rate=dec_ctx->rc_min_rate;<br>                                /* take first format from list of supported formats */<br>                                if (encoder->pix_fmts)<br>                                {<br>                                        enc_ctx->pix_fmt = encoder->pix_fmts[0];<br>                                }<br>                                else<br>                                {<br>                                        enc_ctx->pix_fmt = dec_ctx->pix_fmt;<br>                                }<br>                                // video time_base can be set to whatever is handy and supported by encoder <br>                                // enc_ctx->time_base = av_inv_q(dec_ctx->framerate);<br>                        }<br>                        else<br>                        {<br>                                enc_ctx->sample_rate = dec_ctx->sample_rate;<br>                                enc_ctx->channel_layout = dec_ctx->channel_layout;<br>                                enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);<br>                                /* take first format from list of supported formats */<br>                                enc_ctx->sample_fmt = encoder->sample_fmts[0];<br>                                enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};<br>                        }<br>                        /* Third parameter can be used to pass settings to encoder */<br>                        ret = avcodec_open2(enc_ctx, encoder, NULL);<br>                        if (ret < 0)<br>                        {<br>                                av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);<br>                                return ret;<br>                        }<br>                        ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);<br>                        if (ret < 0)<br>                        {<br>                                av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);<br>                                return ret;<br>                        }<br>                        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)<br></div><div> enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;<br><br>                        out_stream->time_base = enc_ctx->time_base;<br>                        stream_ctx[i].enc_ctx = enc_ctx;<br>                }<br>                else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN)<br>                {<br>                        av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);<br>                        return AVERROR_INVALIDDATA;<br>                }<br>                else<br>                {<br>                        // if this stream must be remuxed <br>                        ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);<br>                        //ifmt_ctx->streams[i]->codec);<br>                        if (ret < 0)<br>                        {<br>                                av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);<br>                                return ret;<br>                        }<br>                        out_stream->time_base = in_stream->time_base;<br>                }<br><br>        }<br>        av_dump_format(ofmt_ctx, 0, filename, 1);<br><br>        if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE))<br>        {<br>                ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);<br>                if (ret < 0)<br>                {<br>                        av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);<br>                        return ret;<br>                }<br>        }<br><br>        /* init muxer, write output file header */<br>        ret = avformat_write_header(ofmt_ctx, NULL);<br>        if (ret < 0)<br>        {<br>                av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");<br>                return ret;<br>        }<br>        return 0;<br>}<br></div><div><span style="background-color:rgb(0,255,0)"><br></span></div><div><span style="background-color:rgb(0,255,0)">Rgards</span></div></div>