SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-08-19 03:24:18

Superchompu
Member
Registered: 2012-07-24
Posts: 12

How To - Youtube to Mp3

Hi everyone...
Well.. I made this script to get Mp3 fast and easy
It uses

  • lynx

  • youtube-dl

  • ffmpeg

  • lame

  • mp3gain (it doesn use it, but its great for normalize the volume on the tracks)

(you can go sudo apt-get install any of this programs easy... )

you save this bash script (something like "GetTrack"...) then do

chmod +x GetTrack

then

sudo mv GetTrack /usr/bin/

and then you can run it anywhere with just typing GetTrack , then it will ask you to put an artist name and song... and then the script uses lynx to get youtube links... then it download one, then extract the audio, then convert it to mp3...

#!/bin/bash
#*********************
#
#    Variables
#
#*********************
artista="artista"
cancion="cancion"
archivo="archivo"

#*********************
#
#    Searching Downloading and converting
#
#*********************

#artista + cancion + archivo = archivo.l
function getList() {
    lynx -dump -listonly http://www.youtube.com/results?search_query="${1}"+"${2}">>${3}.rl
    grep http://www.youtube.com/watch?v= ${3}.rl >> ${3}.rl1
    rm ${3}.rl
    cut -b 1-6 --complement ${3}.rl1 >> ${3}.rl2
    rm ${3}.rl1
    uniq ${3}.rl2 >> ${3}.rl3
    rm ${3}.rl2
    grep -v \& ${3}.rl3 >> ${3}.l
    rm ${3}.rl3
}

#link + archivo = archivo.ytb
function download() {
    youtube-dl ${1} -o ${2}.ytb
}

#archivo = archivo.wav
function extractAudio() {
    ffmpeg -i ${1}.ytb -ac 2 ${1}.wav
}

#archivo = archivo.mp3
function toMp3() {
    lame -b 320 ${1}.wav ${1}.mp3
}

#*********************
#
#    MAIN
#
#*********************
index=`expr index "${lista}" \|`
read -p "Band: " artista
read -p "Song: " cancion
archivo=$(echo "${artista} ${cancion}" | sed -e 's/ /_/g')
if [ -f "${archivo}".mp3 ]
then
    echo "Ya existe!"
    break
fi
getList "$artista" "$cancion" "$archivo"
for link in $(cat ${archivo}.l)
do
    download $link $archivo
    if [ -f "${archivo}.ytb" ]
    then
        extractAudio $archivo
        if [ -f "${archivo}.wav" ]
        then
            toMp3 $archivo
            if [ -f "${archivo}.mp3" ]
            then
                rm ${archivo}.l
                rm ${archivo}.ytb
                rm ${archivo}.wav
                break
            else
                rm ${archivo}.ytb
                rm ${archivo}.wav
            fi
        else
            rm ${archivo}.ytb
        fi
    fi
done

then i recommend using mp3gain -r -T *.mp3 on your music folder..

Last edited by Superchompu (2012-08-19 19:22:01)


What we wanted to preserve was not just a good environment in which to do programming, but a system around which fellowship could form. We knew from experience that the essence of communal computing, as supplied by remote-access, time-shared machines, is not just to type programs into a terminal instead of a keypunch, but to encourage close communication. -Dennis Ritchie

Offline

Help fund CrunchBang, donate to the project!

#2 2012-08-19 03:38:30

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

Re: How To - Youtube to Mp3

^ Great idea! I've been using youtube-dl to download tracks, and always found it annoying to open a web browser and search blabla....

Anyway thanks for taking the code out of my brain and making it happen I guess! smile


Punch all your friends.

Offline

#3 2012-08-19 03:43:53

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

Let's test this out...

Run the script on it's own and it will ask for an Artist and Song?

Or run it with the artist name and song with it?

Please provide an example on how it should run

I have installed everything and saved this file, made it an executable as muzic.sh

When I run it as

sh muzic.sh

I get this error

muzic.sh: 18: muzic.sh: Syntax error: "(" unexpected

I get the same error if I run it as

sh muzic.sh Artist Song

and as

sh muzic.sh "Cheap Trick" "Flame"

VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#4 2012-08-19 06:53:14

