使用 ffmpeg 将摄像头信号(重新)传输到 RTMP 服务器

使用 ffmpeg 将摄像头信号(重新)传输到 RTMP 服务器

情况是这样的,我有多个便宜的中国 WiFi 摄像头,想进行直播。

相机有一个 Web 界面,但(据我所知)缺少 RTSP 流。相机输出 MJPEG 编码流http://admin:@[camip]/media/?action=stream,并使用 VLC 成功连接。

我可以使用 OBS 添加 VLC 源,然后使用 OBS 直接流式传输到 Youtube,这可以正常工作但并不理想,因为我想将多个摄像机流式传输到它们自己的流中。

使用 OBS,我有以下流媒体设置,一切正常

Video bitrate: 750 Kbps
Encoder: Software (x264) 
Audio bitrate: 160

我能够使用 ffmpeg 连接到流,它显示输入流

Input #0, mjpeg, from 'http://admin:@192.168.0.65/media/?action=stream';
  Duration: N/A, bitrate: N/A
    Stream #0:0 Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 1200k tbn, 25 tbc 

现在我的下一步是获取该输入流并将其重新流式传输到 Youtube Live(或者实际上是私有的 YouPHPTube 服务器,但配置相同)。

我尝试了很多方法,但总是遇到奇怪的问题,例如输出速度真的很慢(或者至少我认为这是指示的,并且 Youtube 不会接收流)......

ffmpeg \
    -f mjpeg \
    -i "http://admin:@192.168.0.65/media/?action=stream" \
    -f h264 "rtmp://a.rtmp.youtube.com/live2/[streamkey]"
Output #0, h264, to 'rtmp://a.rtmp.youtube.com/live2/[streamkey]':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: mpeg2video (Main), yuv420p, 720x480 [SAR 32:27 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.107.100 mpeg2video
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame=  577 fps=4.2 q=24.8 Lsize=     994kB time=00:00:23.04 bitrate= 353.6kbits/s speed=0.167x  

有人能帮帮我,告诉我我做错了什么,并建议我如何让 ffmpeg 使用与我的 OBS 设置相同的输出设置重新流式传输,或者任何可行的方法吗?

提前致谢!

编辑:添加了完整日志

ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mjpeg, from 'http://admin:@192.168.0.65/media/?action=stream':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 1200k tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x55e716cbe200] using SAR=1/1
[libx264 @ 0x55e716cbe200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55e716cbe200] profile High, level 3.1
Output #0, h264, to 'rtmp://xxx/xxx/xxx':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: h264 (libx264), yuvj420p(pc), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=   80 fps=4.2 q=-1.0 Lsize=     673kB time=00:00:03.12 bitrate=1766.8kbits/s speed=0.163x    


video:673kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000000%
[libx264 @ 0x55e716cbe200] frame I:1     Avg QP:24.70  size: 70939
[libx264 @ 0x55e716cbe200] frame P:20    Avg QP:24.70  size: 21910
[libx264 @ 0x55e716cbe200] frame B:59    Avg QP:26.67  size:  3049
[libx264 @ 0x55e716cbe200] consecutive B-frames:  1.2%  0.0%  3.8% 95.0%
[libx264 @ 0x55e716cbe200] mb I  I16..4:  1.9% 93.4%  4.6%
[libx264 @ 0x55e716cbe200] mb P  I16..4:  0.0%  1.7%  0.0%  P16..4: 51.7% 15.9% 12.4%  0.0%  0.0%    skip:18.4%
[libx264 @ 0x55e716cbe200] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8: 36.6%  0.6%  0.0%  direct: 0.5%  skip:62.3%  L0:51.5% L1:47.8% BI: 0.7%
[libx264 @ 0x55e716cbe200] 8x8 transform intra:95.1% inter:82.3%
[libx264 @ 0x55e716cbe200] coded y,uvDC,uvAC intra: 92.8% 72.8% 26.8% inter: 14.3% 18.8% 0.2%
[libx264 @ 0x55e716cbe200] i16 v,h,dc,p: 11%  4%  0% 85%
[libx264 @ 0x55e716cbe200] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15% 11% 18%  6%  9% 15%  7%  6% 12%
[libx264 @ 0x55e716cbe200] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 10%  9%  7% 12% 18%  7%  6%  7%
[libx264 @ 0x55e716cbe200] i8c dc,h,v,p: 54% 19% 21%  6%
[libx264 @ 0x55e716cbe200] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x55e716cbe200] ref P L0: 35.0% 11.4% 41.4% 12.2%
[libx264 @ 0x55e716cbe200] ref B L0: 66.4% 25.9%  7.6%
[libx264 @ 0x55e716cbe200] ref B L1: 91.3%  8.7%
[libx264 @ 0x55e716cbe200] kb/s:1722.60
Exiting normally, received signal 2.

