Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: mp3gain - command line options for Linux (Read 20625 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

mp3gain - command line options for Linux

This n00b has a question about mp3gain.

I've been reading the various topics on mp3gain and learned a bit, but all of them rely on the Windows GUI. For example, this thread:

HydrogenAudio - How to use mp3gain

Seems to be the definitive guide here on the forums.

What I'm looking to do is to mimic those settings on the command line so I can create a Perl script (Linux user here) to automate the process of ripping, encoding, tagging, and normalizing.

I've played a bit, but after comparing results between the Windows GUI setting and the command line options (Both are using v1.4.6 as the backend) I'm not quite there. So far this is the closest I've come to replicating the Windows GUI:

mp3gain -a -k <filenames of the album>

Is that the closest I'm going to get, or can someone recommend a better combination of options?

Tristan

mp3gain - command line options for Linux

Reply #1
I'd also very much like to know the answer to the questions asked above. Anyone's got a different opinion, or should I use tphillips method?

mp3gain - command line options for Linux

Reply #2
I use -a -k as well. I also use -t but that shouldn't affect the gain calculation or end result in any way.

mp3gain - command line options for Linux

Reply #3
Just as a point of interest:

From what I can see, -a -k will apply album gain, to a level which means there is no clipping (max no-clip gain).  This is in keeping with the thread quoted.

However, using the GUI the "default" album-related action (IMHO) is to set all albums to an album gain of xx dB (89 by default).  Edit: I say "default" as I am thinking about ReplayGaining, etc. - i.e.: maintaining a collection of albums all at the same approximate album level.

I've looked at the code previously, and, IIRC, the GUI actually achieves this by running mp3gain.exe twice - once to find the max no-clip value (but apply no changes), and then again to actually set the gain, using the specified value and the no-clip value to create the correct offset - presumably using the -d  or -m switches ("/d <n> - modify suggested dB gain by floating-point n" or "/m <i> - modify suggested MP3 gain by integer i").  Edit: looking at it again, it may actually be -g ("/g <i>  - apply gain i to mp3 without doing any analysis") that is used on the second run.  I really should just look at the code again...

If I am right, and someone else is clever enough to create the two commands necessary (presumably using some batch/perl/pseudo script for the variables and calculation), these may be very useful also.

Edit: OK, I have just checked.  The first run uses the /o switch, the second run uses /g, e.g.:

mp3gain /o  "<file1>" "<file2>" "<file3>" ...

mp3gain /g -6 "<file1>"
mp3gain /g -6 "<file2>"
mp3gain /g -6 "<file3>"

... where -6 is the 'Recommended "Album" mp3 gain change for all files' (viewable if you just run mp3gain with no switches, or use "/o").  NB: this value isn't the db change, which is -9.580000 for the same album.

I assume, in theory, you could run with the /o switch, grab the "Album" value from the output (STDOUT), and then run n times with the /g switch and the value obtained...
I'm on a horse.

mp3gain - command line options for Linux

Reply #4
Using the logic Synthetic Soul extracted from the source code, it's not too hard to whip up a script to similate Windows gui album gain:

Code: [Select]
#!/usr/bin/perl

unless (@ARGV) {
   die "usage: $0 mp3file [ mp3file ... ]\n";
}

@quotedfiles = map { qq("$_") } @ARGV;

$listout = `mp3gain -o @quotedfiles`;

($albumgain) = ($listout =~ /^"Album"\s+(\S+)/m);

print "Will attempt to apply MP3 gain of $albumgain\n";

for $file (@quotedfiles) {
   $stdout = `mp3gain -g $albumgain $file`;  
}


However, the problem is that if the recommended album gain clips, it won't be detected by mp3gain -g. I can't seem to find a switch that will test to see if a gain clips before applying. If one exists, the gain could back down until the highest non-clipping gain is found.

mp3gain - command line options for Linux

Reply #5
What confuses me is the fact that no values are passed to the first call.

I can only assume it is using 89dB as the reference.

Quote
However, the problem is that if the recommended album gain clips, it won't be detected by mp3gain -g. I can't seem to find a switch that will test to see if a gain clips before applying. If one exists, the gain could back down until the highest non-clipping gain is found.

Could we just use the /k switch at the same time?  Would this effectively override the value specified by /g  - or maybe even be processed following the /g value is applied?

Or, would the value obtained from /o take this into consideration anyway?  Perhaps it strives for 89dB, but will go lower if required.

I don't have the knowledge to test any of these theories...
I'm on a horse.

mp3gain - command line options for Linux

Reply #6
Quote
Could we just use the /k switch at the same time?  Would this effectively override the value specified by /g  - or maybe even be processed following the /g value is applied?

Unfortunately /g seems to ignore the /k switch!

Quote
Or, would the value obtained from /o take this into consideration anyway?  Perhaps it strives for 89dB, but will go lower if required.

No, I tested this on a few of my albums that clip at 89dB, and the recommended album gain value from /o will still cause clipping. I might take a look at the source as well to see if there's some option that can provide this info (does track clip if I apply n gain).

mp3gain - command line options for Linux

Reply #7
After looking at the source code, the clipping detection formula is pretty easy one you have the max amplitude and album mp3 gain from the mp3gain -o output. So here's a second try at the script. If it detects clipping in any of the files when the gain is greater than 0, it will reduce the gain until there is no clipping.

Code: [Select]
#!/usr/bin/perl

unless (@ARGV) {
   die "usage: $0 mp3file [ mp3file ... ]\n";
}

$clipped = 0;

@quotedfiles = map { qq("$_") } @ARGV;

$listout = `mp3gain -o @quotedfiles`;

($albumgain) = ($listout =~ /^"Album"\s+(\S+)/m);
@maxsample = ($listout =~ /^.+?\t-*\d+\s+\S+\s+(\d+\.\d+)/mg);

print "Will attempt to apply MP3 gain of $albumgain\n";

if ($albumgain > 0) {
   for (0 .. $#maxsample) {
       while ($maxsample[$_] * (2**($albumgain/4)) > 32767) {
           $clipped = 1;
           $albumgain--;
       }
   }
}

if ($clipped) {
   print "Clipping detected - reducing MP3 gain to $albumgain\n";
}

if ($albumgain != 0) {
   for $file (@quotedfiles) {
       $stdout = `mp3gain -g $albumgain $file`;
   }
} else {
   print "No processing required for MP3 gain 0\n";
}

mp3gain - command line options for Linux

Reply #8
The only thing that springs to mind is:
  • Run -a -k to apply max no-clip value
  • Run -o to get Album gain
  • If gain is still negative value run -g
As applying gain is not a lossy process the above should have no issues, but will most likely result in gain being applied twice (so there will be a small increase in processing time).

Does this make sense?

I've just tried to test this but each time running -o after -a -k returned an Album gain of 0.  Surely that's not right?

Edit: NB: Written before I saw the above post.
I'm on a horse.

mp3gain - command line options for Linux

Reply #9
I'm on OS X and would also like to know the command line options (especially increasing to 92 over default 89). Haven't found anything on the web more than generic "-r -p -t -k", etc. Any help or links would be greatly appreciated.

mp3gain - command line options for Linux

Reply #10
I'm on OS X and would also like to know the command line options (especially increasing to 92 over default 89). Haven't found anything on the web more than generic "-r -p -t -k", etc. Any help or links would be greatly appreciated.


Hello H-Audio Members,

I am hoping you will be able to assist.

I have searched far and wide for more information about using MP3Gain command line switches to do the same as Blessingx has asked about (though I am interested in command line utilities on PC platform).

Basically - I am using EAC with Mareo - and I want MP3Gain to raise track levels to 92db rather than 89.

My understanding is that in the GUI - changing track level to 92db carries out an analysis by default.

However, the only switch I can see (and I am looking at the output of MP3Gain.exe /?  - is /g - but that states:

/g <i>  - apply gain i to mp3 without doing any analysis.

and also - I haven't been able to get this switch to work in the context of Mareo.


In contrast: The default parameters used in Mareo are:

PARAMETERS = /k /r /s r C:\Music\@artist@ - @album@\@track@. @title@.mp3

===

Just to be clear about what I am trying to acheive via the command line.

I am trying to acheive exactly the same effect as using the GUI - and setting the track gain to 92db.

Your help will be most appreciated.

Best,
Gingernona


mp3gain - command line options for Linux

Reply #12
If I wanted to set volume to 92 dB, I'd try altering the volume to 89 dB first, then running /g 2. (A 3-dB difference means two steps.)


Thank you kjoonlee - for your answer. I have been experimenting - and have got somewhere...

I am thinking that I do not understand dB - and how a step of two equates into 3-dB. I need to learn about it - and am wondering if there is somewhere you could point me - with relation to this particular issue.

Best,
Simon


mp3gain - command line options for Linux

Reply #14
Thanks kjoonlee.

Appreciated.

Best,
Gingernona

mp3gain - command line options for Linux

Reply #15
Question in general about mp3gain. If i run this script against several directories of mp3s at once, each dir a single album, will it compute the album gain for each individual album or for the entire set of files?

mp3gain - command line options for Linux

Reply #16
Question in general about mp3gain. If i run this script against several directories of mp3s at once, each dir a single album, will it compute the album gain for each individual album or for the entire set of files?


mp3gain (an executable binary, not a script) will consider all files that are presented on the command line as belonging to one single album. You could replaygain mp3's in different directories with this command:

find /media/music/albums -name "*.mp3" -execdir mp3gain "{}" +

This will run mp3gain once for each directory, presenting each time all the files in that directory, thus calculating albumgain for each directory.

The invocation in the command above will apply track and album replaygain tags. Only replay gain tag aware players will adjust the volume. If you need compatibility with players that do not recognize replaygain tags, you need to add the -a (apply album gain) option.

To deviate from the standard reference replaygain level, use the -m option.

mp3gain - command line options for Linux

Reply #17
Is there any difference between the solution posted by "chromium" (find ... -execdir mp3gain {}..) and "jth" (perl skript to avoid clipping)? Dont know if i understood what you are doing, just wanted to add gain-tags to my collection and started writing a python-Script calling the perl-Script of "jth" for every mp3-containing-folder in a specified location:

Code: [Select]
# -*- coding: utf-8 -*-          
#/usr/bin/python                

import sys
import os
import subprocess

def dirEntries(dir_name, subdir, *args):
    fileList = []                      
    for file in os.listdir(dir_name):
        dirfile = os.path.join(dir_name, file)
        if os.path.isfile(dirfile):
            if not args:
                fileList.append(dirfile)
            else:
                if os.path.splitext(dirfile)[1][1:] in args:
                    fileList.append(dirfile)
        # recursively access file names in subdirectories
        elif os.path.isdir(dirfile) and subdir:
            fileList.extend(dirEntries(dirfile, subdir, *args))
    return fileList

def dirNamesWithFiles(dir_name, subdir, *args):
    folderList = []
    for file in os.listdir(dir_name):
        dirfile = os.path.join(dir_name, file)
        if os.path.isfile(dirfile):
            if not args:
                folderList.append(os.path.dirname(dirfile))
            else:
                if os.path.splitext(dirfile)[1][1:] in args:
                    folderList.append(os.path.dirname(dirfile))
        # recursively access file names in subdirectories
        elif os.path.isdir(dirfile) and subdir:
            folderList.extend(dirNamesWithFiles(dirfile, subdir, *args))
    return folderList

perlPath='/home/n/applyMp3Gain.pl'
mp3Path='/home/n/Mp3-Files'

folders = set(dirNamesWithFiles(mp3Path, 1, 'mp3'))
for i in folders:
        subprocess.call(["/usr/bin/perl", perlPath] + dirEntries(i, 0, 'mp3'))


You need to adjust "perlPath" and "mp3Path", perlPath should point to the script posted above by "jth". This script will call the perl-Script once for every subfolder where it finds mp3-files (case sensitive on extension..).

Perhaps some googlers need the script. best regards.
nils

mp3gain - command line options for Linux

Reply #18
If I wanted to set volume to 92 dB, I'd try altering the volume to 89 dB first, then running /g 2. (A 3-dB difference means two steps.)


Further to this excellent suggestion by kjoonlee and also wanting to raise the db level in mp3gain using Mareo from EAC rips I modified the mareo.ini file thus:

; ---------------------------------------------------------------------------------------------------------------------------
; MP3gain: ALBUM Mode: mp3 normalizer: http://www.rarewares.org/mp3.html
; ---------------------------------------------------------------------------------------------------------------------------
EXECUTEIF = [1] = [TRACK]
FINALPATH = F:\[ARTIST]\[ALBUM]\
FINALNAME = [TRACKPADDED] - [ARTIST] - [TITLE]
VAFINALPATH = F:\AARemasters128\Various Artists\[ALBUM]\
VAFINALNAME = [TRACKPADDED] - [ARTIST] - [TITLE]
CUEFINALPATH = F:\Various Artists\[ALBUM]\
CUEFINALNAME = [ARTIST] - [YEAR] - [ALBUM]
EXTENSION = mp3
ENCODEREXE = C:\Program Files\MP3Gain\mp3gain.exe
PARAMETERS = /k /a /s r [FINALPATHSHORT]\*.mp3
RENAME = FALSE
;---------------------------------------------------------------------------------------------------------------------------
EXECUTEIF = [1] = [TRACK]
FINALPATH = F:\[ARTIST]\[ALBUM]\
FINALNAME = [TRACKPADDED] - [ARTIST] - [TITLE]
VAFINALPATH = F:\AARemasters128\Various Artists\[ALBUM]\
VAFINALNAME = [TRACKPADDED] - [ARTIST] - [TITLE]
CUEFINALPATH = F:\Various Artists\[ALBUM]\
CUEFINALNAME = [ARTIST] - [YEAR] - [ALBUM]
EXTENSION = mp3
ENCODEREXE = C:\Program Files\MP3Gain\mp3gain.exe
PARAMETERS = /g 2 /s r [FINALPATHSHORT]\*.mp3
RENAME = FALSE

running the reduce below clipping paramaters first and then re-run again to increase gain to the desired level and seems to work well so far.

No doubt there are more elegant solutions out there us noobs have yet to discover - but in the meantime thanks for the original post.