Superchompu
Member
Registered: 2012-07-24
Posts: 12

Re: How To - Youtube to Mp3

VastOne wrote:

Let's test this out...

Run the script on it's own and it will ask for an Artist and Song?

Or run it with the artist name and song with it?

Please provide an example on how it should run

I have installed everything and saved this file, made it an executable as muzic.sh

When I run it as

sh muzic.sh

I get this error

muzic.sh: 18: muzic.sh: Syntax error: "(" unexpected

I get the same error if I run it as

sh muzic.sh Artist Song

and as

sh muzic.sh "Cheap Trick" "Flame"

hi, you should run it like this

bash nameOfFile

then it shouldn't be any problems


What we wanted to preserve was not just a good environment in which to do programming, but a system around which fellowship could form. We knew from experience that the essence of communal computing, as supplied by remote-access, time-shared machines, is not just to type programs into a terminal instead of a keypunch, but to encourage close communication. -Dennis Ritchie

Offline

#5 2012-08-19 13:28:37

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 9,250

Re: How To - Youtube to Mp3

^ Haven't tried this script yet, but since it declares /usr/bin/bash in the hashbang, shouldn't you be able to run it with just `nameofscript "artist" "title"`? hmm


while ( ! ( succeed = try() ) );
We've earned a reputation as a nice, friendly community; please help us keep it that way.

Offline

#6 2012-08-19 14:48:05

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

Superchompu wrote:

hi, you should run it like this

bash nameOfFile

then it shouldn't be any problems

Interesting... I have always been able to use sh filename instead of bash filename.

For those listening it is

bash muzic.sh

 

and the script then asks you for Band and Title to fill in..

Very nice script Superchompu!  cool


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#7 2012-08-19 15:03:47

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: How To - Youtube to Mp3

the error in Line 18 seems to be 'function getList()'
so i looked up differences between bash and sh, and it seems this has to do with the way of declaring a function (i think).
on many system /bin/sh is symlinked to /bin/bash. however, if i check my crunchbang, i see it is symlinked to /bin/dash. i remember seeing a thread about this somewhere around here, but okay.
so, trying to run 'sh <script>' doesn't work because you are calling dash, which seems to be a lightweight implementation of 'sh'. sh seems to have trouble with declaring functions, so it errors on the first function, Line 18.

still, should be possible to run

./muzic.sh

right?

i think the confusion stems from the weird habit to program in bash then give your file a .sh extension. lots and lots of people do that but i never really saw the point, as it is misleading and not necessary. when i add an extension i make it .bash but often i'll just leave out the extension altogether.
(no offense with this stuff to you Superchompu. still a great script)

Offline

#8 2012-08-19 15:07:01

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 9,250

Re: How To - Youtube to Mp3

I also tend to leave off the extension when I do my own shell scripts; it's just fewer characters to type, and I'm lazy like dat.

And this does look like a great, useful script.


while ( ! ( succeed = try() ) );
We've earned a reputation as a nice, friendly community; please help us keep it that way.

Offline

#9 2012-08-19 18:45:47

Superchompu
Member
Registered: 2012-07-24
Posts: 12

Re: How To - Youtube to Mp3

Hi, im glad people are enjoying it

I hace crunchbang statler, I have that script saved as "GetTrack" and I did chmod +x to it, and then i moved it to /usr/bin/

So i can just type GetTrack anywhere on the terminal and  run it...


What we wanted to preserve was not just a good environment in which to do programming, but a system around which fellowship could form. We knew from experience that the essence of communal computing, as supplied by remote-access, time-shared machines, is not just to type programs into a terminal instead of a keypunch, but to encourage close communication. -Dennis Ritchie

Offline

#10 2012-08-19 18:50:03

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

^ Thats just about what I did as well...

Thanks for this Superchompu, I think a few folks will enjoy it.

I have added it to Quick References and changed the title to How To - Youtube to MP3


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#11 2012-08-19 18:59:05

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

Superchompu wrote:

I have that script saved as "GetTrack" and I did chmod +x to it, and then i moved it to /usr/bin/

So i can just type GetTrack anywhere on the terminal and  run it...


I would suggest putting this information in the first post by editing it.  This way everyone will have just what they need from the first post.


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#12 2012-08-19 19:26:06

