walto has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monks,
I wrote a script to remove unwanted mp3 tags and posted Remove unwanted MP3 tags in the CUFP section. As suggested in the thread I use Parallel::ForkMmanager to loop over the files in parallel. I adjusted the code of the original script but I can see no speed up. I tested it with 100 mp3 files and it was actually slower than the script without Parallel::ForkManager.
time remove_unwanted_mp3.pl
real 0m0.311s, user 0m0.279s, sys 0m0.028s
time parallel_remove_unwanted_mp3.pl
real 0m0.877s, user 0m0.511s, sys 0m0.501s
Here is the code
Without Parallel::ForkManager
using Parallel::ForkManager#!/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; 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 remove_unwanted_tags.pl -d + DIRECTORY\n"; exit; }
#!/usr/bin/perl # # use strict; use warnings; use strict; use Getopt::Std; use File::Find::Rule; use MP3::Tag; use Parallel::ForkManager; 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; 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 $pm = Parallel::ForkManager->new(50); my $rule = File::Find::Rule->file->name("*.mp3")->start($dir); while ( defined( my $file = $rule->match ) ) { my $pid = $pm->start and next; 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(); } } } } $pm->finish; } $pm->wait_all_children; sub usage { print "Please provide parent directory\ne.g. perl remove_unwanted_tags.pl -d + DIRECTORY\n"; exit; }
|
---|
Back to
Seekers of Perl Wisdom