Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Remove unwanted MP3 tags

by walto (Pilgrim)
on Feb 16, 2014 at 18:35 UTC ( #1075102=CUFP: print w/replies, xml ) Need Help??

Sometimes mp3 files come with a plethora of tags. Some of them contain unnecessary information.
Here is a script that removes unwanted tags. The unwanted tags are stored in an array.

#!/usr/bin/perl # # use strict; use warnings; use strict; use Getopt::Std; use File::Find::Rule; use MP3::Tag; use utf8; MP3::Tag->config( write_v24 => 1 ); our $opt_d; my $opt_string = 'd:'; getopts( 'd:', $opt_d ) or usage(); my $dir = $opt_d; die "$!: $dir\n" unless (-d $dir); my @unwanted_tags = qw (COMM COMM01 COMM02 COMM03 MCDI PRIV PRIV01 PRIV02 PRIV03 PRIV04 +PRIV05 PRIV06 TBPM01 TMED TPUB TSSE TXXX TXXX01 TXXX02 TXXX03 TXXX04 +TXXX05 TXXX06 TXXX07 TXXX08 TXXX09 TXXX10 WCOM WXXX); my %unwanted_tags = map { $unwanted_tags[$_] => 1 } 0 .. $#unwanted_tags; my $rule = File::Find::Rule->file->name("*.mp3")->start( $dir ); while ( defined ( my $file = $rule->match ) ) { my ( $album, $artist, $title ); my $mp3 = MP3::Tag->new($file); $mp3->get_tags(); if ( exists $mp3->{ID3v2} ) { my $id3v2 = $mp3->{ID3v2}; my $frameIDs_hash = $id3v2->get_frame_ids(); if ($frameIDs_hash) { foreach my $frame ( keys %$frameIDs_hash ) { if ( $unwanted_tags{$frame} ) { print "$file\n"; print "Unwanted Frame: $frame found\n"; $id3v2->remove_frame($frame); $id3v2->write_tag(); } } } } } sub usage { print "Please provide parent directory\ne.g. perl -d + DIRECTORY\n"; exit; }

10 years ago I joined the monastery. Today I received a HappyMonks day greeting. That's a nice idea, and a thought it is a good occasion to share a script I wrote recently.

Replies are listed 'Best First'.
Re: Remove unwanted MP3 tags
by admiral_grinder (Pilgrim) on Feb 19, 2014 at 16:34 UTC

    Looks good, may I suggest:

    1. You are using File::Find::Rule to return a list of all files, then loop over them. That is fine, however, there is a iteration method called match(). Cuts out the need for the list.

    2. Parallel::Forkmanager. Since you are looping over files with no ties to each other, why not do it in parallel?

      Aye admiral_grinder,

      there is always room for improvements. I adapted the original code, using the method match().

      Thanks for pointing me to a multi threaded approach with Parallel::Forkmanager. I always had problems with threads but I will dig into this module. See what comes up.

Re: Remove unwanted MP3 tags
by walto (Pilgrim) on Mar 01, 2014 at 06:08 UTC
    Edited the original code with a check if the directory exits

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1075102]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-01-22 03:14 GMT
Find Nodes?
    Voting Booth?