ffmpeg RTSP 流从摄像机 + Alsa 音频多路复用到 RTMP 目的地?

ffmpeg RTSP 流从摄像机 + Alsa 音频多路复用到 RTMP 目的地?

各位朋友,

我有一台 IP 摄像机,我想获取 RTSP/H264 流并使用 ffmpeg 将其发送到 youtube Live/RTMP 服务。我面临的挑战是使用单独的音频源流式传输视频。

如果我使用来自 IP CAM 输入的音频流(IP 摄像头上有一个 1/8 英寸音频麦克风插孔),我可以很好地流式传输到 Youtube。但我想将其连接到 PA 系统,其中音频源实际上是运行 ffmpeg 的 PC 所在的位置。这就是我遇到的问题。音频比视频快几秒钟。

也许我的命令行语法不正确,这就是同步不成功的原因。

如果我执行 ffprobe,以下是我的可用流:

$ffprobe rtsp://10.0.0.120

ffprobe version 2.8.15 Copyright (c) 2007-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, rtsp, from 'rtsp://10.0.0.120':
  Metadata:
    title           : Media Server
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuvj420p(pc, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 30 fps, 100 tbr, 90k tbn, 60 tbc
    Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp

这是我想要使用线路输入的本地音频卡:

$ ffprobe -f alsa -i hw:0,0
ffprobe version 2.8.15 Copyright (c) 2007-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, alsa, from 'hw:0,0':
  Duration: N/A, start: 1574855984.306477, bitrate: 1536 kb/s
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s

使用 IPCAM 上的内置音频输入(工作正常),我使用以下语法:

ffmpeg -rtsp_transport udp -i "rtsp://10.0.0.120" -tune zerolatency -vcodec libx264 -t 12:00:00 -pix_fmt + -c:v copy -c:a aac -strict experimental -f flv "rtmp://a.rtmp.youtube.com/live2/thekey"

但尝试使用 PC 上的音频输入时,音频比视频提前至少 2 秒。

$ffmpeg -thread_queue_size 6144  -i rtsp://10.0.0.120 -thread_queue_size 6144   -f alsa -ac 1 -i hw:2,0 -c:a:0 aac -ar 44100 -c:v copy -map 0:0 -map 1:0 -strict -2 -f flv rtmp://a.rtmp.youtube.com/live2/thekey

我必须增加 thread_queue_size 的大小,因为我丢帧的情况很严重。

我尝试使用“itsoffset”开关,但似乎没有太大帮助。它在正方向上工作正常,但这只会进一步延迟我的视频。负偏移似乎不起作用:

$ffmpeg -thread_queue_size 6144  -itsoffset -00:00:02  -i rtsp://10.0.0.120 -thread_queue_size 6144   -f alsa -ac 1 -i hw:2,0 -c:a:0 aac -ar 44100 -c:v copy -map 0:0 -map 1:0 -strict -2 -f flv rtmp://a.rtmp.youtube.com/live2/thekey

顺便说一句,当我使用上述语法进行流式传输时,输出如下所示:

ffmpeg version 2.8.15 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 60.100 / 56. 60.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 40.101 /  5. 40.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Input #0, rtsp, from 'rtsp://10.0.0.120:554':
  Metadata:
    title           : Media Server
  Duration: N/A, start: 0.001000, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuvj420p(pc, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 30 fps, 100 tbr, 90k tbn, 60 tbc
    Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp
Guessed Channel Layout for  Input Stream #1.0 : mono
Input #1, alsa, from 'hw:2,0':
  Duration: N/A, start: 1574856329.559323, bitrate: 768 kb/s
    Stream #1:0: Audio: pcm_s16le, 48000 Hz, 1 channels, s16, 768 kb/s
[flv @ 0x10603c0] Codec for stream 0 does not use global headers but container format requires global headers
Output #0, flv, to 'rtmp://a.rtmp.youtube.com/live2/<keyremoved>':
  Metadata:
    title           : Media Server
    encoder         : Lavf56.40.101
    Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuvj420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 30 fps, 100 tbr, 1k tbn, 90k tbc
    Stream #0:1: Audio: aac ([10][0][0][0] / 0x000A), 44100 Hz, mono, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc56.60.100 aac
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help

有什么想法可以调整它以便延迟音频吗?声音和视频质量看起来很棒...只是同步。

谢谢!

—J

相关内容