Register
It is currently Sat Oct 25, 2014 9:40 pm

mp3 resample script???


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Wed Dec 31, 2008 4:54 am   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
Hello everyone

I am very new to bash scripting and for now I realy need some help to get this done.
Imagine you have an mp3 collection. All handmade ( nicely tagged, 320kb/s, no dupes and so on). Now you have an mp3-player of only 8GB space, which is not enough for all of them.
What I need to to is resample them down to 96kb/s.

Code:
lame -b 96 inputfilename outputfilename


does the resampling and

Code:
id3cp  inputfilename outputfilename


does the copien of the IDTags.

I can't do it quiete yet without help...

Eventually I want a script that creates a "resmpled" directory at a given path where it puts the new mp3 s.

Can this be done?

Here is what I managed to get working:

Code:
find ./ -true -exec lame -b 128 {} \;


TIA :)


Top
 Profile  
 PostPosted: Thu Jan 01, 2009 1:09 pm   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
Anybody out here willing to give advice?

Please help :roll:


Top
 Profile  
 PostPosted: Thu Jan 01, 2009 2:50 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
if [ -z $1 ]; then
   echo "Usage: $0 /path/to/files /path/to/resampled/files";
   exit
fi
if [ ! -d $1 ]; then
   echo "$1 does not exist"
   exit
fi

