Search code examples
perlencodingmp3

MP3::Tag - UTF-16 unrecognized BOM


I am writing a small perl script to re-tag my MP3 collection based on the filenames.

#!/usr/bin/perl

use strict;
use warnings;
use MP3::Tag;
use File::Find;

MP3::Tag->config(write_v24 => 1);
my $dirpath = "../MP3s/";
finddepth(\&wanted, $dirpath);
sub wanted {
    unless (-d $_) {
        my ($track,$artist,$title);
        if(($track,$artist,$title) = ($_ =~ m/(\d+) - (.+?) - (.+)\.mp3$/g)){
            #handle songs with a track number
            my $mp3 = MP3::Tag->new($_) or die $!;

            $mp3->track_set($track);
            $mp3->artist_set($artist);
            $mp3->title_set($title);
            $File::Find::dir =~ m/.*\/(.*)/;
            $mp3->album_set($1);
            $mp3->update_tags();
            $mp3->close();
            print "$track - $artist - $title\n";
        } elsif(($artist,$title) = ($_ =~ m/(.+?) - (.+)\.mp3$/g)){
            #handle songs without a track number
            my $mp3 = MP3::Tag->new($_) or die $!;

            $mp3->track_set("");
            $mp3->artist_set($artist);
            $mp3->title_set($title);
            $mp3->update_tags();
            $mp3->close();
            print "$artist - $title\n";
        }
    }   
}

This works fine, but there are some files which make the script crash with this error: UTF-16:Unrecognised BOM 3100 at C:/strawberry/perl/lib/Encode.pm line 175.

What causes this error? The filename doesn't have special characters like german umlauts (ä,ö,ü).

How can I resolve this problem or skip such files?


Solution

  • You should start by adding use Carp::Always that will give you a stack trace showing you which statement in your own program was fatal.