LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2010, 01:53 PM   #1
Jinouchi
Member
 
Registered: Jul 2007
Location: WI, USA
Distribution: Debian, Lenny
Posts: 111

Rep: Reputation: 19
Bash scripting - sed, lame, etc.


Ok, so I find myself ripping audio CDs frequently, which I then lame to mp3's to put on my media player. I usually define the --ta and --tl (artist and album) ID3 tags and batch encode each album, but don't bother with the track tags as I'd have to do each one seperately. So, I'm working on a script to do all this for me, extracting info from 'pwd' etc. to fill in the blanks for --ta, --tl and --tt (track name). All is working well, except that I can't get sed to pass on the "\" character to lame to escape spaces. Here's what I've got so far: (trouble spot is bolded - no need to pay attention to the rest of it)

Code:
for f in * ; do lame $f $f.mp3 --ta `(pwd | sed 's/.*\/\(.*\)\/.*\/.*/\1/')` --tl `(pwd | sed 's/.*\/\(.*\)\/.*/\1/')` --tt `(echo $f | sed 's/...\(.*\)/\1/' | sed 's/_/\\ /g')` ; done
All this does is pass a 'space' on to lame, which it takes as an invalid argument. I've spent a couple hours googling around, but haven't found a solution. Any ideas?

Last edited by Jinouchi; 03-20-2010 at 01:55 PM.
 
Old 03-20-2010, 02:11 PM   #2
nonamenobody
Member
 
Registered: Oct 2002
Posts: 138

Rep: Reputation: 22
Have you tried sed 's/_/\\ /g | sed 's/^/"/' | sed 's/$/"/' to put it in quotes? Might work
 
Old 03-20-2010, 02:42 PM   #3
Jinouchi
Member
 
Registered: Jul 2007
Location: WI, USA
Distribution: Debian, Lenny
Posts: 111

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by nonamenobody View Post
Have you tried sed 's/_/\\ /g | sed 's/^/"/' | sed 's/$/"/' to put it in quotes? Might work
Unfortunately, that didn't work either...

Code:
lame: excess arg Bright
Same thing as before.
 
Old 03-20-2010, 02:50 PM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
You may need to double up on your backslashes. Don't forget that the shell will parse backslashes as escapes for the following character before passing strings to your command. To prevent that, you might want to use single-quotes around the string passed to lame.
 
Old 03-21-2010, 02:01 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Try leaving out the '\' and add double quotes.
Code:
--tt  "`echo $f | sed 's/...\(.*\)/\1/' | sed 's/_/ /g'`"

# With most versions of sed it can be shortened to
--tt  "`echo $f | sed 's/...//; s/_/ /g'`"

# Another way is to use bash parameter expansion
title="${f#???}"       # Remove the first three characters of $f
--tt "${title//_/ }"   # Change all '_' in $title to spaces
 
Old 03-21-2010, 03:11 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Hi Jinouchi

I feel it would be easier to assist if you gave an example of input and what your probable output will look like.
 
Old 03-21-2010, 05:37 AM   #7
nonamenobody
Member
 
Registered: Oct 2002
Posts: 138

Rep: Reputation: 22
Quote:
for f in * ; do lame $f $f.mp3
Are there any spaces in f? or are the spaces only in pwd?
 
Old 03-21-2010, 12:46 PM   #8
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
As I guess that script should work for you:

Code:
#!/bin/sh
for f in *
do
    ta=`(pwd | sed 's#.*/\(.*\)/.*#\1#' | sed 's#_# #g')`
    tl=`(pwd | sed 's#.*/.*/\(.*\)#\1#' | sed 's#_# #g')`
    tt=`(echo $f | sed 's#...\(.*\).wav#\1#' | sed 's#_# #g')`
    echo \"$ta\"
    echo \"$tl\"
    echo \"$tt\"
    echo
done
I assume you have artists directories such as:

Albert_Ayler
Anthony_Braxton
Art_Ensemble_Of_Chicago

Then album directories such as:

Spirits
Spiritual_Unity
New_York_Eye_And_Ear_Control

And finally consecutive tracks such as:

01_Witches_And_Devils.wav
02_Spirits.wav
03_Holy,_Holy.wav

I'm not sure in which names you use underscores instead of spaces so I assumed you use them everywhere.

***

Of course to produce MP3 files from WAV ones you have to use the commands similar to:

Code:
    lame --ta "$ta" --tl "$tl" --tt "$tt" $f
    mv $f.mp3 `echo $f | sed 's#.wav#.mp3#'`

