#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); // 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}; int64_t videoLastDts = 0; int64_t audioLastDts = 0;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, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); outputPacket.dts = av_rescale_q_rnd(packet.dts, input_time_base, output_time_base, (AVRounding)(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);}
av_write_trailer(outputFormatContext); avio_closep(&outputFormatContext->pb); // Cleaning up. avformat_free_context(outputFormatContext); avformat_close_input(&inputFormatContext); return 0;}
上面例子,仍然报错:[rtp @ 0x55751e3a4ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 19044870 >= 18994997,请继续修复
- Assistant
为了解决这个问题,我建议在设置输出包的PTS和DTS之前,使用libav的av_rescale_delta函数来确保它们是递增的。
以下是更新后的代码:
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 int64_t pts, dts; pts = av_rescale_delta(input_time_base, packet.pts, output_time_base, pts_intervals[output_stream_index], &lastPts[output_stream_index], output_time_base); dts = av_rescale_delta(input_time_base, packet.dts, output_time_base, pts_intervals[output_stream_index], &lastDts[output_stream_index], output_time_base); // Ensure that the output PTS and DTS are non-decreasing if (dts > *lastDtsForOutputStream) { pts += dts - *lastDtsForOutputStream; dts = *lastDtsForOutputStream; } outputPacket.pts = av_rescale_q_rnd(pts, AVRational{1, 1}, output_time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); outputPacket.dts = av_rescale_q_rnd(dts, AVRational{1, 1}, output_time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); // 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); }这将确保传递给复用器的PTS和DTS是递增的,从而解决了错误。请测试一下这个更新后的代码,看看是否满足您的需求。
- User
有较多报错:[rtp_mpegts @ 0x55e76e481080] Application provided invalid, non monotonically increasing dts to muxer in stream 1: 1706088510000 >= 77677740000 [rtp_mpegts @ 0x55e76e481080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1704936600000 >= 170493660000,请修复
- Assistant
抱歉我之前提供的解决方案没有完全解决问题。我现在发现问题出在视频和音频流的DTS和PTS之间的处理。让我们尝试将音/视频包的处理分开,并为它们提供单独的DTS和PTS计数器。这是更新后的代码:
int64_t last_pts[2] = {0, 0}; int64_t last_dts[2] = {0, 0}; int64_t base_dts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[output_stream_index]->time_base; AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); int64_t dts_no_rescale = packet.dts - base_dts[input_stream_index] + (input_stream_index == videoStreamIndex ? videoLastDts : audioLastDts); // Rescale the PTS and DTS of the output packet int64_t new_pts = av_rescale_q(dts_no_rescale, input_time_base, output_time_base); int64_t new_dts = av_rescale_q(packet.dts, input_time_base, output_time_base); // Check if new_dts is non-decreasing compared to last_dts, otherwise increase it if (new_dts < last_dts[output_stream_index]) { new_dts = last_dts[output_stream_index] + 1; new_pts = std::max(new_pts, new_dts); } outputPacket.pts = new_pts; outputPacket.dts = new_dts; // Save the last_dts value for the output stream last_dts[output_stream_index] = new_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 - last_pts[output_stream_index], output_time_base, {1, 1000000}))); // Save the last_pts value for the output stream last_pts[output_stream_index] = outputPacket.pts; if (input_stream_index == videoStreamIndex) { videoLastDts = packet.dts; } else { audioLastDts = packet.dts; } base_dts[input_stream_index] = packet.dts; } av_packet_unref(&packet); }这个新版本的代码应该更稳定地处理输入和输出的DTS和PTS值。请再次试用这个修复过的版本,看看它是否满足您的需求。如果问题仍然存在,请随时告诉我。
- User
这次修改只有视频的报错:[rtp @ 0x55de835a8ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 19002647 >= 18970496
- Assistant
很抱歉给您带来了不便。我对代码进行了进一步修改。核心思路是在提供给复用器之前,确保音频和视频流中的DTS和PTS是递增的,并相应地进行调整。我通过使用单独的DTS和PTS计数来完成这个任务。
以下是更新后的代码:
int64_t last_pts[2] = {0, 0}; int64_t last_dts[2] = {0, 0}; int64_t base_dts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[output_stream_index]->time_base; AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Calculate DTS and PTS for output packets int64_t new_dts = av_rescale_delta(input_time_base, packet.dts, output_time_base, 1, &base_dts[input_stream_index], output_time_base); new_dts = std::max(new_dts, last_dts[output_stream_index] + 1); last_dts[output_stream_index] = new_dts; int64_t new_pts = av_rescale_delta(input_time_base, packet.pts, output_time_base, 1, &base_dts[input_stream_index], output_time_base); new_pts = std::max(new_pts, new_dts); last_pts[output_stream_index] = new_pts; outputPacket.pts = new_pts; outputPacket.dts = new_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(new_pts - last_pts[0], output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }我希望这次的修改能解决您的问题。请试用这个新版本的代码,并告诉我结果是否令人满意。如果仍然存在问题,请随时告诉我。
- User
播放了10秒左右,有重复了上面的报错:[rtp @ 0x5592a0905ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 18956850 >= 18943922
- Assistant
我再次为您的不便道歉。了解到这个问题很可能是由于输入流中有些异常的时间戳引起的。为了解决这个问题,我们需要确保不仅输出流中的时间戳是递增的,而且还要确保输入流中的时间戳是递增的。
以下是针对这个问题的代码更新:
int64_t last_input_pts[2] = {0, 0}; int64_t last_input_dts[2] = {0, 0}; int64_t last_output_pts[2] = {0, 0}; int64_t last_output_dts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[output_stream_index]->time_base; if (packet.pts < last_input_pts[input_stream_index]) { packet.pts = last_input_pts[input_stream_index]; } if (packet.dts < last_input_dts[input_stream_index]) { packet.dts = last_input_dts[input_stream_index]; } last_input_pts[input_stream_index] = packet.pts; last_input_dts[input_stream_index] = packet.dts; AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); // Calculate DTS and PTS for output packets int64_t new_dts = av_rescale_q(packet.dts, input_time_base, output_time_base); new_dts = std::max(new_dts, last_output_dts[output_stream_index] + 1); last_output_dts[output_stream_index] = new_dts; int64_t new_pts = av_rescale_q(packet.pts, input_time_base, output_time_base); new_pts = std::max(new_pts, new_dts); last_output_pts[output_stream_index] = new_pts; outputPacket.pts = new_pts; outputPacket.dts = new_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(new_pts - last_output_pts[0], output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }在这个更新中,我强制将输入流中的DTS和PTS设置为非递减。然后,通过重新缩放操作,我们确保输出流中的DTS和PTS是递增的。因此,这个方案应该能够解决在RTSP流中发现时间戳异常的问题。
请试用这个修改后的代码,看看它是否可以解决您的问题。如有任何问题,请随时告诉我。
- User
这次马上就出现了报错:[rtp @ 0x55be66fb6ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 18956572 >= 18943761
- Assistant
我对您在实现这个解决方案时所遇到的难题表示遗憾。经过反复的思考,我认为最好的做法是简化代码,为每个包单独处理DTS和PTS值,在以下三点上作出调整:
- 确保在输入端,DTS和PTS都是非递减的。
- 适当处理输入流中的异常时间戳。
- 确保输出流中DTS和PTS依然是非递减的。
以下是根据这些修正后的代码:
int64_t last_input_pts[2] = {0, 0}; int64_t last_input_dts[2] = {0, 0}; int64_t last_output_pts[2] = {0, 0}; int64_t last_output_dts[2] = {0, 0}; while (load_packet(inputFormatContext, &packet)) { int input_stream_index = packet.stream_index; if (input_stream_index == videoStreamIndex || input_stream_index == audioStreamIndex) { int output_stream_index = input_stream_index == videoStreamIndex ? 0 : 1; AVRational input_time_base = inputFormatContext->streams[input_stream_index]->time_base; AVRational output_time_base = outputFormatContext->streams[output_stream_index]->time_base; packet.pts = std::max(packet.pts, last_input_pts[input_stream_index]); packet.dts = std::max(packet.dts, last_input_dts[input_stream_index]); last_input_pts[input_stream_index] = packet.pts; last_input_dts[input_stream_index] = packet.dts; AVPacket outputPacket; av_init_packet(&outputPacket); av_packet_ref(&outputPacket, &packet); int64_t new_pts = av_rescale_q(packet.pts, input_time_base, output_time_base); int64_t new_dts = av_rescale_q(packet.dts, input_time_base, output_time_base); new_pts = std::max(new_pts, last_output_pts[output_stream_index]); new_dts = std::max(new_dts, last_output_dts[output_stream_index]); outputPacket.pts = new_pts; outputPacket.dts = new_dts; last_output_pts[output_stream_index] = new_pts; last_output_dts[output_stream_index] = new_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(new_pts - last_output_pts[0], output_time_base, {1, 1000000}))); } av_packet_unref(&packet); }请试用这个修改后的代码,看看它是否可以解决您的问题。如有任何问题,请随时告诉我。