Combining youtube-dl and VLC without a pipe

jsonvlcyoutube-dl

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.

Best Answer

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')"
Related Question