Quiq/File/Audio.pm

filter: Statementstmt 27.5% 19 / 69 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.5% 6 / 19 Podpod 100% 8 / 8 totaltotal 32.3% 33 / 102 CC 4 SCAR 16.1
CC
4
Cov
25%
analyzeFile
20.0
new
2.0
artist
2.0
CRAP 5.0
« Quiq/Chrome.pm ↑ Summary Quiq/Html/Widget/RadioButton.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::File::Audio - Informationen über Audio-Datei
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =cut
14  
15 # -----------------------------------------------------------------------------
16  
17 package Quiq::File::Audio;
18 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.240';
25  
26 1 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # -----------------------------------------------------------------------------
30  
31 =head1 METHODS
32  
33 =head2 Konstruktor
34  
35 =head3 new() - Instantiiere Audio-Datei-Objekt
36  
37 =head4 Synopsis
38  
39 $aud = $class->new($file);
40  
41 =head4 Description
42  
43 Instantiiere ein Audio-Datei-Objekt und liefere eine Referenz auf
44 dieses Objekt zurück.
45  
46 =cut
47  
48 # -----------------------------------------------------------------------------
49  
50 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
51 0 my ($class,$file) = @_;
52  
53 0 return $class->SUPER::new(
54 file => $file,
55 artist => undef,
56 title => undef,
57 duration => undef,
58 bitrate => undef,
59 extension => undef,
60 );
61 }
62  
63 # -----------------------------------------------------------------------------
64  
65 =head2 Meta-Information
66  
67 =head3 artist() - Künstler
68  
69 =head4 Synopsis
70  
71 $artist = $aud->artist;
72  
73 =head4 Description
74  
75 Liefere den Künstler der Audio-Datei.
76  
77 =head4 See Also
78  
79 L<extractMetaData|"extractMetaData() - Ermittele Künstler und Titel">()
80  
81 =cut
82  
83 # -----------------------------------------------------------------------------
84  
85 0 ▶sub artist {
Subroutine artist: not called CC=1 SCAR=6.9
Pod documented
86 0 my $self = shift;
87  
88 0 ▶ return $self->memoize('artist',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 my ($self,$key) = @_;
90 0 $self->extractMetaData;
91 0 return $self->get($key);
92 0 });
93 }
94  
95 # -----------------------------------------------------------------------------
96  
97 =head3 title() - Titel
98  
99 =head4 Synopsis
100  
101 $title = $aud->title;
102  
103 =head4 Description
104  
105 Liefere den Titel der Audio-Datei.
106  
107 =head4 See Also
108  
109 L<extractMetaData|"extractMetaData() - Ermittele Künstler und Titel">()
110  
111 =cut
112  
113 # -----------------------------------------------------------------------------
114  
115 0 ▶sub title {
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117  
118 0 ▶ return $self->memoize('title',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 my ($self,$key) = @_;
120 0 $self->extractMetaData;
121 0 return $self->get($key);
122 0 });
123 }
124  
125 # -----------------------------------------------------------------------------
126  
127 =head2 Objektmethoden
128  
129 =head3 bitrate() - Bitrate
130  
131 =head4 Synopsis
132  
133 $bitrate = $aud->bitrate;
134  
135 =head4 Description
136  
137 Liefere die Bitrate der Audion-Datei (z.Zt. als Zeichenkette).
138  
139 =cut
140  
141 # -----------------------------------------------------------------------------
142  
143 0 ▶sub bitrate {
Subroutine bitrate: not called CC=1 SCAR=6.9
Pod documented
144 0 my $self = shift;
145  
146 0 ▶ return $self->memoize('bitrate',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 my ($self,$key) = @_;
148 0 $self->analyzeFile;
149 0 return $self->get($key);
150 0 });
151 }
152  
153 # -----------------------------------------------------------------------------
154  
155 =head3 duration() - Dauer
156  
157 =head4 Synopsis
158  
159 $duration = $aud->duration;
160  
161 =head4 Description
162  
163 Liefere die Länge (Dauer) der Audio-Datei in Sekunden
164 (ggf. mit Nachkommastellen).
165  
166 =cut
167  
168 # -----------------------------------------------------------------------------
169  
170 0 ▶sub duration {
Subroutine duration: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172  
173 0 ▶ return $self->memoize('duration',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 my ($self,$key) = @_;
175 0 $self->analyzeFile;
176 0 return $self->get($key);
177 0 });
178 }
179  
180 # -----------------------------------------------------------------------------
181  
182 =head3 extension() - Extension
183  
184 =head4 Synopsis
185  
186 $ext = $aud->extension;
187  
188 =head4 Description
189  
190 Liefere die Extension der Audio-Datei.
191  
192 =head4 See Also
193  
194 L<extractMetaData|"extractMetaData() - Ermittele Künstler und Titel">()
195  
196 =cut
197  
198 # -----------------------------------------------------------------------------
199  
200 0 ▶sub extension {
Subroutine extension: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202  
203 0 ▶ return $self->memoize('extension',sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 my ($self,$key) = @_;
205 0 $self->extractMetaData;
206 0 return $self->get($key);
207 0 });
208 }
209  
210 # -----------------------------------------------------------------------------
211  
212 =head3 file() - Dateiname
213  
214 =head4 Synopsis
215  
216 $file = $aud->file;
217  
218 =head4 Description
219  
220 Liefere den Dateinamen (Pfad) der Audio-Datei.
221  
222 =head2 Interne Methoden
223  
224 =head3 analyzeFile() - Analysiere Audio-Datei
225  
226 =head4 Synopsis
227  
228 $aud->analyzeFile;
229  
230 =head4 Description
231  
232 Analysiere die Audio-Datei mit ffprobe und weise die ermittelten
233 Eigenschaften an die betreffenden Attribute des Objektes zu.
234  
235 =cut
236  
237 # -----------------------------------------------------------------------------
238  
239 0 ▶sub analyzeFile {
Subroutine analyzeFile: not called CC=4 SCAR=30.0
Pod documented
240 0 my $self = shift;
241  
242 0 ▶ if (!defined $self->get('duration')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get("duration")) 0 0 0
243 0 my $file = $self->file;
244 0 $file =~ s/([\$"])/\\$1/g;
245 0 my $cmd = sprintf 'ffprobe "%s"',$file;
246 0 my $outp = Quiq::Shell->exec($cmd,
247 -capture => 'stdout+stderr',
248 -sloppy => 1,
249 );
250  
251 # Duration
252  
253 0 my $duration = 0;
254 0 $outp =~ /\bDuration:\s+([\d:.]+)/i;
255 0 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 0 0 0
256 0 my ($h,$m,$s) = split /:/,$1;
257 0 $duration = $h*3600+$m*60+$s;
258 }
259 0 $self->set(duration=>$duration);
260  
261 # Bitrate
262  
263 0 my $bitrate = '';
264 0 $outp =~ /\bBitrate:\s*(.*)/i;
265 0 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 0 0 0
266 0 $bitrate = $1;
267 }
268 0 $self->set(bitrate=>$bitrate);
269 }
270  
271 0 return;
272 }
273  
274 # -----------------------------------------------------------------------------
275  
276 =head3 extractMetaData() - Ermittele Künstler und Titel
277  
278 =head4 Synopsis
279  
280 $aud->extractMetaData;
281  
282 =head4 Description
283  
284 Zerlege den Dateiname in die Komponenten <Artist> und <Title>
285 und weise sie den betreffenden Objektattributen zu.
286  
287 Es wird vorausgesetzt, dass der Dateiname folgenden Aufbau hat:
288  
289 <Path>/<Artist> - <Title>.<Extension>
290  
291 Die Methode liefert keinen Wert zurück.
292  
293 =cut
294  
295 # -----------------------------------------------------------------------------
296  
297 0 ▶sub extractMetaData {
Subroutine extractMetaData: not called CC=1 SCAR=6.9
Pod documented
298 0 my $self = shift;
299  
300 0 my (undef,undef,$basename,$ext) = Quiq::Path->split($self->file);
301 0 my ($artist,$title) = split / - /,$basename;
302 0 $self->set(
303 artist => $artist,
304 title => $title,
305 extension => $ext,
306 );
307  
308 0 return;
309 }
310  
311 # -----------------------------------------------------------------------------
312  
313 =head1 VERSION
314  
315 1.240
316  
317 =head1 AUTHOR
318  
319 Frank Seitz, L<http://fseitz.de/>
320  
321 =head1 COPYRIGHT
322  
323 Copyright (C) 2026 Frank Seitz
324  
325 =head1 LICENSE
326  
327 This code is free software; you can redistribute it and/or modify
328 it under the same terms as Perl itself.
329  
330 =cut
331  
332 # -----------------------------------------------------------------------------
333  
334 1 1;
335  
336 # eof
« Quiq/Chrome.pm ↑ Summary Quiq/Html/Widget/RadioButton.pm »