Closed brandon-doornbos closed 1 year ago
Try --external_downloader ffmpeg
, as you do have ffmpeg.
I don't know if I'm doing something wrong, but ffmpeg doesn't even get invoked when I use it with various dash formats.
Only with a non-dash format, where it crashes.
For me, this works with _ytdlp: wait for fragment without ffmpeg, straight to stdout
with it, but ffmpeg is ignored in yt-dl.
There are two issues:
FFmpegFD
to handle http_dash_segments
For (1):
--- old/youtube_dl/downloader/external.py
+++ new/youtube_dl/downloader/external.py
@@ -273,7 +273,7 @@ class HttpieFD(ExternalFD):
class FFmpegFD(ExternalFD):
@classmethod
def supports(cls, info_dict):
- return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms')
+ return info_dict['protocol'] in ('http', 'https', 'ftp', 'ftps', 'm3u8', 'rtsp', 'rtmp', 'mms', 'http_dash_segments')
@classmethod
def available(cls):
For (2), select a webm format:
$ python -m youtube_dl -v --output - --format bestaudio[ext=webm] 'L-4IuD75pCE' --external-downloader ffmpeg | mpv -
[file] Reading from stdin...
[debug] System config: [u'--prefer-ffmpeg']
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: [u'-v', u'--output', u'-', u'--format', u'bestaudio[ext=webm]', u'L-4IuD75pCE', u'--external-downloader', u'ffmpeg']
[debug] Encodings: locale UTF-8, fs UTF-8, out None, pref UTF-8
[debug] youtube-dl version 2021.12.17
[debug] Git HEAD: 040271022
[debug] Python version 2.7.18 (CPython) - Linux-4.4.0-210-generic-i686-with-Ubuntu-16.04-xenial
[debug] exe versions: avconv 4.3, avprobe 4.3, ffmpeg 4.3, ffprobe 4.3
[debug] Proxy map: {}
[youtube] L-4IuD75pCE: Downloading webpage
[debug] [youtube] Decrypted nsig PLUtfn6oRFl-4PVGv => 4IdEvLLJv56m2A
[debug] [youtube] Decrypted nsig o1PCOZDbiw_vDet-f => 8-xcfMCkd-uQxA
[debug] Invoking downloader on u'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=_3YIZPuHNq-Lp-oPk5KA-AE&ip=46.208.6.20&clen=3953740&id=o-ACoYnNmTVTWXtKHp48s15Y_wvJjB6q4qxF7x6QQZ8N_C&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRQIhAMTsOmAowuqHJ1msAhgejuNPmgGlXrsTJcL3bIeEciS6AiAL7OYTlUf1NjX5-Pnz0rcdB1OQCtpPEhYDjTCOMY7V9Q%3D%3D&pcm2cms=yes&dur=245.421&ns=rUz-oCAkasofphDvDrz4y4UL&initcwndbps=1318750&vprv=1&lsig=AG3C_xAwRgIhALEitcmjDS7MqFB0mIiae94K_szjR3SJoriE3pEUqrQoAiEAg6wRvExxXMZ4Jya1-jyQr2hbGCR_eulLK-Mxy2XvEeQ%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678297951&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=4IdEvLLJv56m2A&mt=1678276141&mv=m&pl=25&ms=au%2Crdu&mvi=5'
[download] Destination: -
[debug] ffmpeg command line: ffmpeg -y -loglevel verbose -headers 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.1 Safari/537.36
' -i 'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=_3YIZPuHNq-Lp-oPk5KA-AE&ip=46.208.6.20&clen=3953740&id=o-ACoYnNmTVTWXtKHp48s15Y_wvJjB6q4qxF7x6QQZ8N_C&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRQIhAMTsOmAowuqHJ1msAhgejuNPmgGlXrsTJcL3bIeEciS6AiAL7OYTlUf1NjX5-Pnz0rcdB1OQCtpPEhYDjTCOMY7V9Q%3D%3D&pcm2cms=yes&dur=245.421&ns=rUz-oCAkasofphDvDrz4y4UL&initcwndbps=1318750&vprv=1&lsig=AG3C_xAwRgIhALEitcmjDS7MqFB0mIiae94K_szjR3SJoriE3pEUqrQoAiEAg6wRvExxXMZ4Jya1-jyQr2hbGCR_eulLK-Mxy2XvEeQ%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678297951&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=4IdEvLLJv56m2A&mt=1678276141&mv=m&pl=25&ms=au%2Crdu&mvi=5' -c copy -f webm -
ffmpeg version 4.3-2ubuntu0~ppa16.04+8 Copyright (c) 2000-2020 the FFmpeg developers
...
[tcp @ 0xa75f80] Starting connection attempt to 86.189.112.16 port 443
[tcp @ 0xa75f80] Successfully connected to 86.189.112.16 port 443
Input #0, matroska,webm, from 'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=_3YIZPuHNq-Lp-oPk5KA-AE&ip=46.208.6.20&clen=3953740&id=o-ACoYnNmTVTWXtKHp48s15Y_wvJjB6q4qxF7x6QQZ8N_C&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRQIhAMTsOmAowuqHJ1msAhgejuNPmgGlXrsTJcL3bIeEciS6AiAL7OYTlUf1NjX5-Pnz0rcdB1OQCtpPEhYDjTCOMY7V9Q%3D%3D&pcm2cms=yes&dur=245.421&ns=rUz-oCAkasofphDvDrz4y4UL&initcwndbps=1318750&vprv=1&lsig=AG3C_xAwRgIhALEitcmjDS7MqFB0mIiae94K_szjR3SJoriE3pEUqrQoAiEAg6wRvExxXMZ4Jya1-jyQr2hbGCR_eulLK-Mxy2XvEeQ%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678297951&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=4IdEvLLJv56m2A&mt=1678276141&mv=m&pl=25&ms=au%2Crdu&mvi=5':
Metadata:
encoder : google/video-file
Duration: 00:04:05.42, start: -0.007000, bitrate: 128 kb/s
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp, delay 312 (default)
Output #0, webm, to 'pipe:':
Metadata:
encoder : Lavf58.45.100
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp, delay 312 (default)
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
(+) Audio --aid=1 --alang=eng (*) (opus 2ch 48000Hz)
AO: [pulse] 48000Hz stereo 2ch floatrate= 114.2kbits/s speed=2.34x
A: 00:00:05 / 00:00:14 (36%) Cache: 9.3s/291KB
...
Thanks for the replies.
I'm guessing this is a separate issue, but it is preventing me from using your solution.
When I use ffmpeg for downloading, it firstly doesn't respect youtube-dl's rate limiting, so when using it with mpv it first downloads the whole file before playing. With ffplay it does its own rate limiting, which is fine by me, however when a video is longer than about 2 minutes it crashes with this error. I assume it finished downloading the file, so youtube closes the connection, and ffmpeg sees this as an error when it hasn't finished streaming.
When streaming to mpv as in my example, it's only downloading fast enough to fill the various buffers in ffmpeg and mpv while playing the stream, so there shouldn't be much point in rate-limiting. But this doesn't happen without the change (1). A pretty plangent song comes out here, with my modified FFmpegFD
.
$ python -m youtube_dl -v --output - --format bestaudio[ext=webm] 'L-4IuD75pCE' --external-downloader ffmpeg | ffplay -loglevel quiet -
[debug] System config: [u'--prefer-ffmpeg']
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: [u'-v', u'--output', u'-', u'--format', u'bestaudio[ext=webm]', u'L-4IuD75pCE', u'--external-downloader', u'ffmpeg']
[debug] Encodings: locale UTF-8, fs UTF-8, out None, pref UTF-8
[debug] youtube-dl version 2021.12.17
[debug] Git HEAD: 040271022
[debug] Python version 2.7.18 (CPython) - Linux-4.4.0-210-generic-i686-with-Ubuntu-16.04-xenial
[debug] exe versions: avconv 4.3, avprobe 4.3, ffmpeg 4.3, ffprobe 4.3
[debug] Proxy map: {}
[youtube] L-4IuD75pCE: Downloading webpage
[debug] [youtube] Decrypted nsig zc3oQz4-UcF7psmV3 => OiYN3mfylZ93ZA
[debug] [youtube] Decrypted nsig S0cQ_Zbf6DfYxdAUR => qaWSRALgvw4P-A
[debug] Invoking downloader on u'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=-xQJZNGJJ87PxwLklYDwAg&ip=46.208.6.20&clen=3953740&id=o-APbNL3mhSwjI8erJqqNI4Bl8PIvcmBXFEujSBC7eGQMK&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRAIgXwpMEtgAQFxNpRutFs37sl_mux69Kj-YJDqoqMQnWwICIHsI4VgAjhJk7EAYzTnzl99P5Lyu-7e-3MwH77Bh5QBi&dur=245.421&ns=OUeD5RQKBhUT9pwJhACNJtAL&initcwndbps=1608750&vprv=1&lsig=AG3C_xAwRQIgR_gmS9-lKDc4L3g8DCUEyNKCZSkAf_4dcP9uxuQsHX4CIQDspCg7phY-VIuDCas19dKMDtzfrlJDIRo_-qeTfFGBVA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678338395&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=OiYN3mfylZ93ZA&mt=1678316229&mv=m&pl=25&ms=au%2Crdu&mvi=5'
[download] Destination: -
[debug] ffmpeg command line: ffmpeg -y -loglevel verbose -headers 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.2 Safari/537.36
' -i 'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=-xQJZNGJJ87PxwLklYDwAg&ip=46.208.6.20&clen=3953740&id=o-APbNL3mhSwjI8erJqqNI4Bl8PIvcmBXFEujSBC7eGQMK&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRAIgXwpMEtgAQFxNpRutFs37sl_mux69Kj-YJDqoqMQnWwICIHsI4VgAjhJk7EAYzTnzl99P5Lyu-7e-3MwH77Bh5QBi&dur=245.421&ns=OUeD5RQKBhUT9pwJhACNJtAL&initcwndbps=1608750&vprv=1&lsig=AG3C_xAwRQIgR_gmS9-lKDc4L3g8DCUEyNKCZSkAf_4dcP9uxuQsHX4CIQDspCg7phY-VIuDCas19dKMDtzfrlJDIRo_-qeTfFGBVA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678338395&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=OiYN3mfylZ93ZA&mt=1678316229&mv=m&pl=25&ms=au%2Crdu&mvi=5' -c copy -f webm -
ffmpeg version 4.3-2ubuntu0~ppa16.04+8 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 20160609
configuration: ...
[tcp @ 0x1ed4080] Starting connection attempt to 86.189.112.16 port 443
[tcp @ 0x1ed4080] Successfully connected to 86.189.112.16 port 443
Input #0, matroska,webm, from 'https://rr5---sn-cu-aigsl.googlevideo.com/videoplayback?sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&fexp=24007246&ei=-xQJZNGJJ87PxwLklYDwAg&ip=46.208.6.20&clen=3953740&id=o-APbNL3mhSwjI8erJqqNI4Bl8PIvcmBXFEujSBC7eGQMK&txp=4532434&gir=yes&requiressl=yes&keepalive=yes&source=youtube&sig=AOq0QJ8wRAIgXwpMEtgAQFxNpRutFs37sl_mux69Kj-YJDqoqMQnWwICIHsI4VgAjhJk7EAYzTnzl99P5Lyu-7e-3MwH77Bh5QBi&dur=245.421&ns=OUeD5RQKBhUT9pwJhACNJtAL&initcwndbps=1608750&vprv=1&lsig=AG3C_xAwRQIgR_gmS9-lKDc4L3g8DCUEyNKCZSkAf_4dcP9uxuQsHX4CIQDspCg7phY-VIuDCas19dKMDtzfrlJDIRo_-qeTfFGBVA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lmt=1668326808849893&c=WEB&expire=1678338395&mime=audio%2Fwebm&fvip=2&itag=251&mm=31%2C29&mn=sn-cu-aigsl%2Csn-cu-c9i6&mh=-I&n=OiYN3mfylZ93ZA&mt=1678316229&mv=m&pl=25&ms=au%2Crdu&mvi=5':
Metadata:
encoder : google/video-file
Duration: 00:04:05.42, start: -0.007000, bitrate: 128 kb/s
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp, delay 312 (default)
Output #0, webm, to 'pipe:':
Metadata:
encoder : Lavf58.45.100
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp, delay 312 (default)
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size= 833kB time=00:00:53.02 bitrate= 128.7kbits/s speed= 1.1x
I should explain myself better, the rate-limiting is to reduce network usage. But that is besides the point.
The song does play, but after 2 minutes or so, the connection gets cut, ffmpeg closes the pipe and the stream stops. So this solution does not work for me, neither in youtube-dl nor yt-dlp.
This would be solved if the native downloader streamed DASH fragments to stdout while downloading, or fixing ffmpeg somehow to not crash while streaming.
Should I file a bug report with ffmpeg, or continue to work with you here to solve it in youtube-dl? I assume downloading whole fragments before streaming is not intended behavior.
... the rate-limiting is to reduce network usage.
It's pointless if you're streaming to a player because the network usage will be as much as needed to play the media and little more. In my log you can see that the song is downloading at ~1/6 of your limit.
Should I ... continue to work with you here to solve it in youtube-dl?
I assume you've applied the patch (1), or ffmpeg wouldn't be running.
This seems to imply some network configuration problem
[tls @ 0x55f64fcb3cc0] Error in the pull function.ts/s speed=1.04x
[tls @ 0x55f64fcb3cc0] IO error: Connection reset by peer
[tls @ 0x55f64fcb3cc0] The specified session has been invalidated for some reason.
G/SO indicates that one or other of these may help:
-http_persistent 0
: makes new HTTP connections, so not relying on maintaining a connection
various -reconnect[...]
options documented here.
Or try an old ffmpeg, if you have one. I'm not seeing the same problems, playing through 2.5 minutes with ffplay.
It's pointless if you're streaming to a player because the network usage will be as much as needed ...
Ah fair enough.
G/SO indicates that one or other of these may help ...
I appreciate the effort, I tried all of these before and none worked. However, just -reconnect 1
did solve it for me which is not documented by ffmpeg for some reason.
My problem is solved now, however I still think the native downloader should stream the fragments while downloading, but I guess that is for you to decide.
Thank you for all the help. Should I close this since it's solved, or keep it open since it is technically still a bug?
As it's a bug/enhancement resulting from https://github.com/ytdl-org/youtube-dl/commit/3da17834a49fad2a97c308fdd89aa26781ef4d60, I'll close it when the 1-line patch is committed, thanks.
Checklist
Verbose log
Description
This happens on git master, not the latest release!
Whenever I stream a video with a dash format (e.g. YouTube 251) to stdout (
--output -
), youtube-dl downloads the entire first dash fragment first and then streams it instead of immediately starting to stream.I've tested it on YouTube and Vimeo, with audio and video formats.
I use a rate limit to reduce stress while streaming, and for videos with one dash fragment, it downloads the entire video before starting to stream to stdout. This increases the latency between requesting and starting a stream dramatically. In the case of the example output (
python -m youtube_dl -vU --output - --format bestaudio L-4IuD75pCE --rate-limit 100K | ffplay -loglevel quiet -
) it's about 30 seconds.Reverting https://github.com/ytdl-org/youtube-dl/commit/3da17834a49fad2a97c308fdd89aa26781ef4d60 fixes it, but that is required to fix throttling(?), so most likely not a permanent fix.
Could we just stream the fragments while downloading? After some watermark has been reached?
If I need to add information or can assist further, please tell me. I'm not familiar enough with python, dash or these kind of streams to dive in myself.