2011年12月15日木曜日

FLACファイルのReplayGain情報を利用して音量正規化(ノーマライズ)

前提となる環境は以下のようなものです。

  • Windows7 Home Premium 64bit
  • 音楽ファイルをPCではFLAC、携帯プレーヤー(COWON S9)ではOgg Vorbisで管理
  • FLACファイルに付与したReplay Gain情報を利用して、Replay Gain非対応のS9でも音量を正規化したい

音源によって異なる音量を一定に保って再生する方法として、Replay Gainがあります。これは音楽ファイルにタグとして音量を記録しておくもので、再生側が対応している必要があります。PCであれば対応ソフトウェアがけっこうありますが、携帯機器となると話は別。私の使っているCOWON S9は未対応…。

もちろんReplay Gain以外の方法で音量正規化をかけることもできます。有名なのはMP3Gainでしょうか。ただこれは対応ファイル形式が.mp3限定。AACGainなるものもあったりしますが、私の使ってるFLACとOgg Vorbisに対応してるものは少ないです。ないことはないんですが。

で、ふと「音量正規化するソフトがあるんだったら、エンコーダに正規化機能が付いてるんじゃないか」と思って調べてみました。

Vorbis.comからコマンドラインエンコーダをダウンロードして、コマンド

oggenc2.exe -h
でオプションを調べてみます。今回はOggenc2.87 using aoTuVb6.03のx64版を使わせてもらいました。出力はこんな感じ。
OggEnc v2.87 (aoTuV Beta 6.03)
(c) 2000-2005 Michael Smith 
 & portions by John Edwards 

Usage: oggenc2 [options] input.wav [...]

OPTIONS:
 General:
 -Q, --quiet          Produce no output to stderr
 -h, --help           Print this help text
 -k, --skeleton       Adds an Ogg Skeleton bitstream
 -r, --raw            Raw mode. Input files are read directly as PCM data
 -F, --raw-format=n   Set format for raw input. Default is 1 for
                      Standard PCM; use 3 for IEEE Float.
 -B, --raw-bits=n     Set bits/sample for raw input. Default is 16,
                      32 assumed for IEEE Float.
 -C, --raw-chan=n     Set number of channels for raw input. Default is 2
 -R, --raw-rate=n     Set samples/sec for raw input. Default is 44100
 --raw-endianness     1 for bigendian, 0 for little (defaults to 0)
 -b, --bitrate        Choose a nominal bitrate to encode at. Attempt
                      to encode at a bitrate averaging this. Takes an
                      argument in kbps. By default, this produces a VBR
                      encoding, equivalent to using -q or --quality.
                      See the --managed option to use a managed bitrate
                      targetting the selected bitrate.
 --managed            Enable the bitrate management engine. This will allow
                      much greater control over the precise bitrate(s) used,
                      but encoding will be much slower. Don't use it unless
                      you have a strong need for detailed control over
                      bitrate, such as for streaming.
 -m, --min-bitrate    Specify a minimum bitrate (in kbps). Useful for
                      encoding for a fixed-size channel.
 -M, --max-bitrate    Specify a maximum bitrate in kbps. Useful for
                      streaming applications.
 --advanced-encode-option option=value
                      Sets an advanced encoder option to the given value.
                      The valid options (and their values) are documented
                      in the man page supplied with this program. They are
                      for advanced users only, and should be used with
                      caution.
 -q, --quality        Specify quality between -2 (low) and 10 (high),
                      instead of specifying a particular bitrate.
                      This is the normal mode of operation.
                      Fractional qualities (e.g. 2.75) are permitted
                      The default quality level is 3.
 --resample n         Resample input data to sampling rate n (Hz)
 -S, --converter      Specify the resampling engine to be used.
                      Options are: 0 (Best), 1 (Medium) & 2 (Fast).
 --downmix            Downmix stereo to mono. Only allowed on stereo
                      input.
 --scale n            Scale input data to n (n = between 0.00 and 1.00)
 --FLAC-scale n       Scale input data using FLAC Replaygain Tags where:
                      c = 1 to use Title Replaygain value, and
                      c = 2 to use Album Replaygain value.
 -s, --serial         Specify a serial number for the stream. If encoding
                      multiple files, this will be incremented for each
                      stream after the first.
 --discard-comments   Prevents comments in FLAC and Ogg FLAC files from
                      being copied to the output Ogg Vorbis file.
 --ignorelength       Ignore the datalength in wav headers. This will allow
                      support for files > 4GB and STDIN data streams. 

 Naming:
 -o, --output=fn      Write file to fn (only valid in single-file mode)
 -n, --names=string   Produce filenames as this string, with %a, %t, %l,
                      %n, %d replaced by artist, title, album, track number,
                      and date, respectively (see below for specifying these).
                      %% gives a literal %.
 -X, --name-remove=s  Remove the specified characters from parameters to the
                      -n format string. Useful to ensure legal filenames.
 -P, --name-replace=s Replace characters removed by --name-remove with the
                      characters specified. If this string is shorter than the
                      --name-remove list or is not specified, the extra
                      characters are just removed.
                      Default settings for the above two arguments are platform
                      specific.
 --utf8               Tells oggenc that the command line parameters date,
                      title, album, artist, genre, and comment are already in
                      UTF8. On windows, this switch applies to file names too.
 -c, --comment=c      Add the given string as an extra comment. This may be
                      used multiple times. The argument should be in the
                      format "tag=value".
 -d, --date           Date for track (usually date of performance)
 -N, --tracknum       Track number for this track
 -t, --title          Title for this track
 -l, --album          Name of album
 -a, --artist         Name of artist
 -G, --genre          Genre of track
 -L, --lyrics         Include lyrics from given file (.srt or .lrc format)
 -Y, --lyrics-language  Sets the language for the lyrics
                      If multiple input files are given, then multiple
                      instances of the previous five arguments will be used,
                      in the order they are given. If fewer titles are
                      specified than files, OggEnc will print a warning, and
                      reuse the final one for the remaining files. If fewer
                      track numbers are given, the remaining files will be
                      unnumbered. For the others, the final tag will be reused
                      for all others without warning (so you can specify a date
                      once, for example, and have it used for all the files)