第二次编辑:新的输出日志 尝试使用新参数但仍然失败

ffmpeg \
    -i "http://admin:@192.168.0.65/media/?action=stream" \
    -re -f lavfi -i anullsrc \
    -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 \
    -c:a aac \
    -f flv "rtmp://a.rtmp.youtube.com/live2/[streamkey]"

结果相同,Youtube 报告YouTube is not receiving enough video to maintain smooth streaming. As such, viewers will experience buffering.但从未拾取流,并且我们的私人流媒体服务器也无法拾取流。

似乎发生的情况是,输出流大约为 4 fps,导致我们的私人 RTSP 服务器拒绝该流,并且 YouTube 在缓冲约一分钟后播放 3 秒的视频(以 10 倍速度播放)。

ffmpeg 输出

$ ffmpeg -i 'http://admin:@192.168.0.65/media/?action=stream' -re -f lavfi -i anullsrc -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 -c:a aac -f flv 'rtmp://x.rtmp.youtube.com/live2/[stream-key-deducted]'

ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mpjpeg, from 'http://admin:@192.168.0.65/media/?action=stream':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Input #1, lavfi, from 'anullsrc':
  Duration: N/A, start: 0.000000, bitrate: 705 kb/s
    Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
  Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native))
Press [q] to stop, [?] for help
[swscaler @ 0x55b35ec1a920] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x55b35ebcd3e0] using SAR=1/1
[libx264 @ 0x55b35ebcd3e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55b35ebcd3e0] profile High, level 3.1
[libx264 @ 0x55b35ebcd3e0] 264 - core 152 r2854 e9a5903 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=50 keyint_min=5 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=4000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=4000 vbv_bufsize=8000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://x.rtmp.youtube.com/live2/[stream-key-deducted]':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 4000 kb/s, 25 fps, 1k tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.107.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 4000000/0/4000000 buffer size: 8000000 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc57.107.100 aac
[flv @ 0x55b35ebcbe00] Failed to update header with correct duration.053.5kbits/s speed=0.168x          
[flv @ 0x55b35ebcbe00] Failed to update header with correct filesize.
frame= 1537 fps=4.3 q=-1.0 Lsize=   30371kB time=00:01:01.36 bitrate=4054.7kbits/s speed=0.172x    

