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 02-10-2007, 12:37 PM   #1
muddywaters
Member
 
Registered: May 2005
Location: Winnipeg, Canada
Distribution: mostly mepis
Posts: 427

Rep: Reputation: 30
Best tool for this script


I'm working (okay, goofing around ) on a bash script to rip selected cd tracks, encode to mp3 and get the track info from freedb.

So far it works ok resulting in a directory with mp3 files named
new_track#.cdda.wav
and a text file with lines like
TTitle#=<name of track>

What tool would be best be able to rename the mp3 files with the corresponding track names from the text file? ie;
<name of track>.mp3

Just playing around with this really. It's a cold snap here and cabin fever is setting in.
 
Old 02-10-2007, 01:42 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Perl.

http://www.perl.org/docs.html
 
Old 02-10-2007, 03:09 PM   #3
muddywaters
Member
 
Registered: May 2005
Location: Winnipeg, Canada
Distribution: mostly mepis
Posts: 427

Original Poster
Rep: Reputation: 30
thanks wjevans

I have seen perl scripts that do this and agree they are very elegant. However in this case it's more about the journey. ie: determined to use bash no matter how ugly it turns out.

That plus my perl knowledge ends at "Hello World"
*Not to say my bash knowledge goes much deeper.
 
Old 02-11-2007, 06:12 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
If you want to stay away from Perl, then investigate the more tricky aspects of sed, including the regular expression stuff.

Good luck!
 
Old 02-11-2007, 08:49 AM   #5
muddywaters
Member
 
Registered: May 2005
Location: Winnipeg, Canada
Distribution: mostly mepis
Posts: 427

Original Poster
Rep: Reputation: 30
Maybe the progress so far will make the question clearer

Code:
#!/bin/bash
# Rips a range of tracks at chosen bit rate
# + finds titles from freedb.org

if [ -z $3 ]; then
	echo "Usage: mp3rip <first track> <last track> <bitrate>"
	echo "example:"
	echo "mp3rip 1 3 192"
	exit 1
fi

echo "Tracks will be placed in music/<new directory>"
echo "Please enter a name for this new directory now"
echo
read a

mkdir $HOME/music/"$a"
cd $HOME/music/"$a"

cdparanoia -B "$1-$2"

for file in *.wav; do
lame --preset $3 $file new_$file
done

# output takes the pattern new_track##.cdda.wav

rm track??.cdda.wav

# flatout plagarized the next 5 lines

DISCID=$( cd-discid /dev/cdrom )
cddbcmd -m http cddb query $DISCID > query.txt
CATEG=$( cut -d ' ' -f 1 ./query.txt )
SERIAL=$( cut -d ' ' -f 2 ./query.txt)
cddbcmd -m http cddb read $CATEG $SERIAL > cdinfo.txt

rm query.txt

# freedb numbers from 0
# strip everything except song title

TRACK1=$(cat cdinfo.txt | grep TTITLE0 | sed 's/TTITLE0=//')
TRACK2=$(cat cdinfo.txt | grep TTITLE1 | sed 's/TTITLE1=//')
TRACK3=$(cat cdinfo.txt | grep TTITLE2 | sed 's/TTITLE2=//')
TRACK4=$(cat cdinfo.txt | grep TTITLE3 | sed 's/TTITLE3=//')
TRACK5=$(cat cdinfo.txt | grep TTITLE4 | sed 's/TTITLE4=//')
TRACK6=$(cat cdinfo.txt | grep TTITLE5 | sed 's/TTITLE5=//')
TRACK7=$(cat cdinfo.txt | grep TTITLE6 | sed 's/TTITLE6=//')
TRACK8=$(cat cdinfo.txt | grep TTITLE7 | sed 's/TTITLE7=//')
TRACK9=$(cat cdinfo.txt | grep TTITLE8 | sed 's/TTITLE8=//')
TRACK10=$(cat cdinfo.txt | grep TTITLE9 | sed 's/TTITLE9=//')
TRACK11=$(cat cdinfo.txt | grep TTITLE10 | sed 's/TTITLE10=//')
TRACK12=$(cat cdinfo.txt | grep TTITLE11 | sed 's/TTITLE11=//')
TRACK13=$(cat cdinfo.txt | grep TTITLE12 | sed 's/TTITLE12=//')

