Ubuntu / Linux news and application reviews.

I've recently came upon a reddit submission which explains how to use mplayer (or VLC) to view YouTube videos external, but while browsing using Firefox (method 1).

While some may find method #1 (works for Firefox only) to be more convenient, I am using method #2 and love it, especially since #2 should work with any browser (I've only tested it with Firefox and Chromium though).


There are other ways of doing this, like:
- a Greasemonkey script which replaces the Flash Player on websites with VLC or Mplayer but that was kind of buggy;
- an external YouTube player but I usually you'll find links YouTube links inside your browser so you would have to copy each link in an external player, etc. and that's too much trouble.


But you will probably find the methods in this post to be more convenient because:
  • you can open any link you're browsing in Firefox, in Mplayer of VLC (method 1)
  • because we are going to use youtube-dl, you can set the video quality (method 1)
  • the second method works for any browser and some other websites too (not just YouTube) - I've only tried Firefox and Google Chrome but it should work with others too
  • basically everything is done within the browser is it is very convenient.
  • all is done with just one click (of course, setting this up takes some more steps but you must only do this once).


You'll understand why this is a really nice way to view YouTube videos once you'll try it. Before doing that, you might want to see a video with both methods in action:





Method 1: Right clicking any YouTube link to play a video



Install required tools / Firefox addons


You'll need either VLC or MPlayer so install either of those from your distribution repositories. For Ubuntu, install VLC with the following command:
sudo apt-get install vlc


You'll also need to install "youtube-dl". In Ubuntu, simply run this in a terminal:
sudo apt-get install youtube-dl


And finally, you'll have to install 2 Firefox addons:

Don't worry about anything related to compatibility. I've tested them on Firefox 3.6 and everything works. To make any Firefox extension work with the latest FF version, see the Compatibility Reporter addon.

Instead of Context Options extension you can also use FireGestures. Details on setting up FireGestures instead of Context Run, on reddit.


Setting up Firefox and required scripts


There are two ways of setting this up: one where you can right click any link without even visiting the page and opening the video on that link in VLC or MPlayer and a second method which will only work for any YouTube video on which page you are on (so the video has to start playing, however you can stop it if you want, but don't close the page).



a) Create a script in your path which will point to the link you will right click. To do this, type this in a terminal:
sudo gedit /usr/local/bin/yt

and paste this in the newly opened file:
#!/bin/sh
vlc "$(youtube-dl -g -f 18 $1)" &

You can replace "vlc" with "mplayer" in the code above! You can also add custom options to vlc or mplayer and choose the youtube format you like best (different than 18).

Then, save the file and make it executable using the following command:
sudo chmod +x /usr/local/bin/yt


b) Set up the Context Options Firefox addon

In Firefox, go to Tools > Add-ons > Context Options > Preferences, click on New, uncheck everything but Hyperlink and paste the following code into the bottom field:
try {
var linkURL = get_link_url();
if (!linkURL) throw "Not a link";
var CR = new CommandRunHandler();
CR.run ("/usr/local/bin/yt",[linkURL]);
} catch (err) {
alert ("Error: " + err.description);
}


Remember to insert the right path ("/usr/local/bin/yt" in my example, but replace it with your own if you used a different path). Finally give it a name in the upper text box in the middle column (I've named it "YouTube VLC"):

Context Option


c)
Set up CommandRun Firefox addon

In Firefox, go to Tools > Add-ons > Commandrun > Preferences and paste this:
["/usr/local/bin/yt"]

If you used a different path, replace it with your own.

Now you must restart Firefox. Also please note that every time you modify something in the Context Options Addon settings, you must restart Firefox (I know, kind of weird).



Method 2: Open the currently playing video in MPlayer / VLC


Update (February 10th, 2011): the instructions below only work with Adobe Flash older then 10.2. For Adobe Flash 10.2 or newer, see: Play YouTube Videos Without Flash [From /tmp, Works With Adobe Flash 10.2]



This method is a lot easier to set up and will also work with other websites, not just YouTube. The downside is that you must create a launcher on your panel and click it whenever you want to open the currently playing video in VLC / Mplayer. Also, the quality will be the same of what you set for the currently playing video in the webpage.

a) Create a script in your path to run VLC or Mplayer

In Ubuntu, paste this in a terminal:
sudo gedit /usr/local/bin/yt2


and paste this in the newly opened file:
#!/bin/bash
vlc /tmp/Flash*


Of course, you can replace "vlc" with "mplayer"

Then make the script executable:
sudo chmod +x /usr/local/bin/yt2


b) Create a launcher on your panel

In Gnome, simply right click a panel, select "Create new custom launcher", set whatever name and icon you want for it and under "Command", enter this:
/usr/local/bin/yt2


That's it. Now when playing a video you can click the stop button and then click the newly created launcher to open the video in Mplayer or VLC.