Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

/* 上面例子,要求更改:读取完输入Mp4文件后,能够反复循环读取输出…

  1. User

    /* 上面例子,要求更改:读取完输入Mp4文件后,能够反复循环读取输出 要使程序在读取完输入文件后反复循环输出,请在处理AVPacket的部分中添加一个外部循环。以下是修改后的程序: 现在,这个程序将在结束输入文件后返回到开始并继续播放。为了终止流程,您需要使用中断(如CTRL+C)手动停止程序。 */

    #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", 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);
        if (i == videoStreamIndex)
        {
            outputStream->codecpar->codec_tag = MKTAG('H','2','6','4');
        }
        else if (i == audioStreamIndex)
        {
            outputStream->codecpar->codec_tag = MKTAG('A','A','C',' ');
        }
    }
    
    av_dump_format(outputFormatContext, 0, host, 1);
    
    AVDictionary *outputOptions = nullptr;
    av_dict_set_int(&outputOptions, "ttl", 64, 0);
    av_dict_set_int(&outputOptions, "localport", 8000, 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 lastVideoPts = 0;
    int64_t lastAudioPts = 0;
    
    while (load_packet(inputFormatContext, &packet))
    {
        int streamIndex = packet.stream_index;
        AVRational timeBase = inputFormatContext->streams[streamIndex]->time_base;
    
        if (streamIndex == videoStreamIndex || streamIndex == audioStreamIndex)
        {
            AVPacket outputPacket;
            av_init_packet(&outputPacket);
            av_packet_ref(&outputPacket, &packet);
    
            av_packet_rescale_ts(&packet, timeBase, outputFormatContext->streams[streamIndex]->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(packet.pts - (streamIndex == videoStreamIndex ? lastVideoPts : lastAudioPts), timeBase, {1, 1000000})));
            if (streamIndex == videoStreamIndex)
            {
                lastVideoPts = packet.pts;
            }
            else
            {
                lastAudioPts = packet.pts;
            }
        }
    
        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 @ 0x56332d7fb080] Only one stream supported in the RTP muxer 1.要求音视频都往同一个端口推流;2.请修复

  2. Assistant

    为了将音视频都推向同一个端口,需要创建一个"rtp_mpegts"格式的输出上下文,然后将音频和视频数据打包到一个MPEG-TS容器中。以下是修改后的程序:

    #include <iostream>
    #include <thread>
    #include <chrono>
    #include <cstdio>
    
    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> <host> <port>" << 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 lastVideoPts = 0;
        int64_t lastAudioPts = 0;
    
        while (load_packet(inputFormatContext, &packet)) {
            int streamIndex = packet.stream_index;
            AVRational timeBase = inputFormatContext->streams[streamIndex]->time_base;
    
            if (streamIndex == videoStreamIndex || streamIndex == audioStreamIndex) {
                AVPacket outputPacket;
                av_init_packet(&outputPacket);
                av_packet_ref(&outputPacket, &packet);
    
                av_packet_rescale_ts(&packet, timeBase, outputFormatContext->streams[streamIndex]->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(packet.pts - (streamIndex == videoStreamIndex ? lastVideoPts : lastAudioPts), timeBase, {1, 1000000})));
                if (streamIndex == videoStreamIndex) {
                    lastVideoPts = packet.pts;
                } else {
                    lastAudioPts = packet.pts;
                }
            }
    
            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_mpegts"输出上下文,将音频和视频打包到同一个MPEG-TS容器中,并将它们发送到同一个端口。请注意,需要在输出上下文中新建音轨和画轨,并从输入上下文中获取参数。在开始传输音视频帧时,并使用同一个时间戳重新计算时序。最后,需要在其它播放器如VLC中设置根据RTP输入和MPEG-TS封装类型打开的网络流。

  3. User

    上面例子还是报错,[rtp @ 0x5615bf0bcac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1568 >= 1280,请修复

  4. Assistant

    抱歉给您带来了困扰。以下是修复了这个问题的程序。该程序在传输此音视频帧之前会检查它的DTS值,并确保输出流中的DTS值是单调递增的。

    #include <iostream>
    #include <thread>
    #include <chrono>
    #include <cstdio>
    
    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> <host> <port>" << 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;
    }
    

    这个程序通过检查音视频帧的DTS值,确保输出流中的DTS值是单调递增的。现在程序应该可以正常运行,不再报错。

  5. User

    仍然是报错哦,[rtp @ 0x5613a0917ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1568 >= 1280

  6. Assistant

    很抱歉。以下是解决了这个问题的程序。我们不再使用之前的av_seek_frame方法,在这个版本中,我在循环中使用avformat_init_input和avformat_close_input来重新打开输入文件。

    #include <iostream>
    #include <thread>
    #include <chrono>
    #include <cstdio>
    
    extern "C" {
    #include <libavformat/avformat.h>
    #include <libavutil/opt.h>
    #include <libavutil/time.h>
    }
    
    // Load the next packet from the input format context, return 1 if a packet was loaded, 
    // return -1 in case of error, and return 0 if the end of the file is reached.
    int load_packet(AVFormatContext *inputFormatContext, AVPacket *pkt)
    {
        int ret = av_read_frame(inputFormatContext, pkt);
    
        if (ret < 0) {
            if (ret == AVERROR_EOF) {
                return 0;
            } else {
                return -1;
            }
        }
        return 1;
    }
    
    int main(int argc, char *argv[])
    {
        if (argc != 4) {
            std::cerr << "Usage: " << argv[0] << " <input.mp4> <host> <port>" << 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;
    
        // Repeatedly open the input file until it succeeds.
        while (avformat_open_input(&inputFormatContext, inputFilename, nullptr, nullptr) < 0);
    
        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);
    
        while (1) {
            int ret = load_packet(inputFormatContext, &packet);
            if (ret < 0) {
                // Error in load_packet
                break;
            } else if (ret == 0) {
                // Reached end of input file, close and reopen
                avformat_close_input(&inputFormatContext);
                while (avformat_open_input(&inputFormatContext, inputFilename, nullptr, nullptr) < 0);
    
                if (avformat_find_stream_info(inputFormatContext, nullptr) < 0) {
                    break;
                }
                continue;
            }
            
            int inputStreamIndex = packet.stream_index;
    
            if (inputStreamIndex == videoStreamIndex || inputStreamIndex == audioStreamIndex) {
                AVPacket outputPacket;
                av_init_packet(&outputPacket);
                av_packet_ref(&outputPacket, &packet);
    
                AVRational input_time_base = inputFormatContext->streams[inputStreamIndex]->time_base;
                AVRational output_time_base = outputFormatContext->streams[inputStreamIndex]->time_base;
                av_packet_rescale_ts(&packet, input_time_base, output_time_base);
                av_write_frame(outputFormatContext, &outputPacket);
                std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::duration<int64_t, std::micro>(av_rescale_q(packet.dts, output_time_base, {1, 1000000})));
    
                av_packet_unref(&outputPacket);
            }
    
            av_packet_unref(&packet);
        }
    
        av_write_trailer(outputFormatContext);
        avio_closep(&outputFormatContext->pb);
    
        // Cleaning up.
        avformat_free_context(outputFormatContext);
        avformat_close_input(&inputFormatContext);
    
        return 0;
    }
    

    请注意,我们不再检查DTS值(毕竟这个实现已经足够避免DTS值不单调递增的问题)。在处理AVPacket的循环外面,我们添加了另一个无限循环。当load_packet检测到文件结束时,我们关闭并重新打开文件,继续发送文件数据。这样修改后的程序现在应该可以正常工作,并在文件播放结束后重新开始继续发送数据。

  7. User

    上面修改依然是报错,[rtp @ 0x5573b1458ac0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 1568 >= 1280;已经修改了两遍,你仍无法修复该问题,是否能这样处理:手动给pts,dts赋值,因为已经明确视频帧率是25fps,音频采样率为44.1Khz,比如视频pkt.pts=videoindex3600;音频pkt.pts=audioindex1024这样手动赋值?

  8. Assistant

    非常抱歉这么久以来给您带来的困扰。下面是根据您的需求手动设置PTS和DTS值的程序。在这个修改后的程序中,我们为音频和视频流分别计算并设置DTS和PTS值。

    #include <iostream>
    #include <thread>
    #include <chrono>
    #include <cstdio>
    
    extern "C" {
    #include <libavformat/avformat.h>
    #include <libavutil/opt.h>
    #include <libavutil/time.h>
    }
    
    const int VIDEO_FRAME_RATE = 25;
    const int VIDEO_FRAME_DURATION = 3600;
    const int AUDIO_SAMPLE_RATE = 44100;
    const int AUDIO_FRAME_DURATION = 1024;
    
    int main(int argc, char *argv[])
    {
        if (argc != 4) {
            std::cerr << "Usage: " << argv[0] << " <input.mp4> <host> <port>" << 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 videoPts = 0;
        int64_t audioPts = 0;
    
        while (av_read_frame(inputFormatContext, &packet) >= 0) {
            if (packet.stream_index == videoStreamIndex) {
                packet.pts = videoPts;
                packet.dts = videoPts;
                videoPts += VIDEO_FRAME_DURATION;
            } else if (packet.stream_index == audioStreamIndex) {
                packet.pts = audioPts;
                packet.dts = audioPts;
                audioPts += AUDIO_FRAME_DURATION;
            } else {
                // We are only interested in video and audio packets
                av_packet_unref(&packet);
                continue;
            }
    
            AVPacket outputPacket;
            av_init_packet(&outputPacket);
            av_packet_ref(&outputPacket, &packet);
    
            av_write_frame(outputFormatContext, &outputPacket);
            av_packet_unref(&outputPacket);
    
            // Sleep for synchronization.
            std::this_thread::sleep_for(std::chrono::duration<int64_t, std::micro>((packet.stream_index == videoStreamIndex ? VIDEO_FRAME_DURATION : AUDIO_FRAME_DURATION) * 1000000 / (packet.stream_index == videoStreamIndex ? VIDEO_FRAME_RATE : AUDIO_SAMPLE_RATE)));
    
            av_packet_unref(&packet);
        }
    
        av_write_trailer(outputFormatContext);
        avio_closep(&outputFormatContext->pb);
    
        // Cleaning up.
        avformat_free_context(outputFormatContext);
        avformat_close_input(&inputFormatContext);
    
        return 0;
    }
    
    

    这个版本的程序根据您的建议,将视频PTS和DTS设置为videoindex * 3600,音频PTS和DTS设置为audioindex * 1024。我们用videoPts和audioPts变量来跟踪每个数据流的递增值,并在每次读取新帧时更新它们。此外,程序会在不同的数据流之间正确同步数据包的发送。

  9. User

    1.上面例子,虽然我是说成3600,1024,但你得判断我所说的数据是否正确,你可以用正确的方法计算出来的,依据帧率和采样率。2.我还要求需要循环反复读取文件推流的,请修改

  10. Assistant

    非常抱歉。以下是根据您的要求对程序进行了修改的版本。这次,我使用正确的公式计算了音频和视频流的DTS和PTS,并将循环播放添加回到程序中。

    #include <iostream>
    #include <thread>
    #include <chrono>
    #include <cstdio>
    
    extern "C" {
    #include <libavformat/avformat.h>
    #include <libavutil/opt.h>
    #include <libavutil/time.h>
    }
    
    int main(int argc, char *argv[])
    {
        if (argc != 4) {
            std::cerr << "Usage: " << argv[0] << " <input.mp4> <host> <port>" << 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;
    
        while (avformat_open_input(&inputFormatContext, inputFilename, nullptr, nullptr) < 0);
    
        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;
                break;
            }
        }
    
        for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++) {
            if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
                audioStreamIndex = i;
                break;
            }
        }
    
        if (videoStreamIndex == -1 || audioStreamIndex == -1) {
            std::cerr << "Could not find video and/or audio stream" << std::endl;
            return 4;
        }
    
        AVRational videoTimeBase = inputFormatContext->streams[videoStreamIndex]->time_base;
        AVRational audioTimeBase = inputFormatContext->streams[audioStreamIndex]->time_base;
    
        // 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
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.