![]() |
mp3 ID3タグ読込・書込ツール(Perlスクリプト) mp3 ID3 tag read/write tool (Perl Script) |
目次 |
MP3::Tagライブラリ モジュールを用いて、mp3ファイルのID3v1/ID3v2タグの読み書きなどを行うコマンドラインツール。多数のファイルの一括処理を行うときに、スクリプトをカスタマイズして使うための参考例として使えます。
Microsoft Windows Media Playerのバグ仕様では、ID3v2標準仕様のUTF-16エンコード書き込みしたものは受け付けない。UTF-16LE + BOMエンコードで書きこむ必要がある。MP3::Tag::ID3v2のソースコードによれば
# 0 = latin1 (effectively: unknown) # 1 = UTF-16 with BOM (we always write UTF-16le to cowtow to M$'s bugs) # 2 = UTF-16be, no BOM # 3 = UTF-8 my @dec_types = qw( iso-8859-1 UTF-16 UTF-16BE utf8 ); my @enc_types = qw( iso-8859-1 UTF-16LE UTF-16BE utf8 ); my @tail_rex; # Actually, disable this code: it always triggers unsync... my $use_utf16le = $ENV{MP3TAG_USE_UTF_16LE}; @enc_types = @dec_types unless $use_utf16le;
となっており、スクリプトを実行するシェルの環境変数にMP3TAG_USE_UTF_16LE
が設定されていればよい。この状態で _add_frame サブルーチンで UTF-16LE + BOM付きで書き込まれる。
if ($fs->{encoded}) { if ($encoding) { # 0 = latin1 (effectively: unknown) # 1 = UTF-16 with BOM (we write UTF-16le to cowtow to M$'s bugs) # 2 = UTF-16be, no BOM # 3 = UTF-8 require Encode; if ($calc_enc or $encode_utf8) { # e_u8==1 by default $d = Encode::encode($enc_types[$encoding], $d); } elsif ($encoding < 3) { # Reencode from UTF-8 $d = Encode::decode('UTF-8', $d); $d = Encode::encode($enc_types[$encoding], $d); } $d = "\xFF\xFE$d" if $use_utf16le and $encoding == 1; } elsif (not $self->{fixed_encoding} # Now $encoding == 0... and $self->get_config1('id3v2_fix_encoding_on_edit') and $e = $self->botched_encoding() and do { require Encode; Encode::decode($e, $d) ne $d }) { # If the current string is interpreted differently # with botched_encoding, need to unbotch... $self->fix_frames_encoding(); } } $datastring .= $d;
ちなみに、このソースコードでもうひとつ分かるのは、add_frameに渡す文字列はEncode::decodeで一旦utf8にデコードされるはずだが…。require Encode;
というライブラリの呼び出し記述で分かるとおり、日本語に特化した呼び出し方法を取っていないので日本語はうまくデコードできない。つまり、add_frameに渡す文字列は予めutf8にデコードしてから渡さなければいけない。
GNU GPL フリーソフトウエア