RTP 流与 FFmpeg 音频和视频不同步

RTP streaming with FFmpeg audio and video out of sync

我正在使用以下命令流式传输 webcam/audio:

ffmpeg.exe -f dshow -framerate 30 -i video="xxx" -c:v libx264 -an -f rtp rtp://localhost:50041 -f dshow -i audio="xxx" -c:a aac -vn -f rtp rtp://localhost:50043

这将输出以下 sdp 信息:

    v=0
    o=- 0 0 IN IP4 127.0.0.1
    s=No Name
    t=0 0
    a=tool:libavformat 57.65.100
    m=video 50041 RTP/AVP 96
    c=IN IP6 ::1
    a=rtpmap:96 H264/90000
    a=fmtp:96 packetization-mode=1
    m=audio 50043 RTP/AVP 97
    c=IN IP6 ::1
    b=AS:128
    a=rtpmap:97 MPEG4-GENERIC/44100/2
    a=fmtp:97 profile-level-id=1;mode=AAC-
    hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=121056E500

然后我使用以下命令读取流:

ffmpeg.exe -protocol_whitelist file,udp,rtp -i D:\test.sdp -c:v libx264 -c:a aac d:\out.mp4

在生成的文件中,音频稍早于视频。我读到 RTCP 在 RTP 端口 + 1 上运行,并包含同步信息。不过,我在 SDP 文件中看不到任何 RTCP 信息。

我需要指定一些内容来包含 RTCP 吗?

如果这不是问题,我还能做些什么来同步音频和视频?

不确定 RTCP 是否是您的问题,但我会首先尝试使用一个 directshow 输入并将其拆分为两个输出,如下所示:

ffmpeg.exe -f dshow -framerate 30 -i video="XX":audio="YY" -an -vcodec libx264 -f rtp rtp://localhost:50041 -acodec aac -vn -f rtp rtp://localhost:50043

ffmpeg DirectShow documentation提到了使用多个输入时的同步问题。它还提到如果您想将输入分开,请尝试使用“-copy_ts”标志来解决同步问题。