libavcodec转码-设置pts和dts

libavcodec转码-设置pts和dts,c,ffmpeg,mp4,libavcodec,C,Ffmpeg,Mp4,Libavcodec,我正在尝试将视频文件转换成标准格式。这是我的主要循环: for(frame_count = 0; av_read_frame(input_ctx, &in_packet) >= 0; frame_count++) { if(in_packet.stream_index == video_stream_index) { decodedPacket = 0; rc = avcodec_decode_video2(video_in_codec

我正在尝试将视频文件转换成标准格式。这是我的主要循环:

for(frame_count = 0; av_read_frame(input_ctx, &in_packet) >= 0; frame_count++) {


    if(in_packet.stream_index == video_stream_index) {

        decodedPacket = 0;
        rc = avcodec_decode_video2(video_in_codec, inputFrame, &decodedPacket, &in_packet);
        if(decodedPacket) {

            out_frames++;


            rc = sws_scale(sws_ctx, inputFrame->data, inputFrame->linesize, 0, video_out_codec->height, outputFrame->data, outputFrame->linesize);
            if(rc != video_out_codec->height) {
                puts("scaling error");
            }

            outputFrame->pts = (1.0 / 30.0) * 90.0 * video_out_codec->frame_number;

            rc = avcodec_encode_video2(video_out_codec, &out_packet, outputFrame, &encodedPacket);
            if(rc != 0) {
                puts("encoding error");
            }

            if(encodedPacket) {

                if (video_out_stream->codec->coded_frame->key_frame) { // deprecated, what to use instead
                    out_packet.flags |= AV_PKT_FLAG_KEY;
                }

                out_packet.stream_index = 0;

                rc = av_interleaved_write_frame(output_ctx, &out_packet);
                if(rc != 0) {
                    puts("frame write error");
                }

                av_free_packet(&out_packet);
                memset(&out_packet, 0, sizeof(AVPacket));

                packet_count++;
            }


            av_free_packet(&in_packet);

        }

    }
...
当我运行这个程序时,我得到了ffprobe报告的tbr/tbn/tbc的奇怪值,视频一团糟

我已尝试根据添加代码,例如

…但随后出现错误,如以下是调试输出:

[mp4 @ 0x1038aba00] Delay between the first packet and last packet in the muxing queue is 10100000 > 10000000: forcing output
我正在正确地设置时间基准(我认为)


关于如何计算正确的输出帧pts和编码数据包dts/pts,有什么想法吗?我显然做错了什么。

几天前,我在使用CLI工具时遇到了(可能)不相关的问题。我使用了
settb
过滤器,但encode包含与从输入视频继承的时间刻度对齐的PTS值。我必须使用所需TB的倒数来获得良好的输出。如果您的TB(1/30)是帧速率(30)的精确倒数,那么帧号
n
的PTS就是
n
,通过查看ffmpeg的功能,我至少找到了部分解决方案。似乎将输出帧dts设置为输入帧编号,即outputFrame->pts=帧计数;这至少创建了一个具有正确持续时间和正确帧速率的视频,尽管我在调试输出“muxing队列中第一个数据包和最后一个数据包之间的延迟为43690667>10000000:强制输出”中遇到了很多这样的错误
[mp4 @ 0x1038aba00] Delay between the first packet and last packet in the muxing queue is 10100000 > 10000000: forcing output
video_out_stream = avformat_new_stream(output_ctx, videoEncoder);
video_out_stream->id = 0;
video_out_stream->time_base.den = 30;
video_out_stream->time_base.num = 1;

video_out_codec = video_out_stream->codec;
avcodec_get_context_defaults3(video_out_codec, videoEncoder);
video_out_codec->codec_id = AV_CODEC_ID_H264;
video_out_codec->bit_rate = 2048;
video_out_codec->width    = 854;
video_out_codec->height   = 480;
video_out_codec->time_base.den = 30;
video_out_codec->time_base.num = 1;
video_out_codec->gop_size = 30;
video_out_codec->pix_fmt = AV_PIX_FMT_YUV420P;