将ffmpeg用于传入的youtube HLS直播流并输出到rtmp

时间:2018-03-14 23:25:21

标签: ffmpeg rtmp hls youtube-dl

我无法使用ftmpeg来处理来自youtube的直播。普通的非现场视频工作正常。但是对于实时流,ffmpeg似乎没有对新段提出足够的请求。它基本上下载,比如一个3秒的段,然后在10秒左右没有任何事情发生,然后它下载一个新的段,重复......

ffmpeg -i '$(./youtube-dl -f best -g https://www.youtube.com/watch?v=emGI3UrzIMc)' -f flv rtmp://192.168.0.11/myapp/mystream

那么自动命令是什么?

ffmpeg版本:

ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7.3.0 (Rev1, Built by MSYS2 project)
configuration: --prefix=/mingw64 --target-os=mingw32 --arch=x86_64 --disable-debug --disable-static --enable-avresample --enable-dxva2 --enable-d3d11va --enable-fontconfig --enable-gnutls --enable-gpl --enable-libass --enable-libbluray --enable-libcaca --enable-libcelt --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libx265 --enable-libxvid --enable-libvpx --enable-libwebp --enable-openal --enable-libwavpack --enable-pic --enable-postproc --enable-runtime-cpudetect --enable-shared --enable-static --enable-swresample --enable-version3 --enable-zlib --disable-doc
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

youtube-dl版本:2018.03.14

以下是上述命令的输出。我添加了<MY NOTE #>用于说明目的。

开始时执行顺利进行。然后在<MY NOTE 1>冻结10或15秒。然后它下载流的一部分 - <MY NOTE 2>,播放,然后再次冻结几秒钟。然后下载下一部分 - <MY NOTE 3>,播放,冻结等等......

$ ffmpeg -i '$(./youtube-dl -f best -g https://www.youtube.com/watch?v=emGI3UrzIMc)' -f flv rtmp://192.168.0.11/myapp/mystream
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (Rev1, Built by MSYS2 project)
  configuration: --prefix=/mingw64 --target-os=mingw32 --arch=x86_64 --disable-debug --disable-static --enable-avresample --enable-dxva2 --enable-d3d11va --enable-fontconfig --enable-gnutls --enable-gpl --enable-libass --enable-libbluray --enable-libcaca --enable-libcelt --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libx265 --enable-libxvid --enable-libvpx --enable-libwebp --enable-openal --enable-libwavpack --enable-pic --enable-postproc --enable-runtime-cpudetect --enable-shared --enable-static --enable-swresample --enable-version3 --enable-zlib --disable-doc
  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
[hls,applehttp @ 00000287da404cc0] Opening 'https://r4---sn-cap1vv-nv4l.googlevideo.com/videoplayback/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8/sq/1308292/goap/clen%3D48852%3Blmt%3D1520930742662673/govp/clen%3D347004%3Blmt%3D1520930742662679/dur/2.000/file/seg.ts' for reading
Input #0, hls,applehttp, from 'https://manifest.googlevideo.com/api/manifest/hls_playlist/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/dover/10/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8':
  Duration: N/A, start: 39603.798400, bitrate: N/A
  Program 0
    Metadata:
      variant_bitrate : 0
    Stream #0:0: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, stereo, fltp
    Metadata:
      variant_bitrate : 0
    Stream #0:1: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 30 fps, 30 tbr, 90k tbn, 60 tbc
    Metadata:
      variant_bitrate : 0
Stream mapping:
  Stream #0:1 -> #0:0 (h264 (native) -> flv1 (flv))
  Stream #0:0 -> #0:1 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, flv, to 'rtmp://192.168.0.11/myapp/mystream':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: flv1 (flv) ([2][0][0][0] / 0x0002), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 30 fps, 1k tbn, 30 tbc
    Metadata:
      variant_bitrate : 0
      encoder         : Lavc57.107.100 flv
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 48000 Hz, stereo, fltp
    Metadata:
      variant_bitrate : 0
      <MY NOTE 1>encoder         : Lavc57.107.100 libmp3lame
<MY NOTE 2>[hls,applehttp @ 00000287da404cc0] Opening 'https://r4---sn-cap1vv-nv4l.googlevideo.com/videoplayback/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8/sq/1308293/goap/clen%3D48853%3Blmt%3D1520930742662680/govp/clen%3D404944%3Blmt%3D1520930742662686/dur/2.000/file/seg.ts' for reading
<MY NOTE 3>[hls,applehttp @ 00000287da404cc0] Opening 'https://r4---sn-cap1vv-nv4l.googlevideo.com/videoplayback/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8/sq/1308294/goap/clen%3D48852%3Blmt%3D1520930742662688/govp/clen%3D403294%3Blmt%3D1520930742662693/dur/2.000/file/seg.ts' for reading
[hls,applehttp @ 00000287da404cc0] Opening 'https://r4---sn-cap1vv-nv4l.googlevideo.com/videoplayback/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8/sq/1308295/goap/clen%3D48355%3Blmt%3D1520930742662695/govp/clen%3D358330%3Blmt%3D1520930742662700/dur/2.000/file/seg.ts' for reading
[hls,applehttp @ 00000287da404cc0] Opening 'https://r4---sn-cap1vv-nv4l.googlevideo.com/videoplayback/id/emGI3UrzIMc.0/itag/95/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D136/hls_chunk_host/r4---sn-cap1vv-nv4l.googlevideo.com/ei/6jiqWt7SIJKD8gPimZTACA/playlist_type/DVR/gcr/bg/initcwndbps/10410/mm/32/mn/sn-cap1vv-nv4l/ms/lv/mv/m/pcm2cms/yes/pl/16/keepalive/yes/mt/1521105015/ip/130.204.135.162/ipbits/0/expire/1521126730/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,ei,playlist_type,gcr,initcwndbps,mm,mn,ms,mv,pcm2cms,pl/signature/503304477BD05CCC71061344F8BB23195E152A32.512A84732C501AF08AC9340181A75D34C5B2FF77/key/dg_yt0/playlist/index.m3u8/sq/1308296/goap/clen%3D48852%3Blmt%3D1520930742662701/govp/clen%3D347658%3Blmt%3D1520930742662707/dur/2.000/file/seg.ts' for reading
frame=  225 fps=1.8 q=31.0 size=     947kB time=00:00:07.92 bitrate= 979.2kbits/s speed=0.0625x

而且你不必输出到rtmp。可以是本地文件 - 行为是相同的。

1 个答案:

答案 0 :(得分:3)

尝试使用hls前缀。例如:

ffmpeg -i "hls+$(./youtube-dl -f best -g https://www.youtube.com/watch?v=emGI3UrzIMc)" -f flv rtmp://192.168.0.11/myapp/mystream