Saturday, 9 November 2013

Listen tracks from youtube in mpd

I don't have my collection of music in the office. Last.fm no longer offers its great radio.

Luckily, there are tons of tracks on youtube.
Luckily, I use linux and incredibly powerful mpd player, so I don't have to keep youtube open

Suppose, you want to listen to some vocal trance on youtube.

Use youtube-dl to determine what formats are available
$ youtube-dl -f http://www.youtube.com/watch?v=HS5fHWmzqIg
[youtube] Setting language
[youtube] HS5fHWmzqIg: Downloading video webpage
[youtube] HS5fHWmzqIg: Downloading video info webpage
[youtube] HS5fHWmzqIg: Extracting video information
Available formats:
22      :       mp4     [720x1280]
18      :       mp4     [360x640]
43      :       webm    [360x640]
5       :       flv     [240x400]
36      :       3gp     [240x320]
17      :       3gp     [144x176]
136     :       mp4     [720p] (DASH Video)
135     :       mp4     [480p] (DASH Video)
134     :       mp4     [360p] (DASH Video)
133     :       mp4     [240p] (DASH Video)
160     :       mp4     [192p] (DASH Video)
141     :       m4a     [256k] (DASH Audio)
172     :       webm    [256k] (DASH Audio)
140     :       m4a     [128k] (DASH Audio)
171     :       webm    [128k] (DASH Audio)
139     :       m4a     [48k] (DASH Audio)

There are 5 audio formats for this video. 141th has highest bitrate. So all we have to do, is to get real URL for this format and feed it to mpd
$ mpc add `youtube-dl -f141 -g http://www.youtube.com/watch?v=HS5fHWmzqIg`

That's all, track from youtube is already in your mpd playlist. Enjoy!