Play soundtracks of Youtube videos with Volumio

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 :slight_smile:

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 !

The extracted YouTube audio URL wouldn’t play. Soundcloud and Bandcamp did work.
I used your idea as a starting point to integrated it with the Volumio web interface. I am now able to paste a URL which will be downloaded and added to the Volumio playlist. Since the file is downloaded it is also possible to set a name using the title of the YouTube video/Soundcloud song.
With my setup it is also possible to delete downloaded files or clear the download folder completely!
It was a great way of exploring the Volumio interface and it’s functions.
VolumioPlay1.png
VolumioBrowse2.png

Amazing! I am now thinking on a way to integrate this seamlessly with the UI…
Thanks!

Just wanted to add my +1 for this feature. I listen to a lot of SoundClound streams, so it would be awesome to have some way of adding streams to the playlist of Volumio :slight_smile:

is this still work on volumio 1.5/rpi ?
i’ve done all step, and the youtube content shown on the playlist correctly.
however, i can’t play it.
when i clicked the youtube content in the playlist, it didn’t work, and just switch to the next song queued .

by the way, where will this script save the audio file converted from youtube?

and i was stuck in writing the bash script, get_youtube.
because it won’t work by saved as DOS format.
so, i tried to convert it to the unix format, and use nano to save it as mac format.
on these way, i succeed to let the youtube content shown on my playlist of volumio.
but as i said on the last message, it cant be played.
am i wrong on this step?

Personaly I use allstream on my Android smarphone or tablette to catch the sound of the youtube app and redirect it on volumio in airplay.

thx for your reply.
may i ask how to separate the sound direction out from the youtube address?

May I please ask how you managed to integrate this?

As shell script alternative to play youtube video (or other sites, democracynow.org/ works), you can use

mplayer -vo null -quiet $(youtube-dl -g “$1”)

$1 being the URL to play.
-vo null suppress video output
-quiet makes it less interactive

PS: a firefox add on to “send this page/link to pi” to play would be very nice …

Nice! To bad it doesnt work with youtube urls (since youtube does not allow http). Does anyone has a solution for this?

i wrote a very basic chrome extension that adds does a get request to launch.php (so it adds the url to mpd)

to make it work you have to change the ip adress of you mpd server in popup.js
to add it: goto chrome://extensions/
check developer mode, and add the (uncompressed) folder
mpd.zip (41.2 KB)

I’am using now from GitHub the new yt-dl api ytdl.info

well, I am late, I saved some videos from Utube but they have no sound on the VLC player, so I wanna know Volumio will save me or not? my friend sent me this page http://www.videoconverterfactory.com/tips/save-youtube-videos.html but this program can not support Mac os, I know this maybe a good tool too, so any similar program recommend? My budget is under 50$, please

Hey guys, have You heard about this new app?

You can find STUMBLER app on Google play. It is an amazing app that:

[i][b]- is daily updated

  • has great content
  • has amazing community of people
  • provides only the best content
  • follows all of the current subjects
  • it is 100% FREE to use[/b][/i]

Find us on Google play - Stumbler: funny videos, memes & viral videos