LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting - sed, lame, etc. (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-sed-lame-etc-796709/)

Jinouchi 03-20-2010 12:53 PM

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?

nonamenobody 03-20-2010 01:11 PM

Have you tried sed 's/_/\\ /g | sed 's/^/"/' | sed 's/$/"/' to put it in quotes? Might work

Jinouchi 03-20-2010 01:42 PM

Quote:

Originally Posted by nonamenobody (Post 3905711)
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.

penguiniator 03-20-2010 01:50 PM

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.

Kenhelm 03-21-2010 01:01 AM

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


grail 03-21-2010 02:11 AM

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.

nonamenobody 03-21-2010 04:37 AM

Quote:

for f in * ; do lame $f $f.mp3
Are there any spaces in f? or are the spaces only in pwd?

w1k0 03-21-2010 11:46 AM

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#'`


Jinouchi 03-21-2010 11:36 PM

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.

grail 03-22-2010 01:15 AM

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

w1k0 03-22-2010 05:03 AM

Quote:

Originally Posted by Jinouchi (Post 3907133)
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).


All times are GMT -5. The time now is 01:18 AM.