if [ -a new_track01.cdda.wav ]; then
	mv new_track01.cdda.wav "$TRACK1".mp3
fi
obviously more "ifs" are needed and something in place for failed lookups, but the script works.

The thing is it's very clumsy looking. How would the renaming be done if a cd contained 100's of tracks rather than <20 ?

Last edited by muddywaters; 02-11-2007 at 08:51 AM.
 
Old 02-11-2007, 09:00 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you use KDE, you could save yourself a lot of work if you type in "audiocd:/" in the konqueror file browser.
It will look up the CDDB data and produce an OGG and MP3 psuedo directory. You only have to drag the MP3 files you want and drop them into the target directory to start the transcoding process.

You may need to install a KDE package for the audiocd:/ protocol.
 
Old 02-11-2007, 04:38 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
TRACK1=$(cat cdinfo.txt | grep TTITLE0 | sed 's/TTITLE0=//')
TRACK2=$(cat cdinfo.txt | grep TTITLE1 | sed 's/TTITLE1=//')
...
if [ -a new_track01.cdda.wav ]; then
	mv new_track01.cdda.wav "$TRACK1".mp3
fi
...
You can use a loop:
Code:
#missing second expression means loop forever unless there's a break statement
for((tnum=0; ; tnum++))
do
   NEXT_TRACK="$(sed -n "s/^TTITLE$tnum=\(.*\)\$/\1/ p" cdinfo.txt)"
   #if TTITLE$tnum was not found, then exit the loop
   if [ -z "$NEXT_TRACK" ] ; then break; fi
   mv "new_track$tnum.cdda.wav" "$NEXT_TRACK.mp3"
done
This isn't a great way since sed ends up having to read the entire cdinfo.txt file once for every track, but it does work. It also assumes that there is no "hole" in the numbering of the tracks, and that they start from 0.
 
Old 02-12-2007, 03:33 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you want to play around i would suggest perl or python.
it's not really a shell sed task. they would not scale well.

python has mp3 libraries aplenty and it's a lovely language.
perl would be good too as you can still use external utilities easily.

if it was work i'd use perl, for fun i'd have a go at python.
 
Old 02-12-2007, 05:59 AM   #9
muddywaters
Member
 
Registered: May 2005
Location: Winnipeg, Canada
Distribution: mostly mepis
Posts: 427

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by bigearsbilly
if it was work i'd use perl, for fun i'd have a go at python.
Will give that a go as I know a bit of python. Just enough to be dangerous really Expect more questions.

And yeah, it's for fun. I have no ambition of writing the next great ripping app.

ntubski

Thanks for the loop. Think I'll use that and then put this bash script on the backburner for a while.
 
Old 02-13-2007, 05:07 AM   #10
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
One reason I often advocate perl over python or ruby is that a perl script is more valuable to your employer in the long run, because more people know it.
 
Old 02-13-2007, 05:29 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I agree totally.
perl is always going to be there if you want to earn money.

I'd love to have a real go at python but unfortunately there's not a lot
of work in it.
(I have no hope of finding lisp work at all )
 
Old 02-14-2007, 06:50 AM   #12
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
bigearsbilly, try googling this:

Code:
lisp jobs
You'll get links like this:

http://lispjobs.wordpress.com/
http://www.alu.org/table/mail.htm#jobs

Last edited by wjevans_7d1@yahoo.co; 02-14-2007 at 06:51 AM.
 
  


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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Tool (setup script?) for re-writing /etc/fstab? MikTheUser Slackware 5 07-11-2004 09:12 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM
URLSCAN tool MS = Linux tool ? OB1 Linux - Security 3 10-05-2002 12:58 AM

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

All times are GMT -5. The time now is 07:55 AM.

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