Saturday, 2 March 2013

Open YouTube video in OMXPlayer with one click!



Poor quality video; but some good fails tho. ;-)


One of the drawbacks of Flash is that it requires a decent amount of power to execute. Even simple animations, when rendered on the Pi using Gnash, are not very smooth at the best of times. Unfortunately, YouTube and numerous other streaming video sites all use Flash.  Wouldn't it be great to stream these videos easily to the hardware accelerated OMXPlayer that is included in Raspbian?

The following tutorial will allow you to browse the YouTube website in Chromium, but instead of playing the video by clicking on it, you click a custom icon on your panel launcher in LXDE. This process should also work in Firefox, but I haven't tried it.

Disclaimer: I can't be held responsible for borking your Raspbian install if you follow these instructions. This procedure worked for me; YMMV. ;-)


Environment:
I am using the latest version of Raspbian for this tutorial.
2013-02-09-wheezy-raspbian (http://www.raspberrypi.org/downloads)

Let's get started:
SSH remotely to your Pi, or open a terminal in LXDE. I find it's a good idea when starting off on something like this to make sure the system is up-to-date.
sudo apt-get update
(check repos for updated packages/libraries)
sudo apt-get upgrade
(upgrade the packages)
sudo reboot
(if you are extra cautious like me)

Step 1: Download/update additional software
Get/update OMXPlayer. I found that the OMXPlayer included with this version of Raspbian resulted in corrupted desktop graphics or black screens after playing video. Updating OMXPlayer fixed this issue for me:
cd ~
wget http://omxplayer.sconde.net/builds/omxplayer_0.2.5~git20130223~b1aec105_armhf.deb
(download to home dir)
sudo dpkg –-install omxplayer_0.2.5~git20130223~b1aec105_armhf.deb
(run installation package)
rm omxplayer_0.2.5~git20130223~b1aec105_armhf.deb
(clean-up)
For the most current version of OMXPlayer, visit: OMXPlayer Builds

Get 'YouTube Downloader' (youtube-dl) to convert YouTube URL's to the video stream address that's passed to OMXPlayer.
- I already had it, but Google had changed the method for addressing the stream so I needed to update it. This occurs fairly frequently, so they have built in an update feature on the command line.

sudo apt-get install youtube-dl
(install from repo)
sudo youtube-dl -–update
(get latest version)

Get SQLite3; a very small database engine that many applications make use of.  It's needed to read the History database of Chromium (& Firefox).
sudo apt-get install sqlite3
(install SQLite3 from repo)

Step 2: Create a script to read the last page in the browser History database and use youtube-dl to find where it is and stream it to OMXPlayer. This can all be done with very few commands (Gotta love the power of Linux).
Create a 'bin' folder in your home folder if not already there and edit a new file called 'omxplayyt.sh'.
mkdir ~/bin
cd ~/bin
nano omxplayyt.sh

Type/copy the following into that file:
#!/bin/bash
#Script to play YouTube using OMXPlayer
echo "Loading YouTube Video..."
cp ~/.config/chromium/Default/History ~/History.tmp
omxplayer -o hdmi $(youtube-dl --max-quality 35 -g $(sqlite3 ~/History.tmp 'SELECT url FROM urls ORDER BY last_visit_time DESC LIMIT 1'))
rm ~/History.tmp

- press Ctrl-X, then 'y', then 'Enter' to save

Explanation:
- We need to copy the browser History database to a .tmp as it is locked when the browser is open
- Line 5 is the magic: SQLite3 queries the last URL from the URLS table and passes it to youtube-dl to fetch. Youtube-dl then passes the stream on to OMXPlayer to display.
- Finally, we delete the .tmp file

Notes:
- I use the '-–max-quality' flag because my DSL isn't fast enough to stream 720p without stutter. See wikipedia for more info on YouTube fomats.
- 'omxplayer -o hdmi' forces OMXPlayer to output via the HDMI connection.

Set the script to be executable:
chmod +x omxplayyt.sh

Step 3: Create a '.desktop' file to define an icon/launcher to call our script while looking at a YouTube page, then place it in the 'Application Launch Bar' panel for quick access.
Create the .desktop file so LXDE will place an icon in the 'Accessories' menu
cd ~/.local/share/applications
nano omxplayyt.desktop

Type/copy the following into that file:
[Desktop Entry]
Type=Application
Name=OMXPlayYT
GenericName=OMXPlayYT
Comment=Open YouTube url in OMXPlayer
TryExec=lxterminal
Exec=lxterminal --geometry=25x1 -t OMXPLAYYT --working-directory=/home/pi/bin -e ./omxplayyt.sh
NStartupNotify=true
Categories=Utility;Player;
Icon=youtube_48
Encoding=UTF-8
Name=OMXPlayYT
Comment=Open YouTube url in OMXPlayer

- press Ctrl-X, then 'y', then 'Enter' to save

Explanation:
- The important items here are:
Exec: the environment that our script runs in; it is a bash script so lxterminal is the binary that launches it
--geometry=(columns)x(rows): I made the terminal window very small so it basically only shows “Loading YouTube Video...”. You may want to leave this out until you know everything works.
-t: The title of the lxterminal window
-e: Execute the following script
Icon: I downloaded a nice one (I'll get to that later)
Categories: 'Utility' actually places the icon in the 'Accessories' menu of the LXDE Menu

Step 4: Get a cool icon and create your 'Application Launch Bar' launcher.
If you're not already there, start LXDE with the 'startx' command (or VNC in).
Open Chromium and get yourself a nice icon! I used a 48x48 .png icon called 'youtube_48' and placed it into the following folder using PCManFM:
~/.local/share/icons/hicolor/16x16/apps

- This location is in the icon path for my LXDE theme so I was able to just type the file-name into the Icon parameter for the .desktop file above. If your icon doesn't show after doing this, try shutting down and restarting LXDE; now might also be a good time to reboot. ;-) If it still doesn't show, you can simply edit the .desktop file and enter the full path to the icon in the Icon parameter.

Now we can set up the icon as a launcher in the 'Application Launch Bar'.
- Right-click the LXDE Menu icon (Start button) and choose 'Add/Remove Panel Items'
- Select 'Application Launch Bar' from the list, and click the 'Edit' button.  

A window with two columns will appear; in the right-hand column, expand 'Accessories', select 'OMXPlayYT', then click the 'Add' button so that it appears in the left-hand column.
- Select the 'OMXPlayYT' icon in the left-hand column, and use the 'Up' and 'Down' buttons to position it to your taste.
- Click 'Close' to close the 'Application Launch Bar' settings, then 'Close' again to close the 'Panel Preferences' window.

We are DONE!!

Fire up Chromium, and go to YouTube. Choose a video to play. Once the page loads up, click your new icon and the vid should play in a full-screen OMXPlayer window.
- Pause = Space
- Quit = q
*Note: If you click your icon before the page completely loads, the last video may play.  I have completely removed Gnash to decrease load times.

See http://elinux.org/Omxplayer for all the OMXPlayer hotkeys.

I hope you get a lot of use out of this.  I find it easier than using some of the non-LXDE YouTube browsers for the Pi. Leave me a comment!

14 comments:

  1. Works great, many thanks.

    ReplyDelete
  2. Works great, many thanks !

    INFO: If you still do not have sound, you can modify omxplayyt.sh file as follow: (-r added)
    cd ~bin
    nano omxplayyt.sh

    #!/bin/bash
    #Script to play YouTube using OMXPlayer
    echo "Loading YouTube Video..."
    cp ~/.config/chromium/Default/History ~/History.tmp
    omxplayer -o hdmi -r $(youtube-dl --max-quality 35 -g $(sqlite3 ~/History.tmp 'SELECT url FROM urls ORDER BY last_visit_time DESC LIMIT 1'))
    rm ~/History.tmp

    ReplyDelete
    Replies
    1. Adding the -r doesn't make the video have sound, but it does make your screen go black after watching the video

      Delete
  3. Holy wow dude. This worked like a champ. Way better than the command-line solutions. I got it working with Midori which comes with Raspbian with this modification to the bash script:


    #!/bin/bash
    #Script to play YouTube using OMXPlayer
    echo "Loading YouTube Video..."
    cp ~/.config/midori/history.db ~/History.tmp
    omxplayer $(youtube-dl --max-quality 35 -g $(sqlite3 ~/History.tmp 'SELECT uri FROM history ORDER BY date DESC LIMIT 1'))
    rm ~/History.tmp

    ReplyDelete
  4. ya this is useless if you're new to linux and the pi with iceweasel

    ReplyDelete
  5. Hi, I'm new to the Pi. I'm running the latest version of Raspbian, when I do the command "cd ~/.local/share/applications" it returns to me that there is no such file or directory. Every other step has worked, am I doing something wrong?

    ReplyDelete
    Replies
    1. do a "cd ~/.local/share" first, then a "ls -a" (you will see the directory) and then "cd applications"

      Delete
  6. Better is use it in that way: omxplayer $(youtube-dl -s -g http://www.youtube.com/watch?v=CSvFpBOe8eY)

    I have alias for omplayer to add few options as my Pi is connected to TV and Pi sends coded sound to TV and TV is doing entire decoding.

    ReplyDelete
  7. Hi.. I got the same problem with randomhkkid, any solution to this? Pls email me at E4zyfr34k@yahoo.com. Thank u.

    ReplyDelete
  8. Thanks, very cool! How hard to download, or even play, all the files in YT search result?

    ReplyDelete
  9. Works great, thanks!
    However, I liked having a GUI for the Video, therefore I used the one from https://www.raspberrypi.org/forums/viewtopic.php?t=40860

    #!/bin/bash
    #Script to play YouTube using OMXPlayer
    echo "Loading YouTube Video..."
    cp ~/.config/chromium/Default/History ~/History.tmp
    omxplayergui.py ytdl $(sqlite3 ~/History.tmp 'SELECT url FROM urls ORDER BY last_visit_time DESC LIMIT 1')
    rm ~/History.tmp

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hey mate great tutorial and scripts! I have a little different setup, with less steps involved now throughout the years and new updates, but I basically have a one question I'm dying for someone to answer. I have my YouTube.sh file in my pi home folder so when I click my desktop icon it runs the scrips and grabs the URL that's in my clipboard. Anyway, whoever created the YouTube.sh script set it to local audio instead of HDMI, but I cannot simply edit the .sh file it makes me save as then the file isn't executable anymore! How can I either edit the YouTube.sh file, or make a new one with the same text but switched to HDMI audio, and make the file and script executable! Thanks!

    ReplyDelete