You are not logged in.

#1 2008-01-28 03:44:42

fflarex
Member
Registered: 2007-09-15
Posts: 466

[solved] help with a bash script

I'm trying to write a bash script to add Replay Gain to my FLAC collection, and I think I've hit a wall with a seemingly simple bash task. You don't need to know anything about Replay Gain or FLAC to help me, just a working knowledge of bash scripting.

Okay, so if I were to do this manually for each album (it has to be on a per-album basis, not per-track and not my whole collection at the same time), here's what would happen:

cd album_directory
metaflac --add-replay-gain *.flac

And that's it. Pretty simple, but I'm having a hard time figuring out how to cd into each directory and run a command. (It really is a shame that flac isn't as smart as vorbisgain, which performs this task trivially.) Here's my script so far:

#!/bin/bash

# Script to add replaygain tags to flac files
# (The current flac encoder cannot guess albums by folder like vorbisgain)

top_dir=/media/cuboid/music/

function gain {
        cd && metaflac --add-replay-gain *.flac
}

find $top_dir -mindepth 2 -type d -print0 | xargs -0 gain

Obviously, the gain function is where I need help. I greatly appreciate it.

Last edited by fflarex (2008-01-28 04:56:09)

Offline

#2 2008-01-28 03:51:15

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: [solved] help with a bash script

Shouldn't this do? metaflac --add-replay-gain */*.flac

Offline

#3 2008-01-28 03:56:32

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [solved] help with a bash script

Well, the album folders are 2 levels in, not 1, but I'm not sure I understand what that does.

Would that execute metaflac for each folder individually or would it do my entire collection at the same time (which would mess up the albumgain tags)?

Offline

#4 2008-01-28 04:10:58

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: [solved] help with a bash script

Ahh I see the complication now (I don't have that program/not familiar with gain either).

So let's say it looks like
$topdir/genre_piano/album_foobar/song_{1..10}.flac

for album in */*; do ([ -d "$album" ] && cd "$album" && metaflac --add-replay-gain *.flac); done

the subshell "(...)" should make cd'ing back redundant.

Offline

#5 2008-01-28 04:33:01

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [solved] help with a bash script

Well, I'm not sure why that works but I'll try it and see. I don't understand why you don't need to cd back (although I can see how if you did cd back each time, it would just keep going to the same folder and not get anywhere).

EDIT: nvm, I get it now. I'm about to find out if it works...

Last edited by fflarex (2008-01-28 04:36:06)

Offline

#6 2008-01-28 04:43:03

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [solved] help with a bash script

Hallelujah! Thanks, it works perfectly... now I just need to wait like 16 hours or something crazy like that for it to finish running.

EDIT: my favorite part of the whole thing is that it works in alphabetical order... so many programs seem to work in order of creation date, giving me next to no clue how far along it is.

Last edited by fflarex (2008-01-28 04:52:38)

Offline

#7 2008-01-28 04:57:00

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: [solved] help with a bash script

If it's consuming too much CPU, you can always renice it.

Offline

#8 2008-01-28 04:59:42

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [solved] help with a bash script

No, there's no way for it to avoid using lots of CPU. It analyzes the audio of every single song.

EDIT: I misunderstood what you just said. I've never used renice before. In other news, I think I've edited more of my posts in this single thread than all my other posts combined.

Last edited by fflarex (2008-01-28 05:04:47)

Offline

#9 2008-01-28 05:09:00

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: [solved] help with a bash script

But that doesn't mean your Firefox has to become unresponsive. A pgrep firefox and renice -8, and pgrep "for album" and renice +10 might be just the cure it wants.

Offline

#10 2008-01-28 17:30:48

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: [solved] help with a bash script

The other day I was playing Counter Strike Source when a new wine version was released.
I always play CSS at nice -8, and I compiled the new version with nice +10. Both are very consuming processes, but I didn't notice a thing in-game. I thought that was pretty cool.

Offline

#11 2008-01-28 22:10:18

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: [solved] help with a bash script

it's just scheduling priority. don't see how games can be affected. only -20 and 20 will make significant difference. used to compile on gentoo with -10 though..a good compromise.


I need real, proper pen and paper for this.

Offline

Board footer

Powered by FluxBB