File Coverage

blib/lib/Brackup/Chunker/MP3.pm
Criterion Covered Total %
statement 5 34 14.7
branch 0 16 0.0
condition 0 9 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 8 66 12.1


line stmt bran cond sub pod time code
1             package Brackup::Chunker::MP3;
2 13     13   76 use strict;
  13         27  
  13         633  
3              
4             my $HAVE_MP3_INFO;
5             BEGIN {
6 13     13   808 $HAVE_MP3_INFO = eval "use MP3::Info (); 1;";
  13     13   15228  
  0            
  0            
7             }
8              
9             sub chunks {
10 0     0 0   my ($class, $file) = @_;
11 0           my @chunk_list;
12 0 0         die "Required module MP3::Info not found. Needed by the MP3 file chunker.\n"
13             unless $HAVE_MP3_INFO;
14              
15 0           my $file_path = $file->path;
16              
17             # file might've been renamed or deleted in the meantime:
18 0 0         warn "File went away: $file_path ; ignoring it.\n" unless -e $file_path;
19 0 0         return () unless -e $file_path;
20              
21 0           my ($music_offset, $music_size) = main_music_range($file_path);
22 0           my $size = $file->size;
23              
24             # add the ID3v2 header, if necessary:
25 0 0         if ($music_offset != 0) {
26 0           push @chunk_list, Brackup::PositionedChunk->new(
27             file => $file,
28             offset => 0,
29             length => $music_offset,
30             );
31             }
32              
33             # add the music chunk (one big chunk, at least for now)
34 0           push @chunk_list, Brackup::PositionedChunk->new(
35             file => $file,
36             offset => $music_offset,
37             length => $music_size,
38             );
39              
40             # add the ID3v1 header chunk, if necessary:
41 0           my $music_end = $music_offset + $music_size;
42 0 0         if ($music_end != $size) {
43 0           push @chunk_list, Brackup::PositionedChunk->new(
44             file => $file,
45             offset => $music_end,
46             length => $size - $music_end,
47             );
48             }
49              
50 0           return @chunk_list;
51             }
52              
53             sub main_music_range {
54 0     0 0   my $file = shift;
55 0           my $size = -s $file;
56              
57             # if not an mp3, include the whole file
58 0 0         unless ($file =~ /\.mp3$/i) {
59 0           return (0, $size);
60             }
61              
62 0           my $info = MP3::Info::get_mp3info($file);
63 0 0 0       unless ($info && defined $info->{OFFSET}) {
64 0           return (0, $size);
65             }
66              
67 0           my $offset = $info->{OFFSET};
68 0           my $tag = MP3::Info::get_mp3tag($file);
69 0 0 0       if ($tag && $tag->{TAGVERSION} && $tag->{TAGVERSION} =~ /ID3v1/) {
      0        
70 0           return ($offset, $size - $offset - 128);
71             }
72 0           return ($offset, $size - $offset);
73             }
74              
75              
76             1;
77              
78             __END__