You are not logged in.

#1 2012-02-29 22:52:33

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,731
Website

help using find/xargs to apply mp3gain to files [solved]

I need to apply mp3gain (album mode) to all mp3 files in a given directory.  Each album is in its own directory under /media/data/music/albums for example:

/media/data/music/albums/foo
/media/data/music/albums/bar
/media/data/music/albums/more

What needs to happen is:

cd /media/data/music/albums/foo && mp3gain -a -k -p *.mp3
cd /media/data/music/albums/bar && mp3gain -a -k -p *.mp3
cd /media/data/music/albums/more && mp3gain -a -k -p *.mp3

I want to use find and xargs to do this but am unsure how to craft the right syntax.  I have used the this construct to accomplish tasks like this before, but this won't do both the cd into the dir and then run the mp3gain command.

find /media/data/music/album -type d -print0 | xargs -0 cd {} && mp3gain -a -k -p *.mp3

Thanks!

Last edited by graysky (2012-03-01 21:07:28)

Offline

#2 2012-03-01 01:15:36

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: help using find/xargs to apply mp3gain to files [solved]

Why do you need the "cd"? This seems to be working okay for me:

find ~/Music/Nile -type f -regex ".*\.mp3$" -exec mp3gain -a -k -p {} +

Offline

#3 2012-03-01 01:23:15

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,731
Website

Re: help using find/xargs to apply mp3gain to files [solved]

Hmm... without the cd command, I believe you can't use 'album' mode since mp3gain understands all files in a given directory are part of the same album.

Offline

#4 2012-03-01 01:36:50

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: help using find/xargs to apply mp3gain to files [solved]

what's wrong with a for loop? It seems you probably don't want recursion here, so the value of find is diminished:

for d in */; do
  (cd "$d"; mp3gain -a -k -p *.mp3)
done

Offline

#5 2012-03-01 06:26:55

nsb
Member
From: Switzerland
Registered: 2008-03-26
Posts: 57

Re: help using find/xargs to apply mp3gain to files [solved]

There exists execdir, which does the same as exec, but the command is executed from the subdirectory. I've never tried it though.

Offline

#6 2012-03-01 13:38:50

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: help using find/xargs to apply mp3gain to files [solved]

nsb wrote:

There exists execdir, which does the same as exec, but the command is executed from the subdirectory. I've never tried it though.

Correct, but you won't be able to make a glob expand properly without starting up a shell in that directory:

find /foo/bar/baz -type d -execdir bash -c 'mp3gain -a -k -p *.mp3' _ {} \;

...at which point, you're forced to create a new shell for each invocation. You could do it all at once:

find /foo/bar/baz -type d -exec bash -c 'for d in "$@"; do pushd "$d"; mp3gain -a -k -p *.mp3; popd; done' _ {} +

...but then you're just doing the exact same thing as the straight up for loop if its only a single directory deep.

Offline

#7 2012-03-01 21:06:53

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,731
Website

Re: help using find/xargs to apply mp3gain to files [solved]

I like the idea of a for loop.  Here is a variation on the theme:

find /media/data/music/albums -type d | while read dir; do cd "$dir" && mp3gain -a -k -p *.mp3; done

Offline

Board footer

Powered by FluxBB