LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple youtube url to mp3 script (https://www.linuxquestions.org/questions/programming-9/simple-youtube-url-to-mp3-script-719150/)

number-g 04-14-2009 08:06 PM

simple youtube url to mp3 script
 
i wrote a little script in TCL earlier to rip the audio from youtube urls directly to mp3 files.

i thought someone might find it useful so here you go. if this is the wrong place to post it, please move it to the right one.

depends on:
tcl
tclcurl (tcl bindings for libcurl)
clive (a youtube video ripper)
mplayer (media player, no need for gui)
lame (an mp3 encoder)

and the "nice" and "file" commands found on most unix systems

example use:

Code:

robot@mothership:~$ ./youtube http://www.youtube.com/watch?v=G-PQbdmQRwc
/tmp/1239755798ytmp3/Stan_Rogers_Barrett's_Privateers.mp3
MPEG ADTS, layer III, v2,  64 kBits, 22.05 kHz, JntStereo

the output could be used by another process to find the file and send it to someone/thing automatically (and delete it afterwards). i was thinking of making a web interface to it for example.

here's the (very basic) code; i think it could do with a few more safety measures in generating the filename:

Code:

#!/usr/bin/tclsh

package require TclCurl

set path "/tmp"
set folder "[clock seconds]ytmp3"        ;# give output folder a unique name
set path "$path/$folder"                ;# output folder

set nice "/usr/bin/nice"                ;# path to nice binary
set niceN 19                                ;# nice level. 19 is lowest, -20 is highest -- use with caution
set clive "/usr/bin/clive"                ;# path to clive binary
set mplayer "/usr/bin/mplayer"                ;# path to mplayer binary
set lame "/usr/bin/lame"                ;# path to lame binary

global nice; global niceN; global clive; global mplayer; global lame; global path


proc title {url} {                        ;# get title of youtube page for filename
 
 set handle [curl::init]
 $handle configure -url $url -bodyvar url
 $handle perform
 $handle cleanup

regsub -all "<title>" $url ` url
regsub -all "</title>" $url ` url
set url [split $url `]
set url [lindex $url 1]
set url [string replace $url 0 9]
regsub -all " " $url "_" url
regsub -all "&" $url "and" url
regsub -all \\\( $url "" url
regsub -all \\\) $url "" url

return $url
}

proc ytmp3 {url} {                        ;# do the work

global nice; global niceN; global clive; global mplayer; global lame; global path

set title [title $url]

file mkdir $path
cd $path

exec $nice -n $niceN $clive -q --format=flv -O $title.flv $url
catch {exec $nice -n $niceN $mplayer -really-quiet -novideo -ao pcm:file=$title.wav $title.flv} ;# mplayer occasionally gives non fatal errors which break the script
        file delete $title.flv
exec $nice -n $niceN $lame -S $title.wav $title.mp3
        file delete $title.wav
puts $path/$title.mp3
#puts [exec /usr/bin/file -b $path/$title.mp3] ;# this is optional
}

ytmp3 $argv

you could probably write this just as easily in bash, but i dont know much bash.

hope someone finds this useful


All times are GMT -5. The time now is 09:38 AM.