Superchompu
Member
Registered: 2012-07-24
Posts: 12

Re: How To - Youtube to Mp3

VastOne wrote:
Superchompu wrote:

I have that script saved as "GetTrack" and I did chmod +x to it, and then i moved it to /usr/bin/

So i can just type GetTrack anywhere on the terminal and  run it...


I would suggest putting this information in the first post by editing it.  This way everyone will have just what they need from the first post.


Done it... I also made two other scripts..... (one that let you choose the qualiity and shows the title of the youtube videos so you can choose.... and other one that when you call it you give it a file name, and then it uses that file to search download and extract that songs.... )

I will make them pretty and I'll post them...

Actually im thinking of putting that 3 scripts together, make it pretty, and then post it..

Last edited by Superchompu (2012-08-19 19:27:05)


What we wanted to preserve was not just a good environment in which to do programming, but a system around which fellowship could form. We knew from experience that the essence of communal computing, as supplied by remote-access, time-shared machines, is not just to type programs into a terminal instead of a keypunch, but to encourage close communication. -Dennis Ritchie

Offline

#13 2012-08-20 03:52:00

rstrcogburn
CrunchRanger
From: The Wild West
Registered: 2010-06-12
Posts: 1,798
Website

Re: How To - Youtube to Mp3

if you really want to make an mp3 out of anything, i recommend this

http://www.swview.org/blog/how-redirect … utput-file


... and a kind word.  -Duke

Offline

#14 2012-08-20 05:50:29

fratermus
#! CrunchBanger
From: 75081, USA
Registered: 2012-01-20
Posts: 119
Website

Re: How To - Youtube to Mp3

Thanks for the idea.

Inspired me to write one for my own needs that takes the artist, title, and YT URL (I want to give it a specific video).
Encodes to .ogg and tags it with the artitst/title info.


brother mouse
new to crunchbang.
my first linux kernel build was on a 386-16sx with 6MB SIPP RAM ($50/MB!)

Offline

#15 2012-08-20 05:56:23

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

fratermus wrote:

Thanks for the idea.

Inspired me to write one for my own needs that takes the artist, title, and YT URL (I want to give it a specific video).
Encodes to .ogg and tags it with the artitst/title info.

Mind sharing it?


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#16 2012-08-20 13:56:36

fratermus
#! CrunchBanger
From: 75081, USA
Registered: 2012-01-20
Posts: 119
Website

Re: How To - Youtube to Mp3

VastOne wrote:
fratermus wrote:

Thanks for the idea.

Inspired me to write one for my own needs that takes the artist, title, and YT URL (I want to give it a specific video).
Encodes to .ogg and tags it with the artitst/title info.

Mind sharing it?

first draft.  More useful as a something to get the juices flowing rather than for actual use.  But it does appear to work.

#!/bin/bash

# based on a script by superchompu
# http://crunchbanglinux.org/forums/post/252894

# requires:  vorbistools ffmpeg youtube-dl

#  sanity check
if [ -f ripit.ytb ]
then
echo "ripit.ytb exists.  Aborting."
ls -l ripit.*
exit
fi

#  input
echo ARTIST name:
read ARTIST

echo
echo Song name:
read TITLE

echo
echo Youtube URL:
read YTURL

#download it
youtube-dl ${YTURL} -o ripit.ytb

#extract nicely
nice ffmpeg -i ripit.ytb -ac 2 ripit.wav

#encode
nice  oggenc ripit.wav

#rename
#this is poorly thought out - FIXME
NEWNAME="$ARTIST - $TITLE - yt"
mv -v ripit.ogg "${NEWNAME}.ogg"
mv -v ripit.ytb "${NEWNAME}.ytb"

#tag
nice vorbiscomment -w -t "ARTIST=$ARTIST" -t "TITLE=$TITLE" "${NEWNAME}.ogg"

# show'em what they've won, Don Pardo
echo
ogginfo "${NEWNAME}.ogg"

# cleanup
rm ripit.wav

# keep the video
#mv -v *.ytb ~/videos

BN:  one can use

sh -x /path/to/script

or

bash -x /path/to/script 

