| 1 | package File::Sticker::Scribe::Mp3; | ||
| 2 | $File::Sticker::Scribe::Mp3::VERSION = '4.9'; | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | File::Sticker::Scribe::Mp3 - read, write and standardize meta-data from MP3 file | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | version 4.9 | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use File::Sticker::Scribe::Mp3; | ||
| 14 | |||
| 15 | my $obj = File::Sticker::Scribe::Mp3->new(%args); | ||
| 16 | |||
| 17 | my %meta = $obj->write_meta(%args); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This will write meta-data from MP3 files, and standardize it to a common | ||
| 22 | nomenclature, such as "tags" for things called tags, or Keywords or Subject etc. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | 1 | ▶ | use common::sense; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 0 | ▶ | use File::LibMagic; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | use MP3::Tag; | ||
| 29 | |||
| 30 | use parent qw(File::Sticker::Scribe); | ||
| 31 | |||
| 32 | # FOR DEBUGGING | ||
| 33 | =head1 DEBUGGING | ||
| 34 | |||
| 35 | =head2 whoami | ||
| 36 | |||
| 37 | Used for debugging info | ||
| 38 | |||
| 39 | =cut | ||
| 40 | sub whoami { ( caller(1) )[3] } | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =head2 priority | ||
| 45 | |||
| 46 | The priority of this scribe. Scribes with higher priority get tried first. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub priority { | ||
| 51 | my $class = shift; | ||
| 52 | return 2; | ||
| 53 | } # priority | ||
| 54 | |||
| 55 | =head2 allowed_file | ||
| 56 | |||
| 57 | If this scribe can be used for the given file, then this returns true. | ||
| 58 | File must be an MP3 file. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub allowed_file { | ||
| 63 | my $self = shift; | ||
| 64 | my $file = shift; | ||
| 65 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 66 | |||
| 67 | my $ft = $self->{file_magic}->info_from_filename($file); | ||
| 68 | if ($ft->{mime_type} eq 'audio/mpeg') | ||
| 69 | { | ||
| 70 | say STDERR 'Scribe ' . $self->name() . ' allows filetype ' . $ft->{mime_type} . ' of ' . $file if $self->{verbose} > 1; | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | return 0; | ||
| 74 | } # allowed_file | ||
| 75 | |||
| 76 | =head2 allowed_fields | ||
| 77 | |||
| 78 | If this scribe can be used for the known and wanted fields, then this returns true. | ||
| 79 | For this scribe, this always returns true. | ||
| 80 | |||
| 81 | if ($scribe->allowed_fields()) | ||
| 82 | { | ||
| 83 | .... | ||
| 84 | } | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub allowed_fields { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | return 1; | ||
| 92 | } # allowed_fields | ||
| 93 | |||
| 94 | =head2 known_fields | ||
| 95 | |||
| 96 | Returns the fields which this scribe knows about. | ||
| 97 | |||
| 98 | my $known_fields = $scribe->known_fields(); | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub known_fields { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | return { | ||
| 106 | title=>'TEXT', | ||
| 107 | creator=>'TEXT', | ||
| 108 | author=>'TEXT', | ||
| 109 | composer=>'TEXT', | ||
| 110 | performer=>'TEXT', | ||
| 111 | description=>'TEXT', | ||
| 112 | genre=>'TEXT', | ||
| 113 | mood=>'TEXT', | ||
| 114 | song=>'TEXT', | ||
| 115 | url=>'TEXT', | ||
| 116 | year=>'NUMBER', | ||
| 117 | track=>'NUMBER', | ||
| 118 | tags=>'MULTI', | ||
| 119 | %{$self->{wanted_fields}} | ||
| 120 | }; | ||
| 121 | } # known_fields | ||
| 122 | |||
| 123 | =head2 read_meta | ||
| 124 | |||
| 125 | Read the meta-data from the given file. | ||
| 126 | |||
| 127 | my $meta = $obj->read_meta($filename); | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub read_meta { | ||
| 132 | my $self = shift; | ||
| 133 | my $filename = shift; | ||
| 134 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 135 | |||
| 136 | my $mp3 = MP3::Tag->new($filename); | ||
| 137 | my %meta = (); | ||
| 138 | |||
| 139 | my $known_fields = $self->known_fields(); | ||
| 140 | foreach my $field (sort keys %{$known_fields}) | ||
| 141 | { | ||
| 142 | if ($field eq 'title') | ||
| 143 | { | ||
| 144 | $meta{'title'} = $mp3->album(); | ||
| 145 | } | ||
| 146 | elsif ($field eq 'song') | ||
| 147 | { | ||
| 148 | $meta{'song'} = $mp3->title(); | ||
| 149 | } | ||
| 150 | elsif ($field eq 'description') | ||
| 151 | { | ||
| 152 | $meta{'description'} = $mp3->comment(); | ||
| 153 | if (!$meta{description}) # try the COMM field | ||
| 154 | { | ||
| 155 | $meta{$field} = $mp3->select_id3v2_frame_by_descr('COMM'); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | elsif ($field eq 'creator') | ||
| 159 | { | ||
| 160 | $meta{'creator'} = $mp3->artist(); | ||
| 161 | } | ||
| 162 | elsif ($field eq 'genre') | ||
| 163 | { | ||
| 164 | $meta{'genre'} = $mp3->genre(); | ||
| 165 | } | ||
| 166 | elsif ($field eq 'mood') | ||
| 167 | { | ||
| 168 | $meta{'mood'} = $mp3->select_id3v2_frame_by_descr('TMOO'); | ||
| 169 | } | ||
| 170 | elsif ($field eq 'year') | ||
| 171 | { | ||
| 172 | $meta{'year'} = $mp3->year(); | ||
| 173 | } | ||
| 174 | elsif ($field eq 'track') | ||
| 175 | { | ||
| 176 | $meta{'track'} = $mp3->track(); | ||
| 177 | } | ||
| 178 | elsif ($field eq 'composer') | ||
| 179 | { | ||
| 180 | $meta{'composer'} = $mp3->composer(); | ||
| 181 | } | ||
| 182 | elsif ($field eq 'performer') | ||
| 183 | { | ||
| 184 | $meta{'performer'} = $mp3->performer(); | ||
| 185 | } | ||
| 186 | elsif ($field eq 'author') | ||
| 187 | { | ||
| 188 | # author (as distinct from artist) use the 'composer' field | ||
| 189 | # This is used for podfic, whereas composer is used for music. | ||
| 190 | $meta{'author'} = $mp3->composer(); | ||
| 191 | } | ||
| 192 | elsif ($field eq 'url') | ||
| 193 | { | ||
| 194 | # get url | ||
| 195 | # official audio file webpage | ||
| 196 | my $value = $mp3->select_id3v2_frame_by_descr('WOAF'); | ||
| 197 | $meta{url} = $value if $value; | ||
| 198 | # official audio source webpage | ||
| 199 | $value = $mp3->select_id3v2_frame_by_descr('WOAS'); | ||
| 200 | $meta{url} = $value if !$meta{url} and $value; | ||
| 201 | } | ||
| 202 | else # freeform text fields | ||
| 203 | { | ||
| 204 | if ($mp3->have_id3v2_frame('TXXX', [$field])) | ||
| 205 | { | ||
| 206 | my $tagframe = $mp3->select_id3v2_frame('TXXX', [$field], undef); | ||
| 207 | $meta{$field} = $tagframe; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | # Delete any fields that are undefined | ||
| 211 | delete $meta{$field} if !defined $meta{$field}; | ||
| 212 | } | ||
| 213 | |||
| 214 | return \%meta; | ||
| 215 | } # read_meta | ||
| 216 | |||
| 217 | =head1 Helper Functions | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | =head2 replace_one_field | ||
| 222 | |||
| 223 | Overwrite the given field. This does no checking. | ||
| 224 | |||
| 225 | $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value); | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub replace_one_field { | ||
| 230 | my $self = shift; | ||
| 231 | my %args = @_; | ||
| 232 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 233 | |||
| 234 | my $filename = $args{filename}; | ||
| 235 | my $field = $args{field}; | ||
| 236 | my $value = $args{value}; | ||
| 237 | |||
| 238 | my $mp3 = MP3::Tag->new($filename); | ||
| 239 | $mp3->config(write_v24=>1); | ||
| 240 | |||
| 241 | if ($field eq 'title') | ||
| 242 | { | ||
| 243 | $mp3->album_set($value); | ||
| 244 | } | ||
| 245 | elsif ($field eq 'song') | ||
| 246 | { | ||
| 247 | $mp3->title_set($value); | ||
| 248 | } | ||
| 249 | elsif ($field eq 'description') | ||
| 250 | { | ||
| 251 | $mp3->comment_set($value); | ||
| 252 | } | ||
| 253 | elsif ($field eq 'creator') | ||
| 254 | { | ||
| 255 | $mp3->artist_set($value); | ||
| 256 | } | ||
| 257 | elsif ($field eq 'genre') | ||
| 258 | { | ||
| 259 | $mp3->genre_set($value); | ||
| 260 | } | ||
| 261 | elsif ($field eq 'mood') | ||
| 262 | { | ||
| 263 | $mp3->select_id3v2_frame_by_descr('TMOO', $value); | ||
| 264 | } | ||
| 265 | elsif ($field eq 'year') | ||
| 266 | { | ||
| 267 | $mp3->year_set($value); | ||
| 268 | } | ||
| 269 | elsif ($field eq 'track') | ||
| 270 | { | ||
| 271 | $mp3->track_set($value); | ||
| 272 | } | ||
| 273 | elsif ($field eq 'performer') | ||
| 274 | { | ||
| 275 | $mp3->select_id3v2_frame_by_descr('TPE1', $value); | ||
| 276 | } | ||
| 277 | elsif ($field eq 'composer') | ||
| 278 | { | ||
| 279 | $mp3->select_id3v2_frame_by_descr('TCOM', $value); | ||
| 280 | } | ||
| 281 | elsif ($field eq 'author') | ||
| 282 | { | ||
| 283 | # Use the 'composer' field | ||
| 284 | # This is used for podfic, whereas composer is used for music. | ||
| 285 | $mp3->select_id3v2_frame_by_descr('TCOM', $value); | ||
| 286 | } | ||
| 287 | elsif ($field eq 'url') | ||
| 288 | { | ||
| 289 | # official audio file webpage | ||
| 290 | $mp3->select_id3v2_frame_by_descr('WOAF', $value); | ||
| 291 | } | ||
| 292 | else | ||
| 293 | { | ||
| 294 | my $newval = $value; | ||
| 295 | if (ref $value eq 'ARRAY') | ||
| 296 | { | ||
| 297 | $newval = join(',', @{$value}); | ||
| 298 | } | ||
| 299 | $mp3->select_id3v2_frame_by_descr("TXXX[${field}]", $newval); | ||
| 300 | } | ||
| 301 | $mp3->update_tags(); | ||
| 302 | } # replace_one_field | ||
| 303 | |||
| 304 | =head2 delete_field_from_file | ||
| 305 | |||
| 306 | Remove the given field. This does no checking. | ||
| 307 | For some fields, they cannot be removed, merely set to the empty string. | ||
| 308 | |||
| 309 | $scribe->delete_field_from_file(filename=>$filename,field=>$field); | ||
| 310 | |||
| 311 | =cut | ||
| 312 | |||
| 313 | sub delete_field_from_file { | ||
| 314 | my $self = shift; | ||
| 315 | my %args = @_; | ||
| 316 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 317 | |||
| 318 | my $filename = $args{filename}; | ||
| 319 | my $field = $args{field}; | ||
| 320 | |||
| 321 | my $mp3 = MP3::Tag->new($filename); | ||
| 322 | $mp3->config(write_v24=>1); | ||
| 323 | |||
| 324 | if ($field eq 'title') | ||
| 325 | { | ||
| 326 | $mp3->album_set(''); | ||
| 327 | } | ||
| 328 | elsif ($field eq 'song') | ||
| 329 | { | ||
| 330 | $mp3->title_set(''); | ||
| 331 | } | ||
| 332 | elsif ($field eq 'description') | ||
| 333 | { | ||
| 334 | $mp3->comment_set(''); | ||
| 335 | } | ||
| 336 | elsif ($field eq 'creator') | ||
| 337 | { | ||
| 338 | $mp3->artist_set(''); | ||
| 339 | } | ||
| 340 | elsif ($field eq 'mood') | ||
| 341 | { | ||
| 342 | $mp3->select_id3v2_frame_by_descr('TMOO', undef); | ||
| 343 | } | ||
| 344 | elsif ($field eq 'performer') | ||
| 345 | { | ||
| 346 | $mp3->select_id3v2_frame_by_descr('TPE1', undef); | ||
| 347 | } | ||
| 348 | elsif ($field eq 'composer') | ||
| 349 | { | ||
| 350 | $mp3->select_id3v2_frame_by_descr('TCOM', undef); | ||
| 351 | } | ||
| 352 | elsif ($field eq 'author') | ||
| 353 | { | ||
| 354 | # use the 'composer' field | ||
| 355 | $mp3->select_id3v2_frame_by_descr('TCOM', undef); | ||
| 356 | } | ||
| 357 | elsif ($field eq 'url') | ||
| 358 | { | ||
| 359 | # official audio file webpage | ||
| 360 | $mp3->select_id3v2_frame_by_descr('WOAF', undef); | ||
| 361 | } | ||
| 362 | else | ||
| 363 | { | ||
| 364 | $mp3->select_id3v2_frame_by_descr("TXXX[${field}]", undef); | ||
| 365 | } | ||
| 366 | $mp3->update_tags(); | ||
| 367 | } # delete_field_from_file | ||
| 368 | |||
| 369 | =head1 BUGS | ||
| 370 | |||
| 371 | Please report any bugs or feature requests to the author. | ||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | 1; # End of File::Sticker::Scribe | ||
| 376 | __END__ | ||