将音频和视频 RTP 数据合并到 mp4 文件中

Merge audio and video RTP data into mp4 file

我正在通过套接字连接接收音频和视频 RTP 数据。现在我想将视频和音频 RTP 数据合并到 MP4 文件中。我怎样才能做到这一点?我是否需要将视频 RTP 分别保存到 h264 和音频 RTP 到 PCMU 中,然后将它们合并到 MP4 文件中?还是可以直接将音视频RTP合并成MP4文件?

提前致谢!

您无需创建单独的音频和视频文件即可将它们混合为 MP4(或任何容器)。但是在您的程序中,您需要从 RTP 数据包中制作 H.264 和 PCMU 帧并将它们传输到 FFMPEG。

总之,您程序的伪代码应该如下所示,

Main {
    //Setup RTP receiver
    //Configure MP4 Muxer of FFMPEG (set input and oputput format), your input is H.264 and PCMU and output is MP4
    //avformat_alloc_output_context2
    //avformat_new_stream

    //Create Thread1 to read audio RTP packets
    //Create Thread2 to read video RTP packets

    //Complete writing MP4 file
    //av_write_trailer
}

Thread1 ()
{
    //Receive audio RTP packets
    //Form an Audio frame

    //Send the frame to muxer av_interleaved_write_frame
}

Thread1 ()
{
    //Receive video RTP packets
    //Form a video frame

    //Send the frame to muxer av_interleaved_write_frame
}

希望这些对您有所帮助。 ffmpeg 源代码中提供的 Muxer 示例会有所帮助。 https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/muxing.c