Last edited by w1k0; 03-21-2010 at 01:07 PM.
 
Old 03-22-2010, 12:36 AM   #9
Jinouchi
Member
 
Registered: Jul 2007
Location: WI, USA
Distribution: Debian, Lenny
Posts: 111

Original Poster
Rep: Reputation: 19
Ok, I haven't tested all the new replies yet, but I thought I'd do as grail suggested and demonstrate exactly what I'm doing.

Working directory:
Code:
/media/sdc1/music/three_days_grace/one_x/wav
Script: (I've enhanced it a little (forgive my ignorance of proper script formatting/techniques; w1k0's post has given me an example of how it's supposed to be done. I'll get it cleaned up soon)
Code:
for f in * ; do lame $f $f.mp3 --ta `(pwd | sed 's/.*\/\(.*\)\/.*\/.*/\1/' | sed 's/^.\|_[a-z]/\U&/g')` --tl `(pwd | sed 's/.*\/\(.*\)\/.*/\1/' | sed 's/^.\|_[a-z]/\U&/g')` --tt `(echo $f | sed 's/...\(.*\)/\1/' | sed 's/^.\|_[a-z]/\U&/g')` --ti ../art/art.* ; done
This works perfectly, except that it results in underscores in the ID3 tags. Example:
Code:
$ mplayer 01_its_all_over.mp3

(...)

Playing 01_its_all_over.mp3.
Audio only file format detected.
Clip info:
 Title: Its_All_Over
 Artist: Three_Days_Grace
 Album: One_X
 Year: 
 Comment: 
 Genre: Unknown

(...)
As far as filenames go, I like to keep everything lowercase and have no spaces. I'll dissect w1k0's post some more and see if I can get his method to work.
 
Old 03-22-2010, 02:15 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Hi Jinouchi

I would like to start by saying I know little to nothing about lame
But, assuming I understand the issue correctly, I believe this should work for you:

Code:
find /media/sdc1/music/ -name "*wav" | 
awk 'BEGIN{ FS = "/" } 
{ path = $0;
  sub($NF, "", path);
  wav = $NF;
  sub("wav", "mp3", wav);
  gsub("_", " ");
  name = $NF;
  sub("\.wav", "", name);
  system("lame", path $NF, path wav, "--ta", $(NF - 2), "--tl", $(NF - 1), "--tt", name}'
Edit: So good thing I thought about this a bit more, otherwise you would have your whole mp3 collection in one directory.
not very useful
I have also left the addition of the 'art' folder to you but I think that should be easily done

Last edited by grail; 03-22-2010 at 05:55 AM. Reason: Now places mp3s in correct folder
 
Old 03-22-2010, 06:03 AM   #11
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,252

Rep: Reputation: 205Reputation: 205Reputation: 205
Quote:
Originally Posted by Jinouchi View Post
Ok, I haven't tested all the new replies yet, but I thought I'd do as grail suggested and demonstrate exactly what I'm doing.
So try all of them.

Quote:
Script: (I've enhanced it a little (forgive my ignorance of proper script formatting/techniques; w1k0's post has given me an example of how it's supposed to be done. I'll get it cleaned up soon)
You can use one-liners but properly formatted script is easier to read.

Quote:
As far as filenames go, I like to keep everything lowercase and have no spaces. I'll dissect w1k0's post some more and see if I can get his method to work.
So try that script:

Code:
#!/bin/sh

for f in *
do lame $f $f.mp3 \
--ta "`(pwd | sed 's/.*\/\(.*\)\/.*/\1/' | sed 's/^.\|_[a-z]/\U&/g;s/_/ /g')`" \
--tl "`(pwd | sed 's/.*\/.*\/\(.*\)/\1/' | sed 's/^.\|_[a-z]/\U&/g;s/_/ /g')`" \
--tt "`(echo $f | sed 's/...\(.*\)/\1/' | sed 's/^.\|_[a-z]/\U&/g;s/_/ /g')`" \
#--ti ../art/art.*
done
(I commented --ti switch because I found nothing about it in man lame).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash scripting with sed not working fakie_flip Linux - Software 6 12-08-2009 02:56 AM
scripting help with sed n0gnus Linux - General 4 09-30-2008 12:01 PM
scripting with lame linuxhippy Slackware 6 03-20-2007 06:06 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 03:55 AM
Bash script for LAME LocoMojo Linux - Newbie 6 01-09-2005 07:41 PM


All times are GMT -5. The time now is 08:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration