T
Trebor A. Rude
I've been trying to learn Perl in recent days, and was hoping the group
could offer some pointers on my first attempt to do something useful (to
me, anyway) with it. To that end, any constructive comments, suggestions,
etc. on this script would be appreciated.
The script takes a list of .ogg files on the command line and reads the tag
information from them (which come out in "key=value" form), then uses it to
construct a command to put the same tag information into a
corresponding .mp3 file (I prefer .oggs for listening on my computer, but
my car's CD player only works with audio or MP3 CDs). Here it is:
#!/usr/bin/perl
use warnings;
use strict;
use vars '$file';
my %options = (title => '-t',
artist => '-a',
album => '-A',
tracknumber => '-T');
sub run_id3v2 (@)
{
my @command = ("id3v2", @_, $file);
return system(@command) == 0;
}
foreach (@ARGV)
{
$file = $_;
my @output = `vorbiscomment -l \Q$file\E 2>&1`;
if ($? != 0)
{
print "Problems reading tags from $file, skipping it:\n", @output;
next;
}
my %comments = map { chomp; split /=/, $_, 2 } @output;
my @arguments = ();
# Also considered using (if this is strange indentation, thank
# cperl-mode):
#
# push @arguments,
# map {($options{$_}, $comments{$_})}
# grep exists $options{$_}, keys %comments;
push @arguments,
map {exists $options{$_} ? ($options{$_}, $comments{$_}) : ()}
keys %comments;
$file =~ s/\.ogg/.mp3/;
run_id3v2('-D') or die "Problem removing old tags from $file.";
run_id3v2(@arguments) or die "Problem adding new tags to $file.";
run_id3v2('-s') or die "Problem removing new id3v1 tags from $file.";
}
could offer some pointers on my first attempt to do something useful (to
me, anyway) with it. To that end, any constructive comments, suggestions,
etc. on this script would be appreciated.
The script takes a list of .ogg files on the command line and reads the tag
information from them (which come out in "key=value" form), then uses it to
construct a command to put the same tag information into a
corresponding .mp3 file (I prefer .oggs for listening on my computer, but
my car's CD player only works with audio or MP3 CDs). Here it is:
#!/usr/bin/perl
use warnings;
use strict;
use vars '$file';
my %options = (title => '-t',
artist => '-a',
album => '-A',
tracknumber => '-T');
sub run_id3v2 (@)
{
my @command = ("id3v2", @_, $file);
return system(@command) == 0;
}
foreach (@ARGV)
{
$file = $_;
my @output = `vorbiscomment -l \Q$file\E 2>&1`;
if ($? != 0)
{
print "Problems reading tags from $file, skipping it:\n", @output;
next;
}
my %comments = map { chomp; split /=/, $_, 2 } @output;
my @arguments = ();
# Also considered using (if this is strange indentation, thank
# cperl-mode):
#
# push @arguments,
# map {($options{$_}, $comments{$_})}
# grep exists $options{$_}, keys %comments;
push @arguments,
map {exists $options{$_} ? ($options{$_}, $comments{$_}) : ()}
keys %comments;
$file =~ s/\.ogg/.mp3/;
run_id3v2('-D') or die "Problem removing old tags from $file.";
run_id3v2(@arguments) or die "Problem adding new tags to $file.";
run_id3v2('-s') or die "Problem removing new id3v1 tags from $file.";
}