Pages: Welcome | Projects

Stream from shell

2015/7/8
Tags: [ shell ]

Conveniently accessing YouTube

There are a number of good reasons for not accessing YouTube with browsers. It could be a matter of convenience, privacy paranoia, or maybe you just don't want to spawn a browser for a video someone linked to you.

Some useful tools, good for you: youtube-dl and mplayer.

Just download

The youtube-dl software is quite handy to simply download a video from YouTube (and other services too!). In the easiest form

youtube-dl <URL>

it will save one or more videos to the local directory.

Qualities and audio-only

Besides plain download, a video/audio quality selection is also possible. A list of available qualities can be obtained:

youtube-dl -F <URL>

This results in a list of possible selections, so you can download the one you want:

youtube-dl -f <selection> <URL>

Some selections correspond to the audio track only. The best available audio track can be obtained by specifying bestaudio as selection:

youtube-dl -f bestaudio <URL>

Pipe the content

In the best Unix tradition, both youtube-dl and mplayer support piping. You can ask youtube-dl to put the download content on the standard output with the -o - option. On the other side of the pipe there can be any program which understands the multimedia output format.

Watch your video on mplayer:

youtube-dl -o - <URL> | mplayer -

Listen the audio on mplayer:

youtube-dl -f bestaudio -o - <URL> | mplayer -

Paste it directly

If a friend sends you a link to a video, putting the URL inside the previous commands is a bit boring. You have to copy-and-paste it into your terminal, and recall all the parameters around it.

A convenient way of doing it is using the xclip program, which can be used to manipulate your clipboard. Since XWindow supports many clipboards, I usually keep it aliased as follows:

alias xclip='xclip -selection clipboard'

The -o options of xclip stands for read from clipboard, print on stdout.

I have a number of shell function definitions inside my .zshrc (or .bashrc — these definitions are bash-compatible):

ytview() {
    youtube-dl -o - ${1:-`xclip -o`} | mplayer -
}

This first one allows me to directly watch a video given its URL as optional first parameter. If the URL is not supplied, it defaults on the content of the clipboard (see man bash for understanding the ${X:-Y} syntax).

The second command is straightforward now, and plays the audio. I use it whenever I'm interested in listening once the audio track of the video:

ytlisten() {
    youtube-dl -f bestaudio -o - ${1:-`xclip -o`} | mplayer -vo null -
}

If I want to keep the audio, and store it into a specific format, I can use the third command, which makes use of ffmpeg for the conversion part. The first parameter is the destination file name, the second is the usual optional URL, which defaults on the xclip -o output:

ytmusic() {
    youtube-dl -f bestaudio -o - ${2:-`xclip -o`} | ffmpeg -i pipe:0 -vn $1
}

Happy shell-hacking