video:30283kB audio:15kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.240946%
[libx264 @ 0x55b35ebcd3e0] frame I:31    Avg QP:18.65  size:123400
[libx264 @ 0x55b35ebcd3e0] frame P:399   Avg QP:21.77  size: 44957
[libx264 @ 0x55b35ebcd3e0] frame B:1107  Avg QP:23.79  size:  8352
[libx264 @ 0x55b35ebcd3e0] consecutive B-frames:  4.0%  0.0%  0.0% 96.0%
[libx264 @ 0x55b35ebcd3e0] mb I  I16..4:  0.4% 98.7%  1.0%
[libx264 @ 0x55b35ebcd3e0] mb P  I16..4:  0.0%  3.5%  0.0%  P16..4: 43.6% 28.2% 20.5%  0.0%  0.0%    skip: 4.2%
[libx264 @ 0x55b35ebcd3e0] mb B  I16..4:  0.0%  0.2%  0.0%  B16..8: 35.2%  3.0%  0.7%  direct: 5.1%  skip:55.8%  L0:51.7% L1:42.9% BI: 5.3%
[libx264 @ 0x55b35ebcd3e0] 8x8 transform intra:99.0% inter:75.4%
[libx264 @ 0x55b35ebcd3e0] coded y,uvDC,uvAC intra: 99.1% 93.6% 54.1% inter: 28.1% 35.6% 1.1%
[libx264 @ 0x55b35ebcd3e0] i16 v,h,dc,p: 87%  1%  2% 10%
[libx264 @ 0x55b35ebcd3e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 14% 11% 19%  5% 10% 15%  7%  7% 12%
[libx264 @ 0x55b35ebcd3e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25%  9%  9%  6% 11% 18%  8%  7%  8%
[libx264 @ 0x55b35ebcd3e0] i8c dc,h,v,p: 57% 17% 18%  8%
[libx264 @ 0x55b35ebcd3e0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x55b35ebcd3e0] ref P L0: 30.6%  8.3% 48.4% 12.6%
[libx264 @ 0x55b35ebcd3e0] ref B L0: 63.2% 29.2%  7.6%
[libx264 @ 0x55b35ebcd3e0] ref B L1: 93.5%  6.5%
[libx264 @ 0x55b35ebcd3e0] kb/s:4034.99
[aac @ 0x55b35ebd0ec0] Qavg: 65536.000
Exiting normally, received signal 2.

编辑 3:将 ffmpeg 更新至 4.2.1,进行综合测试和重新流式传输测试

更新到 v4.2.1 后,问题仍然存在,但是合成视频测试运行流畅......

合成输出

$ ffmpeg -re -f lavfi -i testsrc2=d=600:s=1280x720 -re -f lavfi -i anullsrc -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 -c:a aac -flags +global_header -f flv 'rtmp://192.168.0.91/live?p=[streamid]/[streamkey]'

ffmpeg version 4.2.1-0york0~18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
  configuration: --prefix=/usr --extra-version='0york0~18.04.1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-nonfree --enable-libfdk-aac --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, lavfi, from 'testsrc2=d=600:s=1280x720':
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Input #1, lavfi, from 'anullsrc':
  Duration: N/A, start: 0.000000, bitrate: 705 kb/s
    Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
  Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x564c9da28540] using SAR=1/1
[libx264 @ 0x564c9da28540] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x564c9da28540] profile High, level 3.1
[libx264 @ 0x564c9da28540] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=50 keyint_min=5 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=4000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=4000 vbv_bufsize=8000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://192.168.0.91/live?p=[streamid]/[streamkey]':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 4000 kb/s, 25 fps, 1k tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 4000000/0/4000000 buffer size: 8000000 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54.100 aac
[flv @ 0x564c9da270c0] Failed to update header with correct duration.123.1kbits/s speed=0.936x     
[flv @ 0x564c9da270c0] Failed to update header with correct filesize.
frame=  779 fps= 25 q=-1.0 Lsize=   15601kB time=00:00:31.04 bitrate=4117.3kbits/s speed=0.979x    
video:15557kB audio:7kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.234822%
[libx264 @ 0x564c9da28540] frame I:16    Avg QP: 7.28  size: 39981
[libx264 @ 0x564c9da28540] frame P:361   Avg QP:20.22  size: 22060
[libx264 @ 0x564c9da28540] frame B:402   Avg QP:24.90  size: 18225
[libx264 @ 0x564c9da28540] consecutive B-frames:  8.7% 65.2%  6.5% 19.5%
[libx264 @ 0x564c9da28540] mb I  I16..4: 66.1% 23.9% 10.0%
[libx264 @ 0x564c9da28540] mb P  I16..4:  1.5%  3.1%  0.5%  P16..4:  5.7%  4.7%  3.1%  0.0%  0.0%    skip:81.4%
[libx264 @ 0x564c9da28540] mb B  I16..4:  0.2%  0.4%  1.8%  B16..8:  8.5%  3.1%  0.8%  direct: 0.8%  skip:84.4%  L0:47.4% L1:39.8% BI:12.8%
[libx264 @ 0x564c9da28540] 8x8 transform intra:37.6% inter:26.4%
[libx264 @ 0x564c9da28540] coded y,uvDC,uvAC intra: 24.7% 29.3% 28.8% inter: 4.0% 7.1% 6.1%
[libx264 @ 0x564c9da28540] i16 v,h,dc,p: 94%  6%  1%  0%
[libx264 @ 0x564c9da28540] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 14%  7% 75%  1%  0%  0%  0%  1%  0%
[libx264 @ 0x564c9da28540] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15% 17% 29% 10%  5%  6%  7%  7%  5%
[libx264 @ 0x564c9da28540] i8c dc,h,v,p: 83%  5% 11%  1%
[libx264 @ 0x564c9da28540] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x564c9da28540] ref P L0: 52.8%  7.6% 24.0% 15.6%
[libx264 @ 0x564c9da28540] ref B L0: 72.2% 25.3%  2.4%
[libx264 @ 0x564c9da28540] ref B L1: 97.3%  2.7%
[libx264 @ 0x564c9da28540] kb/s:4089.76
[aac @ 0x564c9da29bc0] Qavg: 65536.000

MJPEG 相机重新流输出

$ ffmpeg -i 'http://admin:@192.168.0.65/media/?action=stream' -re -f lavfi -i anullsrc -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 -c:a aac -flags +global_header -f flv 'rtmp://192.168.0.91/live?p=[streamid]/[streamkey]'

ffmpeg version 4.2.1-0york0~18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
  configuration: --prefix=/usr --extra-version='0york0~18.04.1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-nonfree --enable-libfdk-aac --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mpjpeg, from 'http://admin:@192.168.0.65/media/?action=stream':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Input #1, lavfi, from 'anullsrc':
  Duration: N/A, start: 0.000000, bitrate: 705 kb/s
    Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
  Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native))
