#include <iostream> #include <thread> #include <chrono> #include <cstdio> extern "C" {…
- User
#include #include #include #include
extern "C" { #include <libavformat/avformat.h> #include <libavutil/opt.h> #include <libavutil/time.h> }
// Load the next packet from the input format context, return whether a packet was loaded // If the end of file is reached, it will seek to the beginning to loop the input char load_packet(AVFormatContext *inputFormatContext, AVPacket *pkt) { if (av_read_frame(inputFormatContext, pkt) >= 0) { return 1; }
// Seek to the beginning and try reading again av_seek_frame(inputFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD); return av_read_frame(inputFormatContext, pkt) >= 0;}
int main(int argc, char *argv[]) { if (argc != 4){ std::cerr << "Usage: " << argv[0] <<"<input.mp4> "<< std::endl; return 1; }
const char *inputFilename = argv[1]; const char *host = argv[2]; int port = std::stoi(argv[3]); av_register_all(); avformat_network_init(); AVFormatContext *inputFormatContext = nullptr; if (avformat_open_input(&inputFormatContext, inputFilename, nullptr, nullptr) < 0){ std::cerr << "Could not open input file: " << inputFilename << std::endl; return 2; } if (avformat_find_stream_info(inputFormatContext, nullptr) < 0){ std::cerr << "Could not find stream info" << std::endl; return 3; } av_dump_format(inputFormatContext, 0, inputFilename, 0); int videoStreamIndex = -1; int audioStreamIndex = -1; for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++) { if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; } else if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audioStreamIndex = i; } } if (videoStreamIndex == -1 || audioStreamIndex == -1){ std::cerr << "Could not find video and/or audio stream" << std::endl; return 4; } // Set up output. AVFormatContext *outputFormatContext = nullptr; if (avformat_alloc_output_context2(&outputFormatContext, nullptr, "rtp_mpegts", nullptr) < 0){ std::cerr << "Could not allocate output context" << std::endl; return 5; } for (int i = 0; i < inputFormatContext->nb_streams; i++){ AVStream *inputStream = inputFormatContext->streams[i]; AVStream *outputStream = avformat_new_stream(outputFormatContext, nullptr); if (!outputStream) { std::cerr << "Could not allocate stream" << std::endl; return 6; } avcodec_parameters_copy(outputStream->codecpar, inputStream->codecpar); } av_dump_format(outputFormatContext, 0, host, 1); AVDictionary *outputOptions = nullptr; av_dict_set_int(&outputOptions, "ttl", 64, 0); std::string outputUri = "rtp://" + std::string(host) + ":" + std::to_string(port); if (avio_open2(&outputFormatContext->pb, outputUri.c_str(), AVIO_FLAG_WRITE, nullptr, &outputOptions) < 0){ std::cerr << "Could not open output" << std::endl; return 7; } if (avformat_write_header(outputFormatContext, nullptr) < 0){ std::cerr <<"Could not write header"<< std::endl; return 8; } // Start streaming. AVPacket packet; av_init_packet(&packet); int64_t lastDts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int64_t *lastDtsForStream = &lastDts[input_stream_index == videoStreamIndex ? 0 : 1]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); av_packet_rescale_ts(&packet, input_time_base, output_time_base); // Make sure the output DTS is monotonically increasing if (packet.dts <= *lastDtsForStream) { packet.dts = *lastDtsForStream + 1; packet.pts = packet.dts; // Assume no B-frames } *lastDtsForStream = packet.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(packet.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); } av_write_trailer(outputFormatContext); avio_closep(&outputFormatContext->pb); // Cleaning up. avformat_free_context(outputFormatContext); avformat_close_input(&inputFormatContext); return 0;}
上面例子: 要求: 1.请计算出h265 25 fps的pts,dts的间隔,我估算是3600,并以此来计算视频的Pts,dts 2.请计算出aac 44.1KHZ的pts,dts的间隔,我估算是1024,并以此来计算视频的Pts,dts
- Assistant
-
对于 h265 编码的 25 fps 视频,我们可以根据帧速率计算 pts 和 dts 之间的间隔。我们知道,帧速率表示的是每秒钟显示的帧数。对于 25 fps 的视频,每秒钟有 25 帧,所以每帧之间的时间间隔是 1/25 = 0.04 秒。为了表示这个间隔,我们需要将其转换为以时间基(time_base)为单位的整数。通常情况下,对于 h265 编码的视频,时间基为 1/90000,所以 pts 和 dts 的间隔为:
interval = 0.04 * 90000 = 3600
所以,您的估算是正确的,h265 25 fps 视频的 pts 和 dts 间隔为 3600。
-
对于 AAC 编码的 44.1 KHz 音频,我们同样需要根据音频的采样率来计算 pts 和 dts 之间的间隔。AAC 音频通常使用 1024 个采样点(samples)作为一帧。对于 44.1 KHz 的音频,每秒钟有 44100 个采样点,所以每帧的时间间隔为:
frame_duration = 1024 / 44100 ≈ 0.02322 秒
要将这个时间间隔转换为整数,我们需要知道音频的时间基。对于 AAC 音频,时间基通常为 1/44100,这意味着 pts 和 dts 的间隔为:
interval = 0.02322 * 44100 = 1024
您的估算也是正确的,AAC 44.1 KHz 音频的 pts 和 dts 间隔为 1024。
-
- User
既然这样,上面例子就用3600,1024来给pts,dts赋值,以此避免Application provided invalid, non monotonically increasing dts to muxer in stream 0的问题,请修改输出
- Assistant
为了根据前面计算出的间隔值为 pts 和 dts 赋值,并避免 "Application provided invalid, non monotonically increasing dts to muxer in stream 0" 的问题,可以通过修改示例中的循环部分来实现。将与 pts 和 dts 相关的部分修改如下:
// Assign the values of 3600 and 1024 to video and audio streams // based on the stream index, to properly calculate pts and dts. int pts_intervals[2] = {3600, 1024}; int64_t lastPts[2] = {0, 0}; int64_t lastDts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForStream = &lastDts[output_stream_index]; int64_t *lastPtsForStream = &lastPts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); av_packet_rescale_ts(&packet, input_time_base, output_time_base); // Assign the calculated pts and dts intervals packet.pts = *lastPtsForStream + pts_intervals[output_stream_index]; packet.dts = *lastDtsForStream + pts_intervals[output_stream_index]; // Make sure the output DTS is monotonically increasing if (packet.dts <= *lastDtsForStream) { packet.dts = *lastDtsForStream + 1; packet.pts = packet.dts; // Assume no B-frames } *lastPtsForStream = packet.pts; *lastDtsForStream = packet.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(packet.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }这个修改根据视频和音频流分别设置了 pts 和 dts 间隔(3600 对于 h265 25 fps 视频流,1024 对于 AAC 44.1 KHz 音频流),确保输出 dts 的单调递增,从而解决了“Application provided invalid, non monotonically increasing dts to muxer in stream 0”的问题。
- User
但还是报错,Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1568 >= 1280,这是为什么
- Assistant
抱歉,我的解释中有错误。每个数据包的 PTS 和 DTS 应该来源于输入数据包的值,以确保正确的音视频同步。问题可能在于输出时间基 (output_time_base) 被用来计算时间间隔,并且可能与实际输入流的时间基不一致。
修改了下 main 函数循环内的相关部分。这次我们先复制 输入数据包的 PTS 和 DTS,然后再根据输入和输出时间基进行重新缩放。
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Copy the input packet's PTS and DTS first outputPacket.pts = packet.pts; outputPacket.dts = packet.dts; // Then rescale them based on the input and output time bases. av_packet_rescale_ts(&outputPacket, input_time_base, output_time_base); // Make sure the output DTS is monotonically increasing if (outputPacket.dts <= *lastDtsForStream) { outputPacket.dts = *lastDtsForStream + 1; outputPacket.pts = outputPacket.dts; // Assume no B-frames } *lastDtsForStream = outputPacket.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }这次修改应该解决“Application provided invalid, non monotonically increasing dts to muxer in stream 0”的问题。
- User
还是报错,Application provided invalid, non monotonically increasing dts to muxer in stream 0: 18970477 >= 18951830
- Assistant
抱歉给您带来不便。在重写时间戳和检查单调递增DTS的顺序上,我犯了一个错误。我已经调整了代码以首先确保单调递增,然后再重写时间戳。
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Ensure monotonically increasing DTS first if (packet.dts <= *lastDtsForStream) { packet.pts = packet.dts = *lastDtsForStream + 1; } // Save the new DTS value *lastDtsForStream = packet.dts; // Copy the input packet's PTS and DTS outputPacket.pts = packet.pts; outputPacket.dts = packet.dts; // Rescale the PTS and DTS based on the input and output time bases av_packet_rescale_ts(&outputPacket, input_time_base, output_time_base); av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }再次调整了代码,这次应该解决了之前的问题。
- User
这是我测试mp4的信息,ffprobe h264-aac-768x320.mp4 ffprobe version 4.4.2 Copyright (c) 2007-2021 the FFmpeg developers built with gcc 11 (Ubuntu 11.2.0-19ubuntu1) configuration: --prefix=/home/zouyl/work/ffmpeg/ffmpeg-4.4.2/build --enable-shared --enable-gpl --enable-nonfree --enable-libopus --enable-libx264 --extra-cflags=-I/home/zouyl/work/ffmpeg/x264/build/include --extra-ldflags=-L/home/zouyl/work/ffmpeg/x264/build/lib --enable-libx265 --enable-libfdk-aac --enable-libvpx --enable-sdl2 --enable-ffplay libavutil 56. 70.100 / 56. 70.100 libavcodec 58.134.100 / 58.134.100 libavformat 58. 76.100 / 58. 76.100 libavdevice 58. 13.100 / 58. 13.100 libavfilter 7.110.100 / 7.110.100 libswscale 5. 9.100 / 5. 9.100 libswresample 3. 9.100 / 3. 9.100 libpostproc 55. 9.100 / 55. 9.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'h264-aac-768x320.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.76.100 Duration: 00:03:30.65, start: 0.000000, bitrate: 248 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 768x320 [SAR 1:1 DAR 12:5], 212 kb/s, 25 fps, 25 tbr, 16k tbn, 50 tbc (default) Metadata: handler_name : VideoHandler vendor_id : [0][0][0][0] Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 30 kb/s (default) Metadata: handler_name : SoundHandler vendor_id : [0][0][0][0] zouyl@zouyl-VirtualBox:~/work/20230603$ 上面的例子还是报错,请继续修复
- Assistant
根据您提供的输入文件信息,我注意到视频流的时间基 (tbn) 是 16k。我会在这个基础上继续调整代码以解决问题。
首先,我们需要从输入流提取时间基。然后,更新循环内检查单调递增 DTS 部分,基于输入时间基进行比较。最后,在调整输出 PTS 和 DTS 之前,确保输入 PTS 和 DTS 的单调递增。这里是更新后的代码:
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; constexpr int video_output_stream_index = 0; constexpr int audio_output_stream_index = 1; int output_stream_index = input_stream_index == videoStreamIndex ? video_output_stream_index : audio_output_stream_index; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { // Ensure monotonically increasing DTS for the input packet AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; int64_t current_dts = packet.dts; int64_t last_dts = av_rescale_q(lastDts[output_stream_index], outputFormatContext->streams[output_stream_index]->time_base, input_time_base); if (current_dts <= last_dts) { packet.pts = packet.dts = last_dts + 1; } lastDts[output_stream_index] = av_rescale_q(packet.dts, input_time_base, outputFormatContext->streams[output_stream_index]->time_base); // Then copy and rescale the input packet's PTS and DTS using input and output time bases AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); outputPacket.pts = packet.pts; outputPacket.dts = packet.dts; av_packet_rescale_ts(&outputPacket, input_time_base, outputFormatContext->streams[output_stream_index]->time_base); av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, outputFormatContext->streams[output_stream_index]->time_base, {1, 1000000}))); } av_packet_unref(&packet); }这次调整应该解决了之前的问题。
- User
还是没解决哦,Application provided invalid, non monotonically increasing dts to muxer in stream 0: 19089158 >= 19047455一直都是这个报错
- Assistant
我明白了。抱歉之前的解决方案仍然没有解决问题。这里提供一个新的解决方案,我们根据输入流的时间基先调整时间戳,然后确保输出 DTS 的单调递增。
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForOutputStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Adjust input PTS and DTS first int64_t dts_diff = packet.pts - packet.dts; if (packet.dts < *lastDtsForOutputStream) { packet.dts = *lastDtsForOutputStream + 1; packet.pts = packet.dts + dts_diff; } // Copy and rescale the adjusted input packet's PTS and DTS outputPacket.pts = packet.pts; outputPacket.dts = packet.dts; av_packet_rescale_ts(&outputPacket, input_time_base, output_time_base); // Save the output DTS value *lastDtsForOutputStream = outputPacket.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }这次调整后,应该解决了问题。
- User
这次改动是有效果,多出了更多的错误:Application provided invalid, non monotonically increasing dts to muxer in stream 1: 9194429384278975041 >= 1937438424010622;[rtp_mpegts @ 0x563073986080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 9178680874633687491 >= 17135573038369,数值变得更大更异常
- Assistant
我们再尝试一种方法来确保 DTS 是单调递增的。这次将时间戳调整限制在数据包的重写过程中。在与输入相关的部分,我们会先修改 PTS,然后再修改 DTS;在输出相关的部分,首先会重写 PTS,然后计算新的 PTS 和 DTS。请注意这种方法可能需要在不同的输入中进行测试,以确保时间戳正确。
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForOutputStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { // Modify input PTS first if (packet.pts < packet.dts) { packet.pts = packet.dts; } // Modify input DTS if (input_stream_index == videoStreamIndex) { if (packet.dts < videoLastDts) { packet.dts = videoLastDts; } videoLastDts = packet.dts; } else if (input_stream_index == audioStreamIndex) { if (packet.dts < audioLastDts) { packet.dts = audioLastDts; } audioLastDts = packet.dts; } AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Rewrite PTS and calculate new PTS and DTS outputPacket.pts = av_rescale_q_rnd(packet.pts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); outputPacket.dts = av_rescale_q_rnd(packet.dts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); if (outputPacket.dts >= *lastDtsForOutputStream) { // Save the output DTS value *lastDtsForOutputStream = outputPacket.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } else { std::cerr << "Skipping packet" << std::endl; } } av_packet_unref(&packet); }希望这次的更改能解决问题。请进行测试并告知问题是否已得到解决。
- User
这次的报错数值是变小了,但还是原来的报错:pplication provided invalid, non monotonically increasing dts to muxer in stream 0: 18943740 >= 18943740;Application provided invalid, non monotonically increasing dts to muxer in stream 1: 18956539 >= 18956539
- Assistant
非常抱歉,我们再尝试一种方法来确保 DTS 的递增。这次我们将在输出过程的前一步尝试修复 DTS。如果新计算的 DTS 不递增,我们将手动递增上一个 DTS 值。
while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForOutputStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Rewrite PTS and calculate new PTS and DTS outputPacket.pts = av_rescale_q_rnd(packet.pts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); outputPacket.dts = av_rescale_q_rnd(packet.dts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); // Fix DTS increment if required if (outputPacket.dts <= *lastDtsForOutputStream) { outputPacket.dts = *lastDtsForOutputStream + 1; outputPacket.pts = std::max(outputPacket.pts, outputPacket.dts); } // Save the output DTS value *lastDtsForOutputStream = outputPacket.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }希望这次的更改能解决问题。请再次测试并告知问题是否得到解决。
- User
这次没音频pts的报错了,只有视频pts的报错,[rtp @ 0x56504628aac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 19004697 >= 18971686
- Assistant
根据您提供的反馈,音频流问题已经解决,只剩下视频流的问题。我们将针对视频流进行特殊处理。首先,我们将跟踪上一个输入视频包的 PTS 和 DTS,然后检查当前包与上一个包之间的关系。根据这些值,我们将调整当前包的 PTS 和 DTS,以确保它们在输出中递增。
下面是更新的代码:
int64_t lastInputVideoPts = 0; int64_t lastInputVideoDts = 0; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; int64_t *lastDtsForOutputStream = &lastDts[output_stream_index]; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[input_stream_index]->time_base; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { if (input_stream_index == videoStreamIndex) { // Adjust input video packet PTS and DTS if (packet.pts < packet.dts) { packet.pts = packet.dts; } if (packet.pts < lastInputVideoPts) { packet.pts = lastInputVideoPts; } if (packet.dts < lastInputVideoDts) { packet.dts = lastInputVideoDts; } lastInputVideoPts = packet.pts; lastInputVideoDts = packet.dts; } AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Rewrite PTS and calculate new PTS and DTS outputPacket.pts = av_rescale_q_rnd(packet.pts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); outputPacket.dts = av_rescale_q_rnd(packet.dts, input_time_base, output_time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX); // Fix DTS increment if required if (outputPacket.dts <= *lastDtsForOutputStream) { outputPacket.dts = *lastDtsForOutputStream + 1; outputPacket.pts = std::max(outputPacket.pts, outputPacket.dts); } // Save the output DTS value *lastDtsForOutputStream = outputPacket.dts; av_write_frame(outputFormatContext, &outputPacket); av_packet_unref(&outputPacket); // Sleep for synchronization std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>( av_rescale_q(outputPacket.pts, output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }通过对视频流进行特殊处理,希望这次的更改能够解决问题。请再次测试并告知问题是否得到解决。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.