blib/lib/FixMP3Tags/Processor.pm

filter: Statementstmt 93.0% 173 / 186 Branchbran 85.7% 72 / 84 Conditioncond 58.0% 29 / 50 MC/DCmcdc 36.3% 12 / 33 Subroutinesub 95.0% 19 / 20 Podpod 33.3% 3 / 9 totaltotal 80.6% 308 / 382 CC 61 SCAR 22.6
CC
61
Cov
86%
process_file
18.8
process_directory
13.1
preview_album_name
12.0
CRAP 9.6
↑ Summary bin/fix-mp3-tags »
1 package FixMP3Tags::Processor;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use FixMP3Tags ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Use MP3::Tag for ID3v2 writing
16 9 eval {
17 9 require MP3::Tag;
18 9 MP3::Tag->import();
19 };
20 9 ▶if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
21 {
22 0 die "MP3::Tag required: sudo cpan MP3::Tag\n";
23 }
24  
25 9 my @CD_PATTERNS = qw/cd disk disc part volume/;
26  
27 sub run
28 5 ▶{
Subroutine run: called CC=5 SCAR=16.2
Pod documented
29 5 my $class = shift;
30  
31 5 my ($quiet, $dry_run, $pattern, $directory, $file);
32 5 GetOptions(
33 'quiet|q' => \$quiet,
34 'dry-run|n' => \$dry_run,
35 'pattern|p=s' => \$pattern,
36 'directory|d=s' => \$directory,
37 'file|f=s' => \$file,
38 'help|h' => \&show_help,
39 'version|v' => \&show_version,
40 );
41  
42 3 ▶ $directory ||= '.';
Condition100%2/2
$directory ||= "."
l!l
12
MC/DC100%1/1
$directory ||= "."
$directory
Truth table100%2/2
A
A= $directory
Aresult
11
00
43  
44 3 ▶ unless (-d $directory)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $directory) 1 2 3
45 {
46 1 die "Directory does not exist: $directory\n";
47 }
48  
49 2 ▶ if ($file)
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 2 0 2
50 {
51 2 ▶ unless (-f $file)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 1 1 2
52 {
53 1 die "File does not exist: $file\n";
54 }
55 $class->process_file(
56 1 $file,
57 {
58 quiet => $quiet,
59 dry_run => $dry_run,
60 pattern => $pattern,
61 }
62 );
63 }
64 else
65 {
66 0 $class->process_directory(
67 $directory,
68 {
69 quiet => $quiet,
70 dry_run => $dry_run,
71 pattern => $pattern,
72 }
73 );
74 }
75 }
76  
77 sub process_directory
78 4 ▶{
Subroutine process_directory: called CC=13 SCAR=25.8
Pod documented
79 4 my ($class, $directory, $opts) = @_;
80  
81 4 my %album_preview;
82 4 my $abs_directory = abs_path($directory);
83  
84 # First pass: collect album directories for preview
85 find(
86 {
87 38 ▶ wanted => sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
88 38 my $file_path = $File::Find::name;
89 38 ▶ return unless $file_path && -f $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_path and -f $file_path 21 17 38
Condition66%2/3
$file_path and -f $file_path
!ll&&!rl&&r
02117
MC/DC50%1/2
$file_path and -f $file_path
$file_path -f $file_path
Truth table66%2/3
A and B
A= $file_pathB= -f $file_path
ABresult
0X0
100
111
90 17 ▶ return unless $file_path =~ /\.mp3$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_path =~ /\.mp3$/i 1 16 17
91  
92 16 my ($album_dir, $artist_dir, $cd_dir) =
93 $class->find_album_dir($file_path);
94 16 ▶ if ($album_dir && $artist_dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($album_dir and $artist_dir) 16 0 16
Condition33%1/3
$album_dir and $artist_dir
!ll&&!rl&&r
0016
MC/DC0%0/2
$album_dir and $artist_dir
$album_dir $artist_dir
Truth table33%1/3
A and B
A= $album_dirB= $artist_dir
ABresult
0X0
100
111
95 {
96 16 my $album_name = basename($album_dir);
97 16 my $artist_name = basename($artist_dir);
98  
99 # Store unique album-artist combinations
100 16 my $key = "$artist_name|$album_name";
101 16 ▶ $album_preview{$key} = {
Branch: 1 branch
BranchTrueFalseTotal
$cd_dir ? : 3 13 16
102 artist => $artist_name,
103 album => $album_name,
104 has_cd => $cd_dir ? 1 : 0
105 };
106 }
107 },
108 4 no_chdir => 1,
109 },
110 $abs_directory
111 );
112  
113 # Show preview
114 4 ▶ if (!$opts->{quiet} && keys %album_preview)
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"quiet"} and keys %album_preview) 2 2 4
Condition66%2/3
not $opts->{"quiet"} and keys %album_preview
!ll&&!rl&&r
202
MC/DC50%1/2
not $opts->{"quiet"} and keys %album_preview
not $opts->{"quiet"} keys %album_preview
Truth table66%2/3
not(A) and B
A= not $opts->{"quiet"}B= keys %album_preview
ABresult
0X0
100
111
115 {
116 2 print "=== Album Name Preview ===\n";
117 2 print
118 "Format: Artist -> 'Real Album Name' -> 'Extracted Album Name' (Year-Month)\n";
119 2 print "====================================\n";
120  
121 2 for my $key (sort keys %album_preview)
122 {
123 4 my $info = $album_preview{$key};
124 my ($year, $month, $album_clean) =
125 4 $class->extract_album_info($info->{album}, $opts->{pattern});
126  
127 4 $album_clean =~ s/^['"]*|['"]*$//g;
128  
129 4 ▶ my $cd_indicator = $info->{has_cd} ? " [has CD subdirs]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$info->{'has_cd'} ? : 1 3 4
130  
131 4 ▶ if ($year && $month)
Branch: 1 branch
BranchTrueFalseTotal
if ($year and $month) { } 3 1 4
Condition66%2/3
$year and $month
!ll&&!rl&&r
103
MC/DC50%1/2
$year and $month
$year $month
Truth table66%2/3
A and B
A= $yearB= $month
ABresult
0X0
100
111
132 {
133 3 print
134 " $info->{artist} -> '$info->{album}' -> '$album_clean' ($year-$month)$cd_indicator\n";
135 }
136 else
137 {
138 1 print
139 " $info->{artist} -> '$info->{album}' -> '$album_clean' (no date)$cd_indicator\n";
140 }
141 }
142 2 print "\n";
143 }
144  
145 4 ▶ return if $opts->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"dry_run"} 1 3 4
146  
147 # Ask for confirmation
148 3 ▶ if (!$opts->{quiet} && keys %album_preview)
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"quiet"} and keys %album_preview) 2 1 3
Condition66%2/3
not $opts->{"quiet"} and keys %album_preview
!ll&&!rl&&r
102
MC/DC50%1/2
not $opts->{"quiet"} and keys %album_preview
not $opts->{"quiet"} keys %album_preview
Truth table66%2/3
not(A) and B
A= not $opts->{"quiet"}B= keys %album_preview
ABresult
0X0
100
111
149 {
150 2 print "Proceed with processing? (Y/n): ";
151 2 my $response = <STDIN>;
152 2 chomp $response;
153 2 ▶ if ($response !~ /^[Yy]?$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response =~ /^[Yy]?$/) 1 1 2
154 {
155 1 print "Processing cancelled.\n";
156 1 return;
157 }
158 1 print "\n";
159 }
160  
161 # Second pass: actual processing
162 2 my ($processed, $errors) = (0, 0);
163 find(
164 {
165 16 ▶ wanted => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
166 16 my $file_path = $File::Find::name;
167 16 ▶ return unless $file_path && -f $file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_path and -f $file_path 9 7 16
Condition66%2/3
$file_path and -f $file_path
!ll&&!rl&&r
097
MC/DC50%1/2
$file_path and -f $file_path
$file_path -f $file_path
Truth table66%2/3
A and B
A= $file_pathB= -f $file_path
ABresult
0X0
100
111
168 7 ▶ return unless $file_path =~ /\.mp3$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_path =~ /\.mp3$/i 1 6 7
169  
170 6 ▶ if ($class->process_file($file_path, $opts))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->process_file($file_path, $opts)) { } 5 1 6
171 {
172 5 $processed++;
173 }
174 else
175 {
176 1 $errors++;
177 }
178 },
179 2 no_chdir => 1,
180 },
181 $abs_directory
182 );
183  
184 2 ▶ if ($errors > 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($errors > 0) { } 1 1 2
elsif (not $opts->{'quiet'}) { } 1 0 1
185 {
186 1 print
187 "Processing complete with errors: $processed files updated, $errors errors\n";
188 }
189 elsif (!$opts->{quiet})
190 {
191 1 print
192 "Processing complete: $processed files updated, $errors errors\n";
193 }
194 }
195  
196 sub process_file
197 13 ▶{
Subroutine process_file: called CC=17 SCAR=29.3
Pod documented
198 13 my ($class, $file_path, $opts) = @_;
199  
200 # Validate file path
201 13 ▶ unless ($file_path && -f $file_path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_path and -f $file_path) 1 12 13
Condition66%2/3
$file_path and -f $file_path
!ll&&!rl&&r
0112
MC/DC50%1/2
$file_path and -f $file_path
$file_path -f $file_path
Truth table66%2/3
A and B
A= $file_pathB= -f $file_path
ABresult
0X0
100
111
202 {
203 1 warn "ERROR: Invalid file path: $file_path\n";
204 1 return;
205 }
206  
207 # Get album and artist directories
208 12 my ($album_dir, $artist_dir, $cd_dir) =
209 $class->find_album_dir($file_path);
210 12 ▶ return unless $album_dir && $artist_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $album_dir and $artist_dir 0 12 12
Condition33%1/3
$album_dir and $artist_dir
!ll&&!rl&&r
0012
MC/DC0%0/2
$album_dir and $artist_dir
$album_dir $artist_dir
Truth table33%1/3
A and B
A= $album_dirB= $artist_dir
ABresult
0X0
100
111
211  
212 12 my $album_name = basename($album_dir);
213 12 my $artist_name = basename($artist_dir);
214  
215 # Extract year, month, and clean album name
216 my ($year, $month, $album) =
217 12 $class->extract_album_info($album_name, $opts->{pattern});
218  
219 # Clean quotes for tags only
220 12 my $album_clean = $album;
221 12 $album_clean =~ s/^['"]*|['"]*$//g;
222  
223 # Parse filename
224 12 my $base_name;
225 12 eval {
226 12 $base_name = basename($file_path);
227 12 $base_name =~ s/\.(mp3|MP3)$//i;
228 };
229 12 ▶ if ($@ || !defined $base_name)
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $base_name) 0 12 12
Condition33%1/3
$@ or not defined $base_name
l!l&&r!l&&!r
0012
MC/DC0%0/2
$@ or not defined $base_name
$@ not defined $base_name
Truth table33%1/3
A or not(B)
A= $@B= not defined $base_name
ABresult
1X1
011
000
230 {
231 0 warn "ERROR: Could not parse filename: $file_path\n";
232 0 return;
233 }
234  
235 12 ▶ unless ($base_name =~ /^(\d+)\.(.+)$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_name =~ /^(\d+)\.(.+)$/) 2 10 12
236 {
237 2 warn
238 "ERROR: Filename doesn't match pattern NN.SongName: $base_name\n";
239 2 return;
240 }
241  
242 10 my ($track, $title) = ($1, $2);
243 10 $title =~ s/[._]/ /g;
244  
245 # Display processing info
246 10 ▶ unless ($opts->{quiet})
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"quiet"}) 2 8 10
247 {
248 2 print "Processing: $file_path\n";
249 2 ▶ if ($cd_dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($cd_dir) 0 2 2
250 {
251 0 print " CD Directory: $cd_dir\n";
252 }
253 2 ▶ if ($year && $month)
Branch: 1 branch
BranchTrueFalseTotal
if ($year and $month) { } 2 0 2
Condition33%1/3
$year and $month
!ll&&!rl&&r
002
MC/DC0%0/2
$year and $month
$year $month
Truth table33%1/3
A and B
A= $yearB= $month
ABresult
0X0
100
111
254 {
255 2 print " Album: '$album_name' -> '$album_clean' ($year-$month)\n";
256 }
257 else
258 {
259 0 print " Album: '$album_name' -> '$album_clean' (no date)\n";
260 }
261 2 print " Artist: '$artist_name'\n";
262 2 print " Track: $track, Title: '$title'\n";
263 }
264  
265 10 ▶ return 1 if $opts->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"dry_run"} 2 8 10
266  
267 8 eval {
268 8 my $mp3 = MP3::Tag->new($file_path);
269 8 $mp3->get_tags;
270  
271 # Completely remove existing ID3v2 tag and create new one
272 8 ▶ delete $mp3->{ID3v2} if exists $mp3->{ID3v2};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mp3->{"ID3v2"} 8 0 8
273 8 my $id3v2 = $mp3->new_tag("ID3v2");
274  
275 # Set the frames
276 8 $id3v2->add_frame("TPE1", $artist_name); # Artist
277 8 $id3v2->add_frame("TALB", $album_clean); # Album
278 8 $id3v2->add_frame("TIT2", $title); # Title
279 8 $id3v2->add_frame("TRCK", $track); # Track number
280  
281 8 ▶ if ($year)
Branch: 1 branch
BranchTrueFalseTotal
if ($year) 5 3 8
282 {
283 5 $id3v2->add_frame("TYER", $year); # Year
284 }
285  
286 # Write the tag
287 8 $id3v2->write_tag;
288 7 $mp3->close;
289  
290 7 ▶ print " ✓ Tags updated successfully\n" unless $opts->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"quiet"} 1 6 7
291 };
292  
293 8 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
294 {
295 1 warn "ERROR: Failed to update tags for: $file_path - $@\n";
296 1 return;
297 }
298  
299 7 return 1;
300 }
301  
302 sub find_album_dir
303 31 ▶{
Subroutine find_album_dir: called CC=4 SCAR=13.9
Pod undocumented
304 31 my ($class, $file_path) = @_;
305  
306 31 my $abs_path = eval {abs_path($file_path)};
307 31 ▶ unless ($abs_path && -f $abs_path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($abs_path and -f $abs_path) 1 30 31
Condition66%2/3
$abs_path and -f $abs_path
!ll&&!rl&&r
0130
MC/DC50%1/2
$abs_path and -f $abs_path
$abs_path -f $abs_path
Truth table66%2/3
A and B
A= $abs_pathB= -f $abs_path
ABresult
0X0
100
111
308 {
309 1 warn "ERROR: Cannot get absolute path for: $file_path\n";
310 1 return (undef, undef);
311 }
312  
313 30 my $current_dir = dirname($abs_path);
314 30 my $current_name = basename($current_dir);
315  
316 # First, check if current directory is a CD directory
317 30 ▶ if ($class->is_cd_directory($current_name))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_cd_directory($current_name)) 5 25 30
318 {
319 # Go up one level to get the album directory
320 5 my $album_dir = dirname($current_dir);
321 5 my $artist_dir = dirname($album_dir);
322 5 return ($album_dir, $artist_dir, $current_name);
323 }
324  
325 # Current directory is not a CD directory, so it should be the album directory
326 25 my $artist_dir = dirname($current_dir);
327 25 return ($current_dir, $artist_dir, undef);
328 }
329  
330 sub is_cd_directory
331 36 ▶{
Subroutine is_cd_directory: called CC=7 SCAR=19.5
Pod undocumented
332 36 my ($class, $name) = @_;
333 36 ▶ return 0 unless defined $name && $name ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name ne "" 1 35 36
Condition66%2/3
defined $name and $name ne ""
!ll&&!rl&&r
1035
MC/DC50%1/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table66%2/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
334  
335 35 my $lower = lc($name);
336  
337 35 for my $pattern (@CD_PATTERNS)
338 {
339 143 ▶ return 1 if $lower =~ /^$pattern\s*\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $lower =~ /^$pattern\s*\d+/ 7 136 143
340 136 ▶ return 1 if $lower =~ /\d+\s*$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
if $lower =~ /\d+\s*$pattern/ 1 135 136
341 135 ▶ return 1 if $lower =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $lower =~ /^\d+$/ 1 134 135
342 }
343  
344 26 return 0;
345 }
346  
347 sub extract_album_info
348 27 ▶{
Subroutine extract_album_info: called CC=8 SCAR=20.9
Pod undocumented
349 27 my ($class, $dir_name, $pattern) = @_;
350  
351 27 ▶ unless (defined $dir_name && $dir_name ne '')
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir_name and $dir_name ne "") 1 26 27
Condition66%2/3
defined $dir_name and $dir_name ne ""
!ll&&!rl&&r
1026
MC/DC50%1/2
defined $dir_name and $dir_name ne ""
defined $dir_name $dir_name ne ""
Truth table66%2/3
A and B
A= defined $dir_nameB= $dir_name ne ""
ABresult
0X0
100
111
352 {
353 1 return (undef, undef, '');
354 }
355  
356 # User pattern
357 26 ▶ if ($pattern && $dir_name =~ /$pattern/)
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern and $dir_name =~ /$pattern/) 3 23 26
Condition100%3/3
$pattern and $dir_name =~ /$pattern/
!ll&&!rl&&r
2213
MC/DC100%2/2
$pattern and $dir_name =~ /$pattern/
$pattern $dir_name =~ /$pattern/
Truth table100%3/3
A and B
A= $patternB= $dir_name =~ /$pattern/
ABresult
0X0
100
111
358 {
359 3 my ($year, $month, $album) = ($1, $2, $3);
360 3 ▶ $album ||= $dir_name;
Condition66%2/3
$album ||= $dir_name
l!l&&r!l&&!r
210
MC/DC0%0/2
$album ||= $dir_name
$album $dir_name
Truth table66%2/3
A ||= B
A= $albumB= $dir_name
ABresult
1X1
011
000
361 3 return ($year, $month, $album);
362 }
363  
364 # Flexible YYYY*MM pattern - specifically handle the quote case
365 23 ▶ if ($dir_name =~ /^.*(\d{4})\D*?(\d{2})['"\s]*([^'"]*)['"]*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_name =~ /^.*(\d{4})\D*?(\d{2})['"\s]*([^'"]*)['"]*$/) 17 6 23
366 {
367 17 my ($year, $month, $album) = ($1, $2, $3);
368 17 ▶ return ($year, $month, $album) if $album;
Branch: 1 branch
BranchTrueFalseTotal
if $album 17 0 17
369 }
370  
371 # No pattern matched
372 6 return (undef, undef, $dir_name);
373 }
374  
375 sub preview_album_name
376 0 ▶{
Subroutine preview_album_name: not called CC=3 SCAR=24.8
Pod undocumented
377 0 my ($class, $dir_name, $pattern) = @_;
378  
379 0 my ($year, $month, $album_name) =
380 $class->extract_album_info($dir_name, $pattern);
381  
382 # Clean quotes for display in preview
383 0 my $album_clean = $album_name;
384 0 $album_clean =~ s/^['"]*|['"]*$//g;
385  
386 0 ▶ if (defined $year && defined $month)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $year and defined $month) { } 0 0 0
Condition0%0/3
defined $year and defined $month
!ll&&!rl&&r
000
MC/DC0%0/2
defined $year and defined $month
defined $year defined $month
Truth table0%0/3
A and B
A= defined $yearB= defined $month
ABresult
0X0
100
111
387 {
388 0 print " '$dir_name' -> '$album_clean' ($year-$month)\n";
389 }
390 else
391 {
392 0 print " '$dir_name' -> '$album_clean' (no date)\n";
393 }
394 }
395  
396 sub show_help
397 1 ▶{
Subroutine show_help: called CC=1 SCAR=0.0
Pod undocumented
398 1 print <<'HELP';
399 Usage: fix-mp3-tags [OPTIONS]
400  
401 Options:
402 -d, --directory DIR Process all MP3 files in directory DIR (default: current directory)
403 -f, --file FILE Process single MP3 file FILE
404 -q, --quiet Display only errors
405 -p, --pattern PATTERN Custom album directory pattern (regex)
406 -n, --dry-run Preview without processing
407 -v, --version Show version
408 -h, --help Show this help
409  
410 Examples:
411 fix-mp3-tags # Process all MP3s in current directory (verbose)
412 fix-mp3-tags -q # Process all MP3s, show only errors
413 fix-mp3-tags -n # Preview album name extraction
414 fix-mp3-tags -d /path/to/music # Process specific directory
415  
416 Installation:
417 cpanm --installdeps .
418 HELP
419 1 exit 0;
420 }
421  
422 sub show_version
423 1 ▶{
Subroutine show_version: called CC=1 SCAR=0.0
Pod undocumented
424 1 print "fix-mp3-tags version $FixMP3Tags::VERSION\n";
425 1 exit 0;
426 }
427  
428 9 1;
429  
430 __END__
↑ Summary bin/fix-mp3-tags »