forked from spicyjack/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_id3.pl
executable file
·162 lines (141 loc) · 4.9 KB
/
batch_id3.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl -w
# $Id$
# Batch MP3 ID3 tagger
# (c)2003 Brian Manning
#
# will change the text tags located in MP3 files in various ways
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# TODO's
#
# - pull the date for the comment field using the date command
# or perl equivalent; use the date trick for ripit.pl as well; see
# thumbnails.pl for one example of how to do it
# - pull Artist, Album, Track number and track name (Title) from
# the path and filename
# - year and genre fields will have to be prompted for
# - switch to MP3::Tag module for modifying MP3 tags
# - test to make sure MP3::Tag was installed on the system, and exit if it's
# not
# - script defaults to -s (show tags) if no other options are given
# use directives
use Getopt::Std; # parse command line parameters
use strict; # strictness is good
my $ID3 = "/usr/bin/id3"; # path to the id3 binary
### begin main script body ###
# some variables please
my %mp3tag; # the tag information that will be applied to an MP3 file
my %opts; # for &getopts
my @files = <*>; # all the files in the current directory
my $total_files = 0; # how many files we changed
my $start_time = time; # time we started processing files
# read in the command line options
&getopts("acdhinsv", \%opts);
# a - change all tags
# c - change comment only
# d - debugging
# i - ignore path, change Track, Title, and Comment tags only
# h - show help
# n - don't make changes, just pretend you're going to make changes
# s - show tags *DEFAULT*
# show help?
if ( $opts{h} ) {
&ShowHelp;
exit 1;
} # if ( $opts{h} )
# count the time it takes to process
# TODO parse out the filename/path here
# use code from mp3_reencode.pl
# loop thru @files, processing MP3 files only
foreach my $file (@files) {
chomp($file); # remove any trailing EOL's
# now format the filename so it doesn't make the shell barf
# add in extra backwhacks to hide shell metacharacters
$file =~ s/ /\\ /g;
$file =~ s/\(/\\\(/g;
$file =~ s/\)/\\\)/g;
$file =~ s/\,/\\\,/g;
$file =~ s/\&/\\\&/g;
$file =~ s/\'/\\\'/g;
# $file =~ s/\\/\\\\/g;
# are we displaying tags, or modifying tags?
if ($show eq "show") {
system("id3 -l $file");
if ($total_lines % 5 == 0 and $total_lines != 0) {
print "Line $total_lines; Press any key to continue";
$answer = <STDIN>;
$answer = ""; # this should take care of -w
}
}
elsif ($show eq "verbose") {
print "Showing $file\n";
system("id3 -l $file");
}
else {
#system("id3 -c \"SpicyJack's Stash, (R)2000\" $_");
my $id3cmd = "id3 -c \"SpicyJack's Stash, (R)2000\" ";
system("id3 -c \"SpicyJack's Stash, (R)2000\" $_");
}
# update total files changed
$total_lines++;
}
# tell'em how we did...
$total_time=time - $start_time;
$total_min = $total_time /60;
print "Processed $total_lines lines\n";
print "in $total_time seconds, or $total_min minutes.\n";
### end main script body ###
##############
# ShowMP3Tag #
##############
#sub ShowMP3Tag {
# displays a file's MP3 tag, if it has one. Display will include ID3 version,
# track, song title, album, artist, comment and filename
# } # sub ShowMP3Tag
###############
# MP3FileInfo #
###############
# sub MP3FileInfo {
# reads the file path, and separates out all of the info for use in tagging an
# MP3 (Track, Song Title, Artist, Album)
# my $opts = $_[0]; # command line options
# -i: set only Comment, Track, Song Title fields
# -a: set all fields
# -c: set only comment field
# } sub MP3FileInfo
################
# ChangeMP3Tag #
################
# sub ChangeMP3Tag {
# changes the MP3 tag info for a specific file; any fields in the hash that are
# _undef_ don't get changed; this will allow the command line switches -c and
# -i work with minimal hassle
#} # sub ChangeMP3Tag
############
# ShowHelp #
############
sub ShowHelp {
# display a list of script options and exit
print "$0:\n";
print "Batch MP3 ID3 Tagger\n";
print "Script options:\n";
print "-c change comment only (edit script to set comment\n";
print "-d debug, noisy output\n";
print "-i ignore path, change Track, Title and Comment tags only\n";
print " (Track and Title will be taken from the filename)\n";
print "-n don't make any changes, just pretend\n";
print "-s show existing tags, don't make any changes\n";
} # sub ShowHelp
# end of line