to get the shell to output useful info, depending on one's chosen poison^H^H^H^ shell..  Not a debugger in the normal sense (gdb, etc) but better than nothing.


brother mouse
new to crunchbang.
my first linux kernel build was on a 386-16sx with 6MB SIPP RAM ($50/MB!)

Offline

#17 2012-08-23 02:50:18

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

fratermus, I have been away a couple of days and will check this out tomorrow..

Thanks! cool


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

#18 2012-08-23 04:23:35

johnraff
#!Drunkard
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 2,497
Website

Re: How To - Youtube to Mp3

rhowaldt wrote:

i think the confusion stems from the weird habit to program in bash then give your file a .sh extension. lots and lots of people do that but i never really saw the point, as it is misleading and not necessary.

I think the big problem is when people write a script that needs bash but give it a #!/bin/sh hashbang. If you want bash you should specifically call for it with #!/bin/bash.

About the extension, though, without being an expert, I'm not sure if using a .sh extension for bash scripts is any problem. bash is a shell too, after all. Could be wrong though, and of course any extension, or none, ought to be OK...

PS Superchompu thank you for this thread! A topic dear to my heart. cool


John
--------------------
( a boring Japan blog , and idle twitterings )

Offline

#19 2012-08-23 09:38:03

GamBit
#! CrunchBanger
From: Vienna/Austria
Registered: 2012-07-22
Posts: 104

Re: How To - Youtube to Mp3

I'm using http://clipgrab.de/.

Copy the Url in the browser and it is done.

Fast and easy.. smile

Offline

#20 2012-08-23 11:52:45

fratermus
#! CrunchBanger
From: 75081, USA
Registered: 2012-01-20
Posts: 119
Website

Re: How To - Youtube to Mp3

YT has been seeking takedown orders against sites that provide the service;  that was the source of my interest in making a local solution.


brother mouse
new to crunchbang.
my first linux kernel build was on a 386-16sx with 6MB SIPP RAM ($50/MB!)

Offline

#21 2012-08-23 12:31:01

GamBit
#! CrunchBanger
From: Vienna/Austria
Registered: 2012-07-22
Posts: 104

Re: How To - Youtube to Mp3

fratermus wrote:

a local solution.

Clipgrap is a local solution. A quick and easy download manager for youtube and all other online servives. smile

Offline

#22 2012-08-23 12:47:28

rhowaldt
#!*$%:)
Registered: 2011-03-09
Posts: 4,396

Re: How To - Youtube to Mp3

@johnraff: the extension itself indeed doesn't influence anything - where not on Windows anymore smile
however, i noticed VastOne attempting to execute the script by invoking 'sh', which was probably due to that extension.
so, it doesn't do harm, but it does add confusion, which is why i say 'stop making bashscripts and giving them a .sh name'.
also, while we're on the subject of naming conventions, people should stop making python scripts and naming them 'py-<something>' wink

Offline

#23 2012-08-23 13:43:27

Tim
#! CrunchBanger
From: SWeden
Registered: 2012-07-09
Posts: 142

Re: How To - Youtube to Mp3

This one i like alot, very simple!
Iam just running:
bash mp3
And it ask me for band, and song! Then done! Thank you very much, finaly i dont need to use my webbrowser and use a addon to do this. big_smile

Ofc i cd myself to the directory i want the music to be in, or make a folder.


Crunchy feeling #!

Offline

#24 2012-08-23 15:08:20

VastOne
#! Ranger
From: #! Fringe Division
Registered: 2011-04-26
Posts: 9,883
Website

Re: How To - Youtube to Mp3

johnraff wrote:

I think the big problem is when people write a script that needs bash but give it a #!/bin/sh hashbang. If you want bash you should specifically call for it with #!/bin/bash.

This was exactly the issue I had and did not realize it until looking closer at the code... 

Assumptions are always at a cost, yes... It is all good now.


VSIDO | LinuxBBQ
If you build it, they will come...
Words That Build Or Destroy

Offline

Help fund CrunchBang, donate to the project!

#25 2012-08-23 15:17:33

gurtid
#! Junkie
From: NEW ZEALAND
Registered: 2011-04-07
Posts: 370

Re: How To - Youtube to Mp3

@vastone - you have a pm


all your Base are belong to us

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo