Image/ExifTool/Audible.pm

filter: Statementstmt 60.1% 68 / 113 Branchbran 39.7% 27 / 68 Conditioncond 38.1% 8 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 66.6% 4 / 6 Podpod 0.0% 0 / 3 totaltotal 49.1% 110 / 224 CC 42 SCAR 41.7
CC
42
Cov
51%
ProcessAudible_meta
156.0
ProcessAA
38.4
ProcessAudible_cvrx
20.0
CRAP 64.5
« File/RandomAccess.pm ↑ Summary Image/ExifTool/AIFF.pm »
1 #------------------------------------------------------------------------------
2 # File: Audible.pm
3 #
4 # Description: Read metadata from Audible audio books
5 #
6 # Revisions: 2015/04/05 - P. Harvey Created
7 #
8 # References: 1) https://github.com/jteeuwen/audible
9 # 2) https://code.google.com/p/pyaudibletags/
10 # 3) http://wiki.multimedia.cx/index.php?title=Audible_Audio
11 #------------------------------------------------------------------------------
12  
13 package Image::ExifTool::Audible;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 $VERSION = '1.02';
20  
21 sub ProcessAudible_meta($$$);
22 sub ProcessAudible_cvrx($$$);
23  
24 1 %Image::ExifTool::Audible::Main = (
25 GROUPS => { 2 => 'Audio' },
26 NOTES => q{
27 ExifTool will extract any information found in the metadata dictionary of
28 Audible .AA files, even if not listed in the table below.
29 },
30 # tags found in the metadata dictionary (chunk 2)
31 pubdate => { Name => 'PublishDate', Groups => { 2 => 'Time' } },
32 pub_date_start => { Name => 'PublishDateStart', Groups => { 2 => 'Time' } },
33 author => { Name => 'Author', Groups => { 2 => 'Author' } },
34 copyright => { Name => 'Copyright', Groups => { 2 => 'Author' } },
35 # also seen (ref PH):
36 # product_id, parent_id, title, provider, narrator, price, description,
37 # long_description, short_title, is_aggregation, title_id, codec, HeaderSeed,
38 # EncryptedBlocks, HeaderKey, license_list, CPUType, license_count, <12 hex digits>,
39 # parent_short_title, parent_title, aggregation_id, short_description, user_alias
40  
41 # information extracted from other chunks
42 _chapter_count => { Name => 'ChapterCount' }, # from chunk 6
43 _cover_art => { # from chunk 11
44 Name => 'CoverArt',
45 Groups => { 2 => 'Preview' },
46 Binary => 1,
47 },
48 );
49  
50 # 'tags' atoms observed in Audible .m4b audio books (ref PH)
51 1 %Image::ExifTool::Audible::tags = (
52 GROUPS => { 0 => 'QuickTime', 2 => 'Audio' },
53 NOTES => 'Information found in "tags" atom of Audible M4B audio books.',
54 meta => {
55 Name => 'Audible_meta',
56 SubDirectory => { TagTable => 'Image::ExifTool::Audible::meta' },
57 },
58 cvrx => {
59 Name => 'Audible_cvrx',
60 SubDirectory => { TagTable => 'Image::ExifTool::Audible::cvrx' },
61 },
62 tseg => {
63 Name => 'Audible_tseg',
64 SubDirectory => { TagTable => 'Image::ExifTool::Audible::tseg' },
65 },
66 );
67  
68 # 'meta' information observed in Audible .m4b audio books (ref PH)
69 1 %Image::ExifTool::Audible::meta = (
70 PROCESS_PROC => \&ProcessAudible_meta,
71 GROUPS => { 0 => 'QuickTime', 2 => 'Audio' },
72 NOTES => 'Information found in Audible M4B "meta" atom.',
73 Album => 'Album',
74 ALBUMARTIST => { Name => 'AlbumArtist', Groups => { 2 => 'Author' } },
75 Artist => { Name => 'Artist', Groups => { 2 => 'Author' } },
76 Comment => 'Comment',
77 Genre => 'Genre',
78 itunesmediatype => { Name => 'iTunesMediaType', Description => 'iTunes Media Type' },
79 SUBTITLE => 'Subtitle',
80 Title => 'Title',
81 TOOL => 'CreatorTool',
82 Year => { Name => 'Year', Groups => { 2 => 'Time' } },
83 track => 'ChapterName', # (found in 'meta' of 'tseg' atom)
84 );
85  
86 # 'cvrx' information observed in Audible .m4b audio books (ref PH)
87 1 %Image::ExifTool::Audible::cvrx = (
88 PROCESS_PROC => \&ProcessAudible_cvrx,
89 GROUPS => { 0 => 'QuickTime', 2 => 'Audio' },
90 NOTES => 'Audible cover art information in M4B audio books.',
91 VARS => { ID_FMT => 'none' },
92 CoverArtType => 'CoverArtType',
93 CoverArt => {
94 Name => 'CoverArt',
95 Groups => { 2 => 'Preview' },
96 Binary => 1,
97 },
98 );
99  
100 # 'tseg' information observed in Audible .m4b audio books (ref PH)
101 1 %Image::ExifTool::Audible::tseg = (
102 GROUPS => { 0 => 'QuickTime', 2 => 'Audio' },
103 tshd => {
104 Name => 'ChapterNumber',
105 Format => 'int32u',
106 ValueConv => '$val + 1', # start counting from 1
107 },
108 meta => {
109 Name => 'Audible_meta2',
110 SubDirectory => { TagTable => 'Image::ExifTool::Audible::meta' },
111 },
112 );
113  
114 #------------------------------------------------------------------------------
115 # Process Audible 'meta' tags from M4B files (ref PH)
116 # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
117 # Returns: 1 on success
118 sub ProcessAudible_meta($$$)
119 0 ▶{
Subroutine ProcessAudible_meta: not called CC=12 SCAR=50.5
Pod undocumented
120 0 my ($et, $dirInfo, $tagTablePtr) = @_;
121 0 my $dataPt = $$dirInfo{DataPt};
122 0 my $dataPos = $$dirInfo{DataPos};
123 0 my $dirLen = length $$dataPt;
124 0 ▶ return 0 if $dirLen < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $dirLen < 4 0 0 0
125 0 my $num = Get32u($dataPt, 0);
126 0 $et->VerboseDir('Audible_meta', $num);
127 0 my $pos = 4;
128 0 my $index;
129 0 ▶ for ($index=0; $index<$num; ++$index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $num) 0 0 0
130 0 ▶ last if $pos + 3 > $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + 3 > $dirLen 0 0 0
131 0 my $unk = Get8u($dataPt, $pos); # ? (0x80 or 0x00)
132 0 ▶ last unless $unk == 0x80 or $unk == 0x00;
Branch: 1 branch
BranchTrueFalseTotal
unless $unk == 128 or $unk == 0 0 0 0
Condition0%0/3
$unk == 128 or $unk == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$unk == 128 or $unk == 0
$unk == 128 $unk == 0
Truth table0%0/3
A or B
A= $unk == 128B= $unk == 0
ABresult
1X1
011
000
133 0 my $len = Get16u($dataPt, $pos + 1); # tag length
134 0 $pos += 3;
135 0 ▶ last if $pos + $len + 6 > $dirLen or not $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $len + 6 > $dirLen or not $len 0 0 0
Condition0%0/3
$pos + $len + 6 > $dirLen or not $len
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos + $len + 6 > $dirLen or not $len
$pos + $len + 6 > $dirLen not $len
Truth table0%0/3
A or not(B)
A= $pos + $len + 6 > $dirLenB= not $len
ABresult
1X1
011
000
136 0 my $tag = substr($$dataPt, $pos, $len); # tag ID
137 0 my $ver = Get16u($dataPt, $pos + $len); # version?
138 0 ▶ last unless $ver == 0x0001;
Branch: 1 branch
BranchTrueFalseTotal
unless $ver == 1 0 0 0
139 0 my $size = Get32u($dataPt, $pos + $len + 2);# data size
140 0 $pos += $len + 6;
141 0 ▶ last if $pos + $size > $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $size > $dirLen 0 0 0
142 0 my $val = $et->Decode(substr($$dataPt, $pos, $size), 'UTF8');
143 0 ▶ unless ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$tag}) 0 0 0
144 0 ▶ my $name = Image::ExifTool::MakeTagName(($tag =~ /[a-z]/) ? $tag : lc($tag));
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /[a-z]/ ? : 0 0 0
145 0 AddTagToTable($tagTablePtr, $tag, { Name => $name });
146 }
147 0 $et->HandleTag($tagTablePtr, $tag, $val,
148 DataPt => $dataPt,
149 DataPos => $dataPos,
150 Start => $pos,
151 Size => $size,
152 Index => $index,
153 );
154 0 $pos += $size;
155 }
156 0 return 1;
157 }
158  
159 #------------------------------------------------------------------------------
160 # Process Audible 'cvrx' cover art atom from M4B files (ref PH)
161 # Inputs: 0) ExifTool object ref, 1) dirInfo ref, 2) tag table ref
162 # Returns: 1 on success
163 sub ProcessAudible_cvrx($$$)
164 0 ▶{
Subroutine ProcessAudible_cvrx: not called CC=4 SCAR=30.0
Pod undocumented
165 0 my ($et, $dirInfo, $tagTablePtr) = @_;
166 0 my $dataPt = $$dirInfo{DataPt};
167 0 my $dataPos = $$dirInfo{DataPos};
168 0 my $dirLen = length $$dataPt;
169 0 ▶ return 0 if 0x0a > $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if 10 > $dirLen 0 0 0
170 0 my $len = Get16u($dataPt, 0x08);
171 0 ▶ return 0 if 0x0a + $len + 6 > $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if 10 + $len + 6 > $dirLen 0 0 0
172 0 my $size = Get32u($dataPt, 0x0a + $len + 2);
173 0 ▶ return 0 if 0x0a + $len + 6 + $size > $dirLen;
Branch: 1 branch
BranchTrueFalseTotal
if 10 + $len + 6 + $size > $dirLen 0 0 0
174 0 $et->VerboseDir('Audible_cvrx', undef, $dirLen);
175 0 $et->HandleTag($tagTablePtr, 'CoverArtType', undef,
176 DataPt => $dataPt,
177 DataPos => $dataPos,
178 Start => 0x0a,
179 Size => $len,
180 );
181 0 $et->HandleTag($tagTablePtr, 'CoverArt', undef,
182 DataPt => $dataPt,
183 DataPos => $dataPos,
184 Start => 0x0a + $len + 6,
185 Size => $size,
186 );
187 0 return 1;
188 }
189  
190 #------------------------------------------------------------------------------
191 # Read information from an Audible .AA file
192 # Inputs: 0) ExifTool ref, 1) dirInfo ref
193 # Returns: 1 on success, 0 if this wasn't a valid AA file
194 sub ProcessAA($$)
195 1 ▶{
Subroutine ProcessAA: called CC=28 SCAR=36.5
Pod undocumented
196 1 my ($et, $dirInfo) = @_;
197 1 my $raf = $$dirInfo{RAF};
198 1 my ($buff, $toc, $entry, $i);
199  
200 # check magic number
201 1 ▶ return 0 unless $raf->Read($buff, 16) == 16 and $buff=~/^.{4}\x57\x90\x75\x36/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $raf->Read($buff, 16) == 16 and $buff =~ /^.{4}\x57\x90\x75\x36/s 0 1 1
Condition33%1/3
$raf->Read($buff, 16) == 16 and $buff =~ /^.{4}\x57\x90\x75\x36/s
!ll&&!rl&&r
001
MC/DC0%0/2
$raf->Read($buff, 16) == 16 and $buff =~ /^.{4}\x57\x90\x75\x36/s
$raf->Read($buff, 16) == 16 $buff =~ /^.{4}\x57\x90\x75\x36/s
Truth table33%1/3
A and B
A= $raf->Read($buff, 16) == 16B= $buff =~ /^.{4}\x57\x90\x75\x36/s
ABresult
0X0
100
111
202 # check file size
203 1 ▶ if (defined $$et{VALUE}{FileSize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $et->{"VALUE"}{"FileSize"}) 1 0 1
204 # first 4 bytes of the file should be the filesize
205 1 ▶ unpack('N', $buff) == $$et{VALUE}{FileSize} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
unpack("N", $buff) == $et->{"VALUE"}{"FileSize"} or return 0 1 0 1
206 }
207 1 $et->SetFileType();
208 1 SetByteOrder('MM');
209 1 my $bytes = 12 * Get32u(\$buff, 8); # table of contents size in bytes
210 1 ▶ $bytes > 0xc00 and $et->Warn('Invalid TOC'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$bytes > 3072 and $et->Warn("Invalid TOC"), (return 1) 0 1 1
211 # read the table of contents
212 1 ▶ $raf->Read($toc, $bytes) == $bytes or $et->Warn('Truncated TOC'), return 1;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($toc, $bytes) == $bytes or $et->Warn("Truncated TOC"), (return 1) 1 0 1
213 1 my $tagTablePtr = GetTagTable('Image::ExifTool::Audible::Main');
214 # parse table of contents (in $toc)
215 1 ▶ for ($entry=0; $entry<$bytes; $entry+=12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry < $bytes) 12 1 13
216 12 my $type = Get32u(\$toc, $entry);
217 12 ▶ next unless $type == 2 or $type == 6 or $type == 11;
Branch: 1 branch
BranchTrueFalseTotal
unless $type == 2 or $type == 6 or $type == 11 9 3 12
Condition100%6/6
$type == 2 or $type == 6 or $type == 11
l!l&&r!l&&!r
219
$type == 2 or $type == 6
l!l&&r!l&&!r
1110
MC/DC100%3/3
$type == 2 or $type == 6 or $type == 11
$type == 2 $type == 6 $type == 11
Truth table100%4/4
A or B or C
A= $type == 2B= $type == 6C= $type == 11
ABCresult
1XX1
01X1
0011
0000
218 3 my $offset = Get32u(\$toc, $entry + 4);
219 3 ▶ my $length = Get32u(\$toc, $entry + 8) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $length = &Get32u(\$toc, $entry + 8) or next 2 1 3
220 2 ▶ $raf->Seek($offset, 0) or $et->Warn("Chunk $type seek error"), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Seek($offset, 0) or $et->Warn("Chunk $type seek error"), last 2 0 2
221 2 ▶ if ($type == 6) { # offset table
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 6) 0 2 2
222 0 ▶ next if $length < 4 or $raf->Read($buff, 4) != 4; # only read the chapter count
Branch: 1 branch
BranchTrueFalseTotal
if $length < 4 or $raf->Read($buff, 4) != 4 0 0 0
Condition0%0/3
$length < 4 or $raf->Read($buff, 4) != 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$length < 4 or $raf->Read($buff, 4) != 4
$length < 4 $raf->Read($buff, 4) != 4
Truth table0%0/3
A or B
A= $length < 4B= $raf->Read($buff, 4) != 4
ABresult
1X1
011
000
223 0 $et->HandleTag($tagTablePtr, '_chapter_count', Get32u(\$buff, 0));
224 0 next;
225 }
226 # read the chunk
227 2 ▶ $length > 100000000 and $et->Warn("Chunk $type too big"), next;
Branch: 1 branch
BranchTrueFalseTotal
$length > 100000000 and $et->Warn("Chunk $type too big"), next 0 2 2
228 2 ▶ $raf->Read($buff, $length) == $length or $et->Warn("Chunk $type read error"), last;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, $length) == $length or $et->Warn("Chunk $type read error"), last 2 0 2
229 2 ▶ if ($type == 11) { # cover art
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 11) 1 1 2
230 1 ▶ next if $length < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 8 0 1 1
231 1 my $len = Get32u(\$buff, 0);
232 1 my $off = Get32u(\$buff, 4);
233 1 ▶ next if $off < $offset + 8 or $off - $offset + $len > $length;
Branch: 1 branch
BranchTrueFalseTotal
if $off < $offset + 8 or $off - $offset + $len > $length 0 1 1
Condition33%1/3
$off < $offset + 8 or $off - $offset + $len > $length
l!l&&r!l&&!r
001
MC/DC0%0/2
$off < $offset + 8 or $off - $offset + $len > $length
$off < $offset + 8 $off - $offset + $len > $length
Truth table33%1/3
A or B
A= $off < $offset + 8B= $off - $offset + $len > $length
ABresult
1X1
011
000
234 1 $et->HandleTag($tagTablePtr, '_cover_art', substr($buff, $off-$offset, $len));
235 1 next;
236 }
237 # parse metadata dictionary (in $buff)
238 1 ▶ $length < 4 and $et->Warn('Bad dictionary'), next;
Branch: 1 branch
BranchTrueFalseTotal
$length < 4 and $et->Warn("Bad dictionary"), next 0 1 1
239 1 my $num = Get32u(\$buff, 0);
240 1 ▶ $num > 0x200 and $et->Warn('Bad dictionary count'), next;
Branch: 1 branch
BranchTrueFalseTotal
$num > 512 and $et->Warn("Bad dictionary count"), next 0 1 1
241 1 my $pos = 4; # dictionary starts immediately after count
242 1 require Image::ExifTool::HTML; # (for UnescapeHTML)
243 1 $et->VerboseDir('Audible Metadata', $num);
244 1 ▶ for ($i=0; $i<$num; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num) 28 1 29
245 28 my $tagPos = $pos + 9; # position of tag string
246 28 ▶ $tagPos > $length and $et->Warn('Truncated dictionary'), last;
Branch: 1 branch
BranchTrueFalseTotal
$tagPos > $length and $et->Warn("Truncated dictionary"), last 0 28 28
247 # (1 unknown byte ignored at start of each dictionary entry)
248 28 my $tagLen = Get32u(\$buff, $pos + 1); # tag string length
249 28 my $valLen = Get32u(\$buff, $pos + 5); # value string length
250 28 my $valPos = $tagPos + $tagLen; # position of value string
251 28 my $nxtPos = $valPos + $valLen; # position of next entry
252 28 ▶ $nxtPos > $length and $et->Warn('Bad dictionary entry'), last;
Branch: 1 branch
BranchTrueFalseTotal
$nxtPos > $length and $et->Warn("Bad dictionary entry"), last 0 28 28
253 28 my $tag = substr($buff, $tagPos, $tagLen);
254 28 my $val = substr($buff, $valPos, $valLen);
255 28 ▶ unless ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$tag}) 24 4 28
256 24 my $name = Image::ExifTool::MakeTagName($tag);
257 24 $name =~ s/_(.)/\U$1/g; # change from underscore-separated to mixed case
258 24 AddTagToTable($tagTablePtr, $tag, { Name => $name });
259 }
260 # unescape HTML character references and convert from UTF-8
261 28 $val = $et->Decode(Image::ExifTool::HTML::UnescapeHTML($val), 'UTF8');
262 28 $et->HandleTag($tagTablePtr, $tag, $val,
263 DataPos => $offset,
264 DataPt => \$buff,
265 Start => $valPos,
266 Size => $valLen,
267 Index => $i,
268 );
269 28 $pos = $nxtPos; # step to next dictionary entry
270 }
271 }
272 1 return 1;
273 }
274  
275 1 1; # end
276  
277 __END__
« File/RandomAccess.pm ↑ Summary Image/ExifTool/AIFF.pm »