INPUT FILES:
 OggEnc input files must currently be 32, 24, 16, or 8 bit PCM WAV, AIFF, or
 AIFF/C files, or 32 bit IEEE floating point WAV. Files may be mono or stereo
 (or more channels) and any sample rate.
 ALSO with this version, you may input FLAC, MONKEYS AUDIO, WAVPACK, LAPC,
 OPTIMFROG and SHORTEN files. You MUST ensure that the appropriate
 encoder/decoder (i.e., flac.exe, etc) is in the same dir/folder as
 oggenc2.exe, or is in the defined path.
 Alternatively, the --raw option may be used to use a raw PCM data file, which
 must be 16bit stereo little-endian PCM ('headerless wav'), unless additional
 parameters for raw mode are specified.
 You can specify taking the file from stdin by using - as the input filename.
 In this mode, output is to stdout unless an output filename is specified
 with -o

注目は

--FLAC-scale
オプションです。FLACファイルのReplay Gain情報をもとに出力ファイルの音量を調整してくれます。
--FLAC-scale 1
ならTrack Gain,
--FLAC-scale 2
とすればAlbum Gainを使ってくれると。これはエンコード時に直接音量を調整するため、Replay Gain非対応の機器でも有効。たとえば音質q=5, Album Gainを利用して音量正規化をかけたいときはこんな風に。
oggenc2.exe -q 5 --FLAC-scale 2 "input.flac" -o "output.ogg"

このコマンドをちまちま打っていくのも面倒なので、バッチで処理しました。動作は以下。

  1. 起動されたディレクトリに存在する.flacファイルを調べ、ファイル名をlist.datに保存。
  2. list.datからファイル名を一つずつ読み出してエンコーダに投げる。出力ファイル名は入力ファイル名の拡張子を.flacから.oggに変更したもの。
  3. 最後にlist.datを消去
バッチはこんな感じ。もっとスマートに書けるだろうけど、実用上問題ないから気にしない。ファイル名にダイアクリティカルマークが含まれてるとうまくいきませんが、通常のアルファベットに置き換えるなりして対応すれば大丈夫です。環境変数OGGENC2_PATHにはoggenc2.exeのパスを設定してます。クオリティ指定はq=10ですが、これでもFLACよりは小さくなります。
setlocal
dir *.flac /b/a-d > list.dat
for /f "DELIMS=" %%A in ( list.dat ) do (
%OGGENC2_PATH%\oggenc2.exe -q 10 --FLAC-scale 2 "%%A" -o "%%~nA.ogg"
)
del list.dat
endlocal

2012年5月2日追記

今使っているバッチを載せておきます。for命令文に再帰処理オプション/rを指定して、バッチファイルの置かれたディレクトリ以下のFLACファイルをすべてOGG Vorbisに変換します。エンコーダ本体は“C:\oggenc2.87-aoTuVb6.03-x64\oggenc2.exe”に置きました。

setlocal
@echo off
for /r %%A in ( *.flac ) do (
C:\oggenc2.87-aoTuVb6.03-x64\oggenc2.exe -q 6 --FLAC-scale 2 "%%~A" -o "%%~dpnA.ogg"
)
endlocal

オマケ

コマンドラインエンコーダ動作中の画面表示。

0 件のコメント:

コメントを投稿