Kodi Community Forum

Full Version: [Script] bash mp3 youtube-dl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a small script that runs well on debian. I wanted to share with you and see (I do not know python)
if it was possible to "wear" to xbmc and make it a plugin.
The script uses youtube-dl and echonest.

The Goal is, get what you hear (for example on webradio, tv or other...).

For now I run with advanced launcher.
Its purpose is to recover and copy at a given location, music mp3.

To do this, he listens to 20 seconds the sound that comes from the sound card (and therefore not annoying noise)
it produces an audio fingerprint and sending to echonest to see if it exists in the database,
echonest back, returns the title and the artist youtube-dl handles download and convert,
finally the mp3 is placed in my choice ... on nas for example.

A video is worth a thousand words (/ \ 100MB!)
http://www.filedropper.com/vid20140607081049

Below the bash script and the install script I tried to automate dependencies (to be test):

Code:
#!/bin/bash
#SHAZTUBE
#Licence WTFPL
# this piece of dirty program listen to the sound card output for 20sec,
# create an audio fingerprint, make a query with the footprint at echonest
# and if it finds a match, he takes the title and the artist, download video youtube, convert to mp3 and copy on a nas or other :)

# prerequisite run the script install.sh normally:
# youtube-dl package available if sudo wget http://youtube-dl.org/downloads/latest/youtube-dl
# echoprint-codegen: to create audio fingerprint [url] https://github.com/echonest/echoprint-codegen [/ url]
# a api key for echonest: http://developer.echonest.com/api
# wget, curl, xcowsay, ffmpeg

# OUTPUT MUST BE AT LEAST 50% IN GOOD RESULTS
# the hardest part is to walk cfm (capture the sound coming out of the card) but it's worth it :)
#cf [url]https://carthick.wordpress.com/2007/11/26/linux-recording-soundcard-output-using-arecord/[/url]




#*************************************#
#---------Variables---------#


#echonest key
key="XXXXXXXXXXXXXXXXXXXXXXXX"

#destination
nas="/media/nas"

#Sound capture device ou pcm (par défaut)
output="pcm.copy"

#---------Variables à remplir---------#
#*************************************#









clear
echo ""
echo ""
echo ""
echo ""
echo ""
echo "***********************************************"
echo " Listening.... 20sec.... "
echo "***********************************************"
echo ""
echo ""
xcowsay --think "Listening ..."
arecord -V mono -q -d 20 -c 2 -f S16_LE -r 44100 -t wav -D $output find.wav
echo ""
echo ""
./echoprint-codegen-master/echoprint-codegen find.wav 0 20 > find.json
find=`cat find.json`
rm answer
echo ""
echo "***************************"
echo " Asking ..."
echo "***************************"
xcowsay --think "Je demande ..."
curl -s -X POST -H "Content-Type:application/octet-stream" "http://developer.echonest.com/api/v4/song/identify?api_key=$key" --data-binary "$find" > answer

artiste=`cat answer | sed 's/,/\n/g' | grep "artist_name" | sed 's/^.*://g' | sed 's/"//g' | sed 's/&//g' |sed s/\'/\/`
titre=`cat answer | sed 's/,/\n/g' | grep "title" | sed 's/^.*://g' | sed 's/"//g' | sed 's/&//g' |sed s/\'/\/`


if test -z "$artiste"
then clear && echo ""
echo ""
echo ""
echo "********"
echo " Fail !"
echo "********"
echo ""
xcowsay --think "Fail !" && sleep 2 && exit;fi

echo ""
echo ""
echo "***********"
echo " WIN !!!"
echo "***********"
echo ""
xcowsay --think "WIN !!!"
echo titre: "$(youtube-dl --get-title ytsearch:"$artiste""$titre")"

clear
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "                           Oo...><)))°>.............oO"
echo ""
echo "                            Artist : $artiste"
echo "                            Title   :  $titre"
echo ""
echo "                           Oo...><)))°>.............oO"
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
#notify-send -u normal  -t 0 "Downloading" "$titre de $artiste"
xcowsay --think "Downloading $titre de $artiste"
#update youtube-dl
#youtube-dl -U

artisteok=`echo "$artiste" | sed 'y/áàâäçéèêëîïìôöóùúüñÂÀÄÇÉÈÊËÎÏÔÖÙÜÑ/aaaaceeeeiiiooouuunAAACEEEEIIOOUUN/'`
titreok=`echo "$titre" | sed 'y/áàâäçéèêëîïìôöóùúüñÂÀÄÇÉÈÊËÎÏÔÖÙÜÑ/aaaaceeeeiiiooouuunAAACEEEEIIOOUUN/'`

#fonction DL to mp3
echo "Downloading..."

youtube-dl -o "$nas/"'%(title)s.%(ext)s' -x --audio-format mp3 --audio-quality 3 ytsearch:"$artisteok""$titreok"
xcowsay --think "$titre of $artiste has been added to destination :)"
exit 0

Script install :

Code:
#!/bin/bash

echo "Going to install:
xcowsay
curl
unzip
wget
echoprint (http://developer.echonest.com/api)([url]https://github.com/echonest/echoprint-codegen[/url])
youtube-dl"

sudo apt-get install -y xcowsay curl youtube-dl unzip wget

command -v youtube-dl >/dev/null 2>&1 || { echo >&2 "Je n'ai pas réussi à installer youtube-dl, installez le manuellement svp. ( http://youtube-dl.org/downloads/latest/youtube-dl"; exit 1; }



mkdir shaztube && cd shaztube
sudo apt-get install ffmpeg libboost-dev libtag1-dev zlib1g-dev
wget [url]https://github.com/echonest/echoprint-codegen/archive/master.zip[/url]
unzip master.zip
rm master.zip

cd echoprint-codegen-master
./configure
make
sudo make install

echo "
pcm.copy {
type plug
slave {
pcm hw
}
route_policy copy
}" >> ~/.asoundrc
chmod +x shaztube-0.1
clear
echo "Now get an API echonest KEY"
echo "https://developer.echonest.com/account/register"
echo "And copy the key in the script shaztube-0.1"
echo "If everything is good enjoy..."
read -p

I'm not a developper, just sharing Smile