[ -d $2 ] || mkdir -p $2
for i in $(find -type f -iname $1/*.mp3); do
   lame -b 96 $i $2/$i
   id3cp  $i $2/$i
done


This script will resample all files in the directory supplied as the first argument and put them (and create it if it doesn't exist) in the directory specified in the second argument.

./script.sh /home/fredrik/mp3 /home/fredrik/mp3/resampled

You can ofcourse do this with a oneliner but this one is more foolproof :)

Code:
find ./ -type f -iname \*.mp3 -exec mkdir resampled && lame -b 128 {} resampled/{} && id3cp {} resampled/{} \;


That might work, but i'm not entirely sure :)

(ps. Both of those solutions will also do this recursively, note that all the files will end up in the same directory when resampled)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Thu Jan 01, 2009 8:02 pm   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
thank you :)

sadly I get an error executing your script:

Code:
02:53 kai@desktop ~/Desktop/alicia_keys -> /home/kai/Desktop/mp3_forum.sh
Usage: /home/kai/Desktop/mp3_forum.sh /path/to/files /path/to/resampled/files
02:58 kai@desktop ~/Desktop/alicia_keys ->


with my new version i cant set the OUTPUTFILENAME properly using the variables

Code:
#!/bin/bash

echo "Where to create new \"small-mp3-directory\"?"
echo "Enter whole path now:"
read DEST_DIR
if [ -d "$DEST_DIR" ]; then
        echo
else
        echo "This directory does not exist, try again"
        exit
fi



echo "Where are your mp3s located?"
echo "Enter whole path now:"
read MP3_DIR
if [ -d "$MP3_DIR" ]; then
        echo
else
        echo "This directory does not exist, try again"
        exit
fi

mkdir  $DEST_DIR/downsampled
mkdir  $DEST_DIR/temp_orig
cp -Rv $MP3_DIR $DEST_DIR/temp_orig
cd     $DEST_DIR/temp_orig

FILES="*"

for f in "$FILES"
do
        echo "Processing $f file..."
        find ./ -true -exec lame -b 128 {} $DEST_DIR/downsampled{} \;
done

#rm -Rv $DEST_DIR/temp_orig


not to mention that the id3cp thing is not even in there yet.

What did I do wrong?

Thank you :)


Top
 Profile  
 PostPosted: Fri Jan 02, 2009 1:50 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
latenite wrote:
thank you :)

sadly I get an error executing your script:

Code:
02:53 kai@desktop ~/Desktop/alicia_keys -> /home/kai/Desktop/mp3_forum.sh
Usage: /home/kai/Desktop/mp3_forum.sh /path/to/files /path/to/resampled/files
02:58 kai@desktop ~/Desktop/alicia_keys ->


...What did I do wrong?

Thank you :)


You didn't execute Fredrick's script correctly, and you got the "error" message telling you HOW to execute it properly. Looks to me like it should work perfectly if you execute it right.

./mp3_forum.sh /path/to/files /path/to/resampled/files

His script takes 2 arguments, and you didn't feed it any and got the error.


Top
 Profile WWW  
 PostPosted: Fri Jan 02, 2009 6:24 am   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
Sorry but I am very new to scripting....

adding the 2 paths I still get an error:

Code:
01:17 kai@desktop ~/Desktop -> ./mp3_forum.sh /home/kai/Desktop/alicia_keys /home/kai/Desktop/123
find: warning: Unix filenames usually don't contain slashes (though pathnames do).  That means that '-iname "/home/kai/Desktop/alicia_keys/*.mp3"' will probably evaluate to false all the time on this system.  You might find the '-wholename' test more useful, or perhaps '-samefile'.  Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ "/home/kai/Desktop/alicia_keys/*.mp3"'.
01:18 kai@desktop ~/Desktop ->


How can I get the script I wrot to work? What is wrong with the outputlifepath I provided? How do I ad the id3cp line?

Thank you all


Top
 Profile  
 PostPosted: Fri Jan 02, 2009 7:28 am   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
by the way...and for understanding
waht do you test for with the -z flag?

Code:
if [ -z $1 ]; then


thanks


Top
 Profile  
 PostPosted: Fri Jan 02, 2009 9:17 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
latenite wrote:
by the way...and for understanding
waht do you test for with the -z flag?

Code:
if [ -z $1 ]; then


thanks


-z $1 checks if the variable $1 is empty or not. (-z stands for "zero")
to be precise it checks if something is trailing -z and is before ].

if $1 is empty (that means you haven't added an argument to the script) then the real if statement will look like this.
if [ -z ]; then
making it a boolean true and if $1 isn't empty
if [ -z whatever-your-first-argument-said ]; then
making it a boolean false.

Code:
#!/bin/bash
if [ -z $1 ]; then
   echo "Usage: $0 /path/to/files /path/to/resampled/files";
   exit
fi
if [ ! -d $1 ]; then
   echo "$1 does not exist"
   exit
fi

[ -d $2 ] || mkdir -p $2
for i in $(find $1 -type f -iname \*.mp3); do
   lame -b 96 $i $2/$i
   id3cp  $i $2/$i
done


Sorry about that error, I wasn't really thinking when i wrote that. The above version should work better :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Jan 02, 2009 11:13 am   

Joined: Wed Dec 31, 2008 4:39 am
Posts: 6
I practiced a little, taking your ideas into account.
I came up with this:

Code:
#!/bin/bash

SRC_DIR=$1
DEST_DIR=$2


if [ -d $SRC_DIR ]; then
        cd $SRC_DIR
        NUMBER_OF_MP3=`find ./ -type f -iname *.mp3* | wc -l`
        echo $NUMBER_OF_MP3 "mp3s will be resampled"
        echo "This will take approx." $[($NUMBER_OF_MP3 * 30)/60] minutes.
else
        echo "Woring path to mp3 source directory, try again"
        exit
fi


if [ -d $DEST_DIR ]; then
        if [ -d $DEST_DIR/downsampled ]; then
                rm -r  $DEST_DIR/downsampled
                mkdir  $DEST_DIR/downsampled
        else
                mkdir  $DEST_DIR/downsampled
        fi
else
        echo "\"$DEST_DIR\" directory does not exist, try again"
        exit
fi

cd $SRC_DIR

COUNTER=$NUMBER_OF_MP3

for i in $(find ./ -type f -iname *.mp3* | sed 's/.//'); do
        echo processing $i
        echo $COUNTER mp3 remaining...
        mkdir -p $DEST_DIR$i
        lame -b 128 $SRC_DIR$i $DEST_DIR$i
        id3cp $SRC_DIR$i $DEST_DIR$i
        COUNTER=$[ $COUNTER - 1 ]
done


But the line
Code:
mkdir -p $DEST_DIR$i

still need some twaeking I cant come up with... saing I need to cut the filename, so I only create the directories.

How will I do this.

Thank you ...by the way...your script doesn' work over here :roll: I get:

Code:
06:04 kai@desktop ~/Desktop -> /home/kai/Desktop/mp3_forum_2.sh /home/kai/Desktop/alicia_keys /home/kai/Desktop/123
ID3v2 found. Be aware that the ID3 tag is currently lost when transcoding.
Can't init outfile '/home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/03_how_come_you_dont_call_me.mp3'
Parsing /home/kai/Desktop/alicia_keys/songs_in_a_minor/03_how_come_you_dont_call_me.mp3: done.  Copying to /home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/03_how_come_you_dont_call_me.mp3: done
ID3v2 found. Be aware that the ID3 tag is currently lost when transcoding.
Can't init outfile '/home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/01_piano_i.mp3'
Parsing /home/kai/Desktop/alicia_keys/songs_in_a_minor/01_piano_i.mp3: done.  Copying to /home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/01_piano_i.mp3: done
ID3v2 found. Be aware that the ID3 tag is currently lost when transcoding.
Can't init outfile '/home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/02_girlfriend.mp3'
Parsing /home/kai/Desktop/alicia_keys/songs_in_a_minor/02_girlfriend.mp3: done.  Copying to /home/kai/Desktop/123//home/kai/Desktop/alicia_keys/songs_in_a_minor/02_girlfriend.mp3: done
06:13 kai@desktop ~/Desktop ->


Top
 Profile  
 PostPosted: Mon Jan 26, 2009 4:44 am   

Joined: Mon Jan 26, 2009 4:40 am
Posts: 3
Hey guys did you find a solution?

Im just wondering if this change needs to bee made:

for i in $(`find $1 -type f -iname \*.mp3`); do


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP