Hi everyone,
I’ve been tinkering a bit with my Volumio install to have it play the soundtrack of Youtube videos, and I wanted to share. I think it would be really cool to have something similar embedded in the project …
Disclaimer : it’s in prototype stage, it’s dirty, and the track info displayed are ugly (the very long and non-semantic stream URL). But it works and is fairly stable. My setup is slightly different from this, so I may have made some mistakes writing this, feel free to test it and send some feedback,
0. ssh into your volumio
1. Gear up : getting youtube-dl
You need to install the python module “youtube-dl”. You can get it either on github (github.com/rg3/youtube-dl) or on pip. I would recommend installing pip as it’s easier.
1.1. Get pip (as per pip’s documentation : pip.readthedocs.org/en/latest/installing.html)
$ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python get-pip.py
1.2. Get youtube-dl
$ sudo pip install youtube-dl
1.3. Test it !
$ youtube-dl -t http://www.youtube.com/watch?v=04854XqcfCY
It should display : Queen - ‘We Are The Champions’
2. The magic : writing your shell script
To control MPD, we use the “mpc” client. For full info, check the man page : linux.die.net/man/1/mpc
Basically, what we do is use MPC to add the URL of any given video. However, mpd won’t open the URL you use for the video in your browser. We need the “stream URL”, which youtube-dl helps us find.
I’m not a bash wizard at all, so the script is ugly and doesn’t handle exceptions, but I’m sure you’ll excuse me
2.1. Write the shell script :
Upload this script on your machine, and call it get_youtube :
#!/bin/bash
input=$1 # get the argument passed to the script
clean_input="${input/https/http}" # youtube-dl doesn't handle https, so convert "https" to "http"
url=$(youtube-dl -f140 -g $clean_input) # get the stream url, with the -g option. -f140 is to get the audio only
clean_url="${url/https/http}" # mpd doesn't handle https, so convert "https" to "http"
mpc add $clean_url # add the url to the mpc play list
2.2. Make it accessible to all users in rp
Move the shell script to /usr/local/bin/
$ chmod +x get_youtube
$ sudo mv get_youtube /usr/local/bin
2.3. Test it !
$ get_youtube http://www.youtube.com/watch?v=dQw4w9WgXcQ
It should add something to your playlist after a few secondes. You can either launch it from the web interface, or do
$ mpc play
3. User-friendly : creating a php script
Here we want our users to be able to easily add youtube videos to the playlist without ssh-ing to volumio … Since my skills in php are those of an inebriated otter, I wrote a quick hook that is OUTSIDE the normal player. Feel free to integrate it in a nicer way !
3.1 The youtube load form :
This displays a nice form to load a youtube video. It then calls the launcher with the given address, encoded as URL. It uses jQuery, downloaded from Google’s cdn. Call it “youtube.php”
<?php
echo "<html>";
echo " <head>";
echo " <title>Youtube streamer</title>";
echo " <script src='//ajax.googleapis.com/ajax/libs/jquery/2.10.0/jquery.min.js'></script>";
echo " <script>function launch(){ var val = urlencode($('#yurl').val());$.get(\"http://THE_ADDRESS_OF_YOUR_VOLUMIO/launch.php?yurl=\"+val); }</script>";
echo " </head>";
echo " <body>";
echo " <p>Please type the youtube URL you want to listen to:</p>";
echo " <form action='launch.php' method='GET'>";
echo " <input id='yurl' type='text' value='' name='yurl'></input>";
echo " <button onclick='launch()'>Go !</button>";
echo " </form>";
echo " </body>";
echo "</html>";
?>
Don’t forget to update “THE_ADDRESS_OF_YOUR_VOLUMIO” to the correct address / IP of your device.
3.2. The get_youtube launcher
This script gets the “yurl” parameter of your query, decodes it, and launches the magic shell script. Call it “launch.php”
<?php
try {
exec("get_youtube ".urldecode($_GET['yurl']));
header( 'Location: http://THE_ADDRESS_OF_YOUR_VOLUMIO/youtube.php' ) ;
} catch (Exception $e) {
echo "Exception : ", $e->getMessage(),"
";
}
?>
Don’t forget to update “THE_ADDRESS_OF_YOUR_VOLUMIO” to the correct address / IP of your device.
3.3. Move the scripts to /var/www
Execute the command :
$ sudo mv youtube.php /var/www
$ sudo mv launch.php /var/www
4. Profit : enjoying the sound !
4.1 Go to youtube, grab a video
4.2 Copy the url
4.3 Go to THE_ADDRESS_OF_YOUR_VOLUMIO/youtube.php
4.4 Paste, click go
4.5 The video should be in your playlist, with a horrible name !
Hope that helps !