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:
omxplayer `youtube-dl -g youtube-video-page-here`
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.
gem install yt_util
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.
#!/usr/bin/ruby require 'yt_util' puts "Querying..." results = YtUtil::Scrape.query(ARGV.join(" ")) results = results.map {|i| i[:video] }.compact puts "Results are in." video = "" # Some results may not play so we need to verify a URL returns results.each do |code| # Iterate over results for a valid video puts "Attempting video extraction." video = x%(youtube-dl -g #{code}) unless video.empty? # is valid (same as if video is present) # Since the link changes quickly we'll need to redo the request later video = code break # we have a working video, no need to continue loop end end puts "Extraction successful!" system("omxplayer `youtube-dl -g #{video}`")
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.
imfeelinglucky Rhett and Link
Just sit back and enjoy ^_^. If you want to enter the video codes on the command line you can make this simple bash script.
omxplayer `youtube-dl -g $1`
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
Petr Urban
April 26, 2016 - 9:38 am
same script can be used for twitch tv 🙂
my wrapper:
—————
and bash.rc
Petr Urban
April 26, 2016 - 9:41 am
usage:
or
Daniel P. Clark
April 26, 2016 - 10:23 am
Thanks for sharing! And nice choice of sample video.
Powsniffer0110
August 30, 2017 - 10:37 pm
hey when i do omxplayer youtube-dl -g youtubevideo
I get youtube-dl not found, even tho I do have youtube-dl installed. Why is this?
Daniel P. Clark
September 4, 2017 - 8:30 pm
Because you’re telling omxplayer to play a video or audio file named youtube-dl in the current directory. Which does not exist and is not what you want to do.