LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2009, 08:06 PM   #1
number-g
LQ Newbie
 
Registered: Mar 2009
Distribution: Debian
Posts: 16

Rep: Reputation: 2
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Youtube-dl - Download videos from Youtube in openSUSE LXer Syndicated Linux News 1 08-14-2008 08:10 AM
Help with youtube script snowman81 Programming 9 03-09-2008 10:43 PM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
MP3,YouTube,Macromedia Flash player problem!!!!!!!!!!!!!!!!!!!!!! LinuxNewbie999 Linux - Newbie 28 11-06-2006 11:13 PM
How do i in XMMS play other formats than mp3 in url? Schopenhauer101 Linux - Software 3 02-01-2005 03:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration