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!