July 25, 2015 by Daniel P. Clark

Play HD Youtube from the Raspberry Pi Command Line

Quick tip for most Linux distros.  You can just use the mpv player to play a Youtube video from the command line.  It’s a take off on mplayer2, and it has quvi built in.  Unfortunately it’s not available for the Raspberry Pi architecture.

Now the Raspberry Pi has some pretty good graphics built in.  But you need software that’s optimized for it to really take advantage of it.  That’s where omxplayer comes in (built for XBMC/Kodi).  It’s a video player that works directly with the hardware.  So you can turn on your Raspberry Pi’s Raspbian distro and play High Definition videos without ever loading any graphical interface!  Awesome!

Now the omxplayer doesn’t currently support youtube URLs  for video playback so we need a tool to extract the video resource link from youtube.  That’s where youtube-dl comes in.  youtube-dl is an all purpose youtube video extraction tool.  It’s okay to use an older youtube-dl for this from the standard Raspbian distro resource. Once you have these two packages installed and in working order you can simply play a Youtube video with:

This extracts the video URL and passes it to omxplayer which will play it in High Def on your Raspberry Pi without ever loading xWindows.  Now that’s pretty sweet!

The youtube-dl can take several kinds of video page links: the short url, the full watch url (https://www.youtube.com/watch?v=), or just the 11 characters in the URL that represent the video.  Once you give it the human readable URL it extracts a really long URL for the resource which changes about every three seconds.  For this reason the URL needs to be played right away or the link is no longer valid.  So if you’re writing a script to play Youtube videos keep that in mind.

Now I’ve once written a Ruby script to randomly find a Youtube video.  I went back to my code and tried it out and I’ve found that Youtube has disabled their v2 API resource.  Fortunately for me I’ve written a library that does all the heavey lifting for me called YtUtil.

You can get an Array of Hashes back of video results with YtUtil::Scrape.query(‘Rhett and Link’) .  Each video has an index key called :video which will return the 11 character code Youtube has for each video.  This you can hand to the youtube-dl command line tool to extract the resource for omxplayer.

I decided to mimic Google’s “I’m Feeling Lucky” search tool for Youtube video playing so I wrote a script to do so.

Place this file in you path.  I suggest having it in ~/bin and make sure you local environment variable PATH includes it.  Then make it executable chmod +x ~/bin/imfeelinglucky

Then you ready to use it on the command line.

Just sit back and enjoy ^_^.  If you want to enter the video codes on the command line you can make this simple bash script.

Place the file name play into your path ~/bin and change it to executable and then use it: play 2F_fg4QrBvI

What next?

What should be next is to play videos directly from a CLI text web browser such as links or lynx.  But the documentation I’ve seen on these don’t seem to indicate a way to implement this.  Basically what I want to do is when I open a web page that fits the regex pattern matching https://www.youtube.com/watch?v= then open the url with omxplayer in a sub-process.  After that finishes it will be returned directly to the browser web page.  Since the docs aren’t great for this I would really appreciate help on this.  Anyone who knows how let me know in the comments below of hit me up on Twitter: @6ftdan

The Raspberry Pi is a great computer.  I’m currently not a fan of the xWindows desktop in Raspbian as it is a resource hog and I don’t have much resources available.  Make the command line as awesome and efficient as possible and you’ll have a great machine!

Just so you know, I’m using an older Raspberry Pi so yes it plays High Def video no problem with it’s resources.  Thank you omxplayer developers for your wonderfully optimized player!

Please feel free to comment, share, subscribe to my RSS Feed, and follow me on twitter @6ftdan!

God Bless!
-Daniel P. Clark

Image by DRs Kulturarvsprojekt via the Creative Commons Attribution-ShareAlike 2.0 Generic License

#cli#command line#mplayer#mpv#omxplayer#play#Raspberry Pi#RPI#ruby#scripts#video#youtube#youtube-dl