youtube-dl -o - <webpage> | vlc - shows a video in VLC. However, the video is piped (through something like fd://0), which inhibits the possibility to jump forwards/backwards.

However, youtube-dl -j <webpage> lists JSON data which contains several "url" properties. If you do vlc <url>, VLC now shows video length, lets us jump, etc. like if we were playing a local video.

Question: Now, it's perfectly possible to write a small Python script that extracts the URL, but is there a simple way to do this using only simple Bash, preferably a one-liner?

Note: youtube-dl -j lists an array of video streams in different qualities, and it's desirable to pick the video with the highest quality.

  • To move on the stream, you need to store the file. The simple way: you create a temporary file (use mktemp), and you download the youtube video there. You feed the file to vlc. You remove the file (and because it is in /tmp/, if something go wrong, the file will be removed anyway. – Giacomo Catenazzi Feb 5 '19 at 8:46
  • @GiacomoCatenazzi: Storing the file is not necessary, like written in the second paragraph. You can move on the file by using the technique described in the second paragraph. – forthrin Feb 5 '19 at 8:54
  • I'm not understanding vlc json->...->url: are you meaning "get json from youtube-dl, parse it, extract one URL and feed that to vlc? – fra-san Feb 5 '19 at 11:48
  • @fra-san: Correct! – forthrin Feb 5 '19 at 14:12

Parsing JSON in the shell is generally not a great idea. You can easily find that, on U&L, almost all the answers to questions along the lines of "how can I parse this JSON in the shell?" end up using specialized tools (e.g. jq or jshon).

This is why I suggest to leverage the ability of youtube-dl to select one video version when more than one is available and to print its URL on standard output instead of downloading it:

  • --format or -f: lets you... specify a format. To have the highest quality, just specify best. Actually, in your case this is probably not required, because (see manual page youtube-dl(1)):

    By default youtube-dl tries to download the best available quality

  • --get-url, or -g, avoids downloading any video and only prints the URL of the selected one to standard output.

Then, leverage the ability of vlc to play (and seek) a video from URL. You can either pipe the URL to vlc:

youtube-dl --get-url --format best 'https://www.youtube.com/watch?v=video_id' | vlc -

or use command substitution to invoke vlc with the URL as argument:

vlc "$(youtube-dl --get-url --format best 'https://www.youtube.com/watch?v=video_id')"
  • Very nice! I can confirm that the latter solution works as expected. I hope others will find this useful! PS! It would be even better if there was a way to get subtitles support this way. I'm suspecting this will be a bit messy unless we can count on some developer support. – forthrin Feb 6 '19 at 7:47
  • @forthrin Glad to hear that. Unfortunately I'm not aware of any way to let vlc get subtitles from Youtube (or anything else that is not a subtitles file). – fra-san Feb 6 '19 at 8:11
  • Simple: You can do vlc --sub-file video.srt video.mp4. The question is how to get the subtitle file directly from youtube-dl directly on the command line, in a single go. I've requested support for this from the developers. – forthrin Feb 6 '19 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.