Press [q] to stop, [?] for help
[swscaler @ 0x55b731e022c0] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x55b731db6400] using SAR=1/1
[libx264 @ 0x55b731db6400] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55b731db6400] profile High, level 3.1
[libx264 @ 0x55b731db6400] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=50 keyint_min=5 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=4000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=4000 vbv_bufsize=8000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://192.168.0.91/live?p=618a569164672a82c0303495387593f0/5db8eeea4be0b':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 4000 kb/s, 25 fps, 1k tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 4000000/0/4000000 buffer size: 8000000 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 44100 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc58.54.100 aac
[flv @ 0x55b731db51c0] Failed to update header with correct duration.404.3kbits/s speed=0.119x          
[flv @ 0x55b731db51c0] Failed to update header with correct filesize.
frame=  157 fps=4.2 q=-1.0 Lsize=    3178kB time=00:00:06.16 bitrate=4225.8kbits/s speed=0.163x    
video:3170kB audio:1kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.207686%
[libx264 @ 0x55b731db6400] frame I:4     Avg QP:18.14  size:125881
[libx264 @ 0x55b731db6400] frame P:41    Avg QP:21.52  size: 43972
[libx264 @ 0x55b731db6400] frame B:112   Avg QP:23.61  size:  8388
[libx264 @ 0x55b731db6400] consecutive B-frames:  4.5%  1.3%  0.0% 94.3%
[libx264 @ 0x55b731db6400] mb I  I16..4:  0.3% 98.2%  1.5%
[libx264 @ 0x55b731db6400] mb P  I16..4:  0.1%  3.8%  0.0%  P16..4: 42.6% 27.2% 20.2%  0.0%  0.0%    skip: 6.1%
[libx264 @ 0x55b731db6400] mb B  I16..4:  0.0%  0.4%  0.0%  B16..8: 34.2%  3.0%  0.7%  direct: 5.4%  skip:56.2%  L0:52.0% L1:42.1% BI: 5.8%
[libx264 @ 0x55b731db6400] 8x8 transform intra:98.2% inter:75.5%
[libx264 @ 0x55b731db6400] coded y,uvDC,uvAC intra: 93.0% 90.0% 55.7% inter: 27.5% 35.0% 1.5%
[libx264 @ 0x55b731db6400] i16 v,h,dc,p: 94%  0%  2%  4%
[libx264 @ 0x55b731db6400] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 11% 18%  5%  9% 14%  6%  6% 12%
[libx264 @ 0x55b731db6400] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 35%  9%  8%  5%  9% 15%  6%  5%  9%
[libx264 @ 0x55b731db6400] i8c dc,h,v,p: 56% 17% 20%  7%
[libx264 @ 0x55b731db6400] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x55b731db6400] ref P L0: 32.1%  8.0% 47.4% 12.5%
[libx264 @ 0x55b731db6400] ref B L0: 63.3% 29.5%  7.3%
[libx264 @ 0x55b731db6400] ref B L1: 93.6%  6.4%
[libx264 @ 0x55b731db6400] kb/s:4134.76
[aac @ 0x55b731db87c0] Qavg: 65536.000
Exiting normally, received signal 2.

编辑 4:工作命令排序 我已经设法通过以下命令使事情变得稳定,也许有人可以解释为什么这实际上会起作用......

ffmpeg \
 -y -r 4.2 \
 -i "http://admin:@192.168.0.$1/media/?action=stream" \
 -re -f lavfi -i anullsrc \
 -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 \
 -c:a aac \
 -filter:v fps=fps=25 \
 -f flv "rtmp://a.rtmp.youtube.com/live2/[streamkey]" 

运行此命令时,输出流实际上以 24fps 运行,而不是 4fps

frame= 577 fps=4.2 q=24.8 Lsize= 994kB time=00:00:23.04 bitrate= 353.6kbits/s speed=0.167x

对比

frame= 406 fps= 24 q=-1.0 Lsize= 7967kB time=00:00:16.12 bitrate=4048.5kbits/s speed=0.951x

答案1

ffmpeg \
    -i "http://admin:@192.168.0.65/media/?action=stream" \
    -re -f lavfi -i anullsrc \
    -c:v libx264 -vf format=yuv420p -b:v 4000k -bufsize 8000k -maxrate 4000k -g 50 \
    -c:a aac \
    -f flv "rtmp://a.rtmp.youtube.com/live2/[streamkey]"
  • YouTube 需要存在音频流,因此请使用 anullsrc 过滤器添加静音音频。
  • YouTube可能不接受yuvj420p像素格式(我没有验证这一点),所以将其更改为yuv420p。
  • -f h264不能与音频一起使用,因此使用-f flv
  • 您可能不需要-f mjpeg输入选项。
  • 声明流式传输的比特率和 VBV 选项,并将-g值设为帧速率 x 2。

相关内容