#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; - Assistant
这个问题是由于输出格式 "rtp_mpegts" 需要严格的时间戳顺序,而源输入文件的时间戳可能没有按严格的顺序排列导致的。这里我们需要对输入的时间戳进行一些处理,以确保它们在写入输出流之前满足 "rtp_mpegts" 的要求。
下面的代码修复了 "non monotonically increasing dts" 的问题。我在您提供的代码的基础上做了一些修改:
在main函数中的 while loop 部分的代码:
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); } - User
- Assistant
对不起给您带来的困扰。我在这里提供新的版本的代码,这个版本应该能更好地解决您遇到的问题:
首先,将 "load_packet" 函数修改为:
char load_packet(AVFormatContext *inputFormatContext, AVPacket *pkt, int64_t *lastPts, int64_t *lastDts) { while (av_read_frame(inputFormatContext, pkt) >= 0) { int64_t pts = pkt->pts == AV_NOPTS_VALUE ? lastPts[pkt->stream_index] : pkt->pts; int64_t dts = pkt->dts == AV_NOPTS_VALUE ? lastDts[pkt->stream_index] : pkt->dts; if (pts >= lastPts[pkt->stream_index] && dts >= lastDts[pkt->stream_index]) { lastPts[pkt->stream_index] = pts; lastDts[pkt->stream_index] = dts; return 1; } av_packet_unref(pkt); } // Seek to the beginning and try reading again av_seek_frame(inputFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD); return load_packet(inputFormatContext, pkt, lastPts, lastDts); }然后,将 "main" 函数中的以下代码:
while (load_packet(inputFormatContext, &packet)) {替换为:
while (load_packet(inputFormatContext, &packet, last_input_pts, last_input_dts)) {