Quiq/FFmpeg.pm

filter: Statementstmt 61.3% 184 / 300 Branchbran 46.3% 50 / 108 Conditioncond 30.5% 18 / 59 MC/DCmcdc 13.5% 5 / 37 Subroutinesub 81.2% 26 / 32 Podpod 100% 23 / 23 totaltotal 54.7% 306 / 559 CC 77 SCAR 32.8
CC
77
Cov
54%
extract169To43
110.0
extract
90.0
suffix
42.0
CRAP 26.6
« Quiq/Udl.pm ↑ Summary Quiq/Object.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::FFmpeg - Konstruiere eine FFmpeg-Kommandozeile
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 FFmpg Online-Dokumentation:
16 L<https://www.ffmpeg.org/ffmpeg-all.html>
17  
18 Ein Objekt der Klasse repräsentiert eine ffmpeg-Kommandozeile.
19 Die Klasse verfügt einerseits über I<elementare> (Objekt-)Methoden,
20 um eine solche Kommandozeile sukzessive aus Eingabe- und
21 Ausgabe-Dateien, Optionen, Filtern usw. zu konstruieren und
22 andererseits I<höhere> (Klassen-)Methoden, die eine vollständige
23 Kommandozeile zur Erfüllung eines bestimmten Zwecks unter
24 Rückgriff auf die elementaren Methoden erstellen. Die höheren
25 Methoden Methoden befinden sich im Abschnitt L<Klassenmethoden (vollständige Kommandozeilen)|"Klassenmethoden (vollständige Kommandozeilen)">.
26  
27 =cut
28  
29 # -----------------------------------------------------------------------------
30  
31 package Quiq::FFmpeg;
32 3 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 3 our $VERSION = '1.240';
39  
40 3 ▶use Quiq::File::Video;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Quiq::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # -----------------------------------------------------------------------------
47  
48 =head1 METHODS
49  
50 =head2 Konstruktor
51  
52 =head3 new() - Konstruktor
53  
54 =head4 Synopsis
55  
56 $cmd = $class->new;
57 $cmd = $class->new($str);
58  
59 =head4 Description
60  
61 Instantiiere ein FFmpeg-Kommando-Objekt und liefere eine Referenz auf
62 dieses Objekt zurück.
63  
64 =cut
65  
66 # -----------------------------------------------------------------------------
67  
68 24 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
69 24 my ($class,$str) = @_;
70  
71 24 ▶ return $class->SUPER::new(
Condition100%2/2
$str // ""
l!l
123
MC/DC100%1/1
$str // ""
$str
Truth table100%2/2
A
A= $str
Aresult
11
00
72 cmd => $str // '',
73 inputA => [],
74 inputObjectA => [],
75 outputA => [],
76 outName => undef,
77 outWidth => undef,
78 outHeight => undef,
79 outStart => undef,
80 outStop => undef,
81 );
82 }
83
84  
85 # -----------------------------------------------------------------------------
86  
87 =head2 Objektmethoden
88  
89 =head3 command() - Kommandozeile als Zeichenkette
90  
91 =head4 Synopsis
92  
93 $str = $cmd->command;
94  
95 =head4 Description
96  
97 Liefere das Kommando als Zeichenkette.
98  
99 =cut
100  
101 # -----------------------------------------------------------------------------
102  
103 20 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
104 20 return shift->{'cmd'};
105 }
106
107  
108 # -----------------------------------------------------------------------------
109  
110 =head3 input() - Eingabe-Datei als Objekt
111  
112 =head4 Synopsis
113  
114 $fil = $cmd->input($i);
115  
116 =head4 Description
117  
118 Instantiiere Eingabe-Datei $i als Quiq::File-Objekt und liefere
119 dieses zurück. Das Objekt wird gecached.
120  
121 =cut
122  
123 # -----------------------------------------------------------------------------
124  
125 0 ▶sub input {
Subroutine input: not called CC=3 SCAR=24.8
Pod documented
126 0 my ($self,$i) = @_;
127  
128 0 my $fil = $self->inputObjectA->[$i];
129 0 ▶ if (!$fil) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fil) 0 0 0
130 0 ▶ if (my $input = $self->inputA->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $input = $self->inputA->[$i]) 0 0 0
131 0 $fil = Quiq::File::Video->new($input);
132 0 $self->inputObjectA->[$i] = $fil; # cachen
133 }
134 }
135  
136 0 return $fil;
137 }
138
139  
140 # -----------------------------------------------------------------------------
141  
142 =head3 suffix() - Suffix Ausgabe-Datei
143  
144 =head4 Synopsis
145  
146 $str = $cmd->suffix;
147  
148 =head4 Description
149  
150 Liefere den Suffix für eine Ausgabedatei. Der Suffix ist eine
151 Zeichenkette der Form
152  
153 NAME-WIDTH-HEIGHT-START-STOP
154  
155 wobei Komponenten fehlen können, die nicht definiert sind.
156  
157 =cut
158  
159 # -----------------------------------------------------------------------------
160  
161 0 ▶sub suffix {
Subroutine suffix: not called CC=6 SCAR=37.4
Pod documented
162 0 my $self = shift;
163  
164 0 my @suffix;
165  
166 0 my ($outName,$outWidth,$outHeight,$outStart,$outStop) =
167 $self->get(qw/outName outWidth outHeight outStart outStop/);
168
169 # name
170
171 0 ▶ if ($outName) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outName) 0 0 0
172 0 push @suffix,$outName;
173 }
174  
175 # size
176
177 0 ▶ if ($outWidth && $outHeight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outWidth and $outHeight) 0 0 0
Condition0%0/3
$outWidth and $outHeight
!ll&&!rl&&r
000
MC/DC0%0/2
$outWidth and $outHeight
$outWidth $outHeight
Truth table0%0/3
A and B
A= $outWidthB= $outHeight
ABresult
0X0
100
111
178 0 push @suffix,sprintf('%04dx%04d',$outWidth,$outHeight);
179 }
180  
181 # start, stop
182  
183 0 ▶ if (defined($outStart) && $outStop) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outStart and $outStop) 0 0 0
Condition0%0/3
defined $outStart and $outStop
!ll&&!rl&&r
000
MC/DC0%0/2
defined $outStart and $outStop
defined $outStart $outStop
Truth table0%0/3
A and B
A= defined $outStartB= $outStop
ABresult
0X0
100
111
184 0 push @suffix,sprintf '%03d-%03d',POSIX::floor($outStart),
185 POSIX::ceil($outStop);
186 }
187
188 0 return join '-',@suffix;
189 }
190
191  
192 # -----------------------------------------------------------------------------
193  
194 =head2 Kommandozeile konstruieren
195  
196 =head3 addOption() - Füge Option hinzu
197  
198 =head4 Synopsis
199  
200 $cmd->addOption($opt);
201 $cmd->addOption($opt=>$val);
202  
203 =head4 Description
204  
205 Ergänze die Kommandozeile um die Option $opt und (optional) den
206 Wert $val. Die Methode liefert keinen Wert zurück.
207  
208 =head4 Examples
209  
210 Option ohne Wert:
211  
212 $cmd->addOption('-y');
213 =>
214 -y
215  
216 Option mit Wert:
217  
218 $cmd->addOption(-i=>'video/GOPR1409.mp4');
219 =>
220 -i 'video/GOPR1409.mp4'
221  
222 =cut
223  
224 # -----------------------------------------------------------------------------
225  
226 44 ▶sub addOption {
Subroutine addOption: called CC=7 SCAR=19.6
Pod documented
227 44 my $self = shift;
228 44 my $opt = shift;
229 # @_: $val
230  
231 44 my $ref = $self->getRef('cmd');
232 44 my $val;
233 44 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 41 3 44
234 41 $val = shift;
235 41 ▶ if (!defined($val) || $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or $val eq "") 0 41 41
Condition33%1/3
not defined $val or $val eq ""
l!l&&r!l&&!r
0041
MC/DC0%0/2
not defined $val or $val eq ""
not defined $val $val eq ""
Truth table33%1/3
not(A) or B
A= not defined $valB= $val eq ""
ABresult
1X1
011
000
236 # Keine Option hinzufügen, wenn Wert-Argument,
237 # angegeben, dieses aber undef oder '' ist
238 0 return;
239 }
240 }
241  
242 44 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 30 14 44
243 30 $$ref .= ' ';
244 }
245 44 $$ref .= $opt;
246  
247 44 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 41 3 44
248 41 $$ref .= ' ';
249 41 ▶ if ($val =~ /^([0-9.]+|[A-Za-z0-9:]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([0-9.]+|[A-Za-z0-9:]+)$/) { } 26 15 41
250 26 $$ref .= $val;
251 }
252 else {
253 15 $$ref .= "'$val'";
254 }
255 }
256
257 44 return;
258 }
259  
260 # -----------------------------------------------------------------------------
261  
262 =head3 addInput() - Füge Input-Option hinzu
263  
264 =head4 Synopsis
265  
266 $cmd->addInput($input);
267  
268 =head4 Description
269  
270 Ergänze das Kommando um Input $input, sofern $input einen Wert hat.
271 Die Methode liefert keinen Wert zurück.
272  
273 =head4 Examples
274  
275 Dateiname:
276  
277 $cmd->addInput('video/GOPR1409.mp4');
278 =>
279 -i 'video/GOPR1409.mp4'
280  
281 Muster:
282  
283 $cmd->addInput('img/*.jpg');
284 =>
285 -i 'img/*.jpg'
286  
287 Undefiniert:
288  
289 $cmd->addInput(undef);
290 =>
291  
292 =cut
293  
294 # -----------------------------------------------------------------------------
295  
296 9 ▶sub addInput {
Subroutine addInput: called CC=2 SCAR=7.0
Pod documented
297 9 my ($self,$input) = @_;
298  
299 9 ▶ if ($input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) 9 0 9
300 9 $self->addOption(-i=>$input);
301 9 $self->push(inputA=>$input);
302 }
303
304 9 return;
305 }
306  
307 # -----------------------------------------------------------------------------
308  
309 =head3 addFilter() - Füge Filter-Option hinzu
310  
311 =head4 Synopsis
312  
313 $cmd->addFilter($opt,\@filter,$sep);
314  
315 =head4 Description
316  
317 Ergänze das Kommando um Filter-Option $opt mit den Filtern @filter
318 und dem Trenner $sep (Komma oder Semikolon).
319  
320 =head4 Examples
321  
322 Video Filter-Chain:
323  
324 $cmd->addFilter(-vf=>['crop=1440:1080','scale=720*a:720']);
325 =>
326 -vf 'crop=1440:1080,scale=720*a:720'
327  
328 =cut
329  
330 # -----------------------------------------------------------------------------
331  
332 7 ▶sub addFilter {
Subroutine addFilter: called CC=2 SCAR=6.9
Pod documented
333 7 my ($self,$opt,$filterA,$sep) = @_;
334  
335 7 ▶ if (my $val = join($sep,@$filterA)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = join($sep, @$filterA)) 5 2 7
336 5 $self->addOption($opt=>$val);
337 }
338
339 7 return;
340 }
341  
342 # -----------------------------------------------------------------------------
343  
344 =head3 addStartStop() - Füge Optionen für Zeitbereich hinzu
345  
346 =head4 Synopsis
347  
348 $cmd->addStartStop($start,$stop);
349  
350 =head4 Description
351  
352 Ergänze das Kommando um Optionen, die den Zeitbereich auf
353 den Bereich $start und $stop eingrenzen.
354  
355 =head4 Examples
356  
357 Nur Start-Zeitpunkt:
358  
359 $cmd->addStartStop(5.5);
360 =>
361 -ss 5.5
362  
363 Nur Ende-Zeitpunkt:
364  
365 $cmd->addStartStop(undef,20.5);
366 =>
367 -t 20.5
368  
369 Start- und Ende-Zeitpunkt:
370  
371 $cmd->addStartStop(5.5,20.5);
372 =>
373 -ss 5.5 -t 20.5
374  
375 =cut
376  
377 # -----------------------------------------------------------------------------
378  
379 8 ▶sub addStartStop {
Subroutine addStartStop: called CC=5 SCAR=16.1
Pod documented
380 8 my ($self,$start,$stop) = @_;
381  
382 8 ▶ if ($start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start) 4 4 8
383 4 $self->addOption(-ss=>$start);
384 }
385
386 8 ▶ $start ||= 0;
Condition100%2/2
$start ||= 0
l!l
44
MC/DC100%1/1
$start ||= 0
$start
Truth table100%2/2
A
A= $start
Aresult
11
00
387 8 ▶ if ($stop && $stop > $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop and $stop > $start) 4 4 8
Condition66%2/3
$stop and $stop > $start
!ll&&!rl&&r
404
MC/DC50%1/2
$stop and $stop > $start
$stop $stop > $start
Truth table66%2/3
A and B
A= $stopB= $stop > $start
ABresult
0X0
100
111
388 4 $self->addOption(-t=>$stop-$start);
389 }
390
391 8 return;
392 }
393  
394 # -----------------------------------------------------------------------------
395  
396 =head3 addBitrate() - Füge Bitrate-Option hinzu
397  
398 =head4 Synopsis
399  
400 $cmd->addBitrate($bitrate);
401  
402 =head4 Description
403  
404 Ergänze das Kommando um eine allgemeine Bitrate-Option mit
405 Suffix 'k' (= kb/s). Ist die Bitrate 0, '' oder undef, wird
406 die Option nicht hinzugefügt.
407  
408 =head4 Examples
409  
410 Bitrate von 10000k:
411  
412 $cmd->addBitrate(10000);
413 =>
414 -b 10000k
415  
416 =cut
417  
418 # -----------------------------------------------------------------------------
419  
420 2 ▶sub addBitrate {
Subroutine addBitrate: called CC=2 SCAR=6.9
Pod documented
421 2 my ($self,$bitrate) = @_;
422  
423 2 ▶ if ($bitrate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitrate) 1 1 2
424 1 $self->addOption(-b=>$bitrate.'k');
425 }
426
427 2 return;
428 }
429  
430 # -----------------------------------------------------------------------------
431  
432 =head3 addOutput() - Füge Output-Argument hinzu
433  
434 =head4 Synopsis
435  
436 $cmd->addOutput($output);
437  
438 =head4 Description
439  
440 Ergänze das Kommando um Output $output.
441 Die Methode liefert keinen Wert zurück.
442  
443 =head4 Examples
444  
445 Dateiname:
446  
447 $cmd->addOutput('video/GOPR1409.mp4');
448 =>
449 'video/GOPR1409.mp4'
450  
451 Muster:
452  
453 $cmd->addOutput('img/%06d.jpg');
454 =>
455 'img/%06d.jpg'
456  
457 =cut
458  
459 # -----------------------------------------------------------------------------
460  
461 7 ▶sub addOutput {
Subroutine addOutput: called CC=4 SCAR=15.4
Pod documented
462 7 my ($self,$out) = @_;
463  
464 7 ▶ if (!defined $out || $out eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $out or $out eq '') { } 0 7 7
Condition33%1/3
not defined $out or $out eq ""
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $out or $out eq ""
not defined $out $out eq ""
Truth table33%1/3
not(A) or B
A= not defined $outB= $out eq ""
ABresult
1X1
011
000
465 # Dateiname generieren
466  
467 0 my $inputA = $self->get('inputA');
468 0 my ($path,$ext) = $inputA->[0] =~ /(.+?)\.([^.]+)$/;
469 0 $out = sprintf '%s-%s.%s',$path,$self->suffix,$ext;
470 }
471 else {
472 # Suffix einsetzen
473 0 $out =~ s/%S/$self->suffix/e;
474 }
475  
476 7 my $ref = $self->getRef('cmd');
477 7 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 6 1 7
478 6 $$ref .= ' ';
479 }
480 7 $$ref .= qq|'$out'|;
481
482 7 $self->push(outputA=>$out);
483
484 7 return;
485 }
486  
487 # -----------------------------------------------------------------------------
488  
489 =head3 addString() - Füge Zeichenkette am Ende hinzu
490  
491 =head4 Synopsis
492  
493 $cmd->addString($str);
494  
495 =head4 Description
496  
497 Füge Zeichenkette $str am Ende der Kommandozeile hinzu, mit einem
498 Leerzeichen als Trenner.
499  
500 =head4 Example
501  
502 Kommando nach Objekt-Instantiierung:
503  
504 $cmd->addString('ffprobe');
505 =>
506 ffprobe
507  
508 =cut
509  
510 # -----------------------------------------------------------------------------
511  
512 3 ▶sub addString {
Subroutine addString: called CC=2 SCAR=6.9
Pod documented
513 3 my ($self,$str) = @_;
514  
515 3 my $ref = $self->getRef('cmd');
516 3 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 1 2 3
517 1 $$ref .= ' ';
518 }
519 3 $$ref .= $str;
520
521 3 return;
522 }
523  
524 # -----------------------------------------------------------------------------
525  
526 =head3 prependString() - Füge Zeichenkette am Anfang hinzu
527  
528 =head4 Synopsis
529  
530 $cmd->prependString($str);
531  
532 =head4 Description
533  
534 Füge Zeichenkette $str am Beginn der Kommandozeile hinzu.
535 Ein Leerzeichen wird automatisch hinzugefügt.
536  
537 =head4 Example
538  
539 Kommando voranstellen:
540  
541 $cmd->prependString('ffplay -autoexit');
542 =>
543 ffplay -autoexit ...
544  
545 =cut
546  
547 # -----------------------------------------------------------------------------
548  
549 6 ▶sub prependString {
Subroutine prependString: called CC=2 SCAR=7.0
Pod documented
550 6 my ($self,$str) = @_;
551  
552 6 my $ref = $self->getRef('cmd');
553 6 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 6 0 6
554 6 $$ref = ' '.$$ref;
555 }
556 6 $$ref = $str.$$ref;
557
558 6 return;
559 }
560  
561 # -----------------------------------------------------------------------------
562  
563 =head2 Filter
564  
565 =head3 cropFilter() - Liefere Crop-Filter
566  
567 =head4 Synopsis
568  
569 $str = $cmd->cropFilter($width,$height);
570 $str = $cmd->cropFilter($width,$height,$xOffset,$yOffset);
571  
572 =head4 Description
573  
574 Erzeuge eine Crop-Filter-Spezifikation für die angegebenen
575 Argumente und liefere diese zurück.
576  
577 =head4 Examples
578  
579 Nur Breite und Höhe:
580  
581 $cmd->cropFilter(1280,720);
582 =>
583 'crop=1280:720'
584  
585 Breite, Höhe, X-Offset, Y-Offset:
586  
587 $cmd->cropFilter(1280,720,240,0);
588 =>
589 'crop=1280:720:240:0'
590  
591 =cut
592  
593 # -----------------------------------------------------------------------------
594  
595 5 ▶sub cropFilter {
Subroutine cropFilter: called CC=4 SCAR=14.0
Pod documented
596 5 my $self = shift;
597 5 my $width = shift;
598 5 my $height = shift;
599 # @_: $xOffset,$yOffset
600  
601 5 my $str = "crop=$width:$height";
602 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 4 5
603 1 ▶ my $xOffset = shift || 0;
Condition50%1/2
shift() || 0
l!l
10
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
604 1 ▶ my $yOffset = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
605 1 $str .= ":$xOffset:$yOffset";
606 }
607  
608 5 return $str;
609 }
610
611  
612 # -----------------------------------------------------------------------------
613  
614 =head3 scaleFilter() - Liefere Scale-Filter
615  
616 =head4 Synopsis
617  
618 $str = $cmd->scaleFilter($width,$height);
619 $str = $cmd->scaleFilter("$width:$height");
620  
621 =head4 Description
622  
623 Erzeuge eine Crop-Filter-Spezifikation für die angegebenen
624 Argumente und liefere diese zurück.
625  
626 Sind die Argumente undefiniert, wird eine leere Liste geliefert.
627  
628 =head4 Examples
629  
630 Breite und Höhe als getrennte Argumente:
631  
632 $cmd->scaleFilter(1280,720);
633 =>
634 'scale=1280:720'
635  
636 Breite und Höhe in einem Argument:
637  
638 $cmd->scaleFilter('1280:720');
639 =>
640 'scale=1280:720'
641  
642 Undefiniertes Argument:
643  
644 @filter = $cmd->scaleFilter(undef);
645 =>
646 ()
647  
648 =cut
649  
650 # -----------------------------------------------------------------------------
651  
652 4 ▶sub scaleFilter {
Subroutine scaleFilter: called CC=5 SCAR=16.3
Pod documented
653 4 my $self = shift;
654 # @_: $width,$height -or- "$width,$height"
655  
656 4 my $size;
657 4 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 3 1 4
658 3 $size = shift;
659 }
660 else {
661 1 ▶ if (defined(my $width = shift) && defined(my $height = shift)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $width = shift()) and defined(my $height = shift())) 1 0 1
Condition33%1/3
defined(my $width = shift()) and defined(my $height = shift())
!ll&&!rl&&r
001
MC/DC0%0/2
defined(my $width = shift()) and defined(my $height = shift())
defined(my $width = shift()) defined(my $height = shift())
Truth table33%1/3
A and B
A= defined(my $width = shift())B= defined(my $height = shift())
ABresult
0X0
100
111
662 1 $size = "$width:$height";
663 }
664 }
665  
666 4 ▶ if (!defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 2 2 4
667 2 return ();
668 }
669  
670 2 return "scale=$size";
671 }
672
673  
674 # -----------------------------------------------------------------------------
675  
676 =head3 fpsFilter() - Liefere Fps-Filter
677  
678 =head4 Synopsis
679  
680 $str = $cmd->fpsFilter($fps);
681  
682 =head4 Description
683  
684 Erzeuge eine Fps-Filter-Spezifikation und liefere diese zurück.
685 Ist das Argument undef, liefere eine leere Liste.
686  
687 =head4 Examples
688  
689 Argument:
690  
691 $cmd->fpsFilter(24);
692 =>
693 'fps=24'
694  
695 Undefiniertes Argument:
696  
697 @filter = $cmd->fpsFilter(undef);
698 =>
699 ()
700  
701 =cut
702  
703 # -----------------------------------------------------------------------------
704  
705 3 ▶sub fpsFilter {
Subroutine fpsFilter: called CC=2 SCAR=6.9
Pod documented
706 3 my ($self,$fps) = @_;
707 3 ▶ return !defined $fps? (): "fps=$fps";
Branch: 1 branch
BranchTrueFalseTotal
!defined($fps) ? : 1 2 3
708 }
709
710  
711 # -----------------------------------------------------------------------------
712  
713 =head3 framestepFilter() - Liefere Framestep-Filter
714  
715 =head4 Synopsis
716  
717 $str = $cmd->framestepFilter($fps);
718  
719 =head4 Description
720  
721 Erzeuge eine Framestep-Filter-Spezifikation und liefere diese zurück.
722 Ist das Argument undef, liefere eine leere Liste.
723  
724 =head4 Examples
725  
726 Argument:
727  
728 $cmd->framestepFilter(4);
729 =>
730 'framestep=4'
731  
732 Undefiniertes Argument:
733  
734 @filter = $cmd->framestepFilter(undef);
735 =>
736 ()
737  
738 =cut
739  
740 # -----------------------------------------------------------------------------
741  
742 7 ▶sub framestepFilter {
Subroutine framestepFilter: called CC=3 SCAR=11.0
Pod documented
743 7 my ($self,$framestep) = @_;
744  
745 7 ▶ if (!defined($framestep) || $framestep == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $framestep or $framestep == 1) 4 3 7
Condition100%3/3
not defined $framestep or $framestep == 1
l!l&&r!l&&!r
133
MC/DC100%2/2
not defined $framestep or $framestep == 1
not defined $framestep $framestep == 1
Truth table100%3/3
not(A) or B
A= not defined $framestepB= $framestep == 1
ABresult
1X1
011
000
746 4 return ();
747 }
748
749 3 return "framestep=$framestep";
750 }
751
752  
753 # -----------------------------------------------------------------------------
754  
755 =head2 Ausgabe-Datei-Eigenschaften (Getter/Setter)
756  
757 =head3 outName() - Setze/Liefere Bezeichnung Ausgabe-Datei
758  
759 =head4 Synopsis
760  
761 $cmd->outName($name);
762 $name = $cmd->outName;
763  
764 =head4 Description
765  
766 Setze oder liefere die Bezeichnung für die Ausgabe-Datei.
767 Die Angabe wird für den Suffix der Ausgabe-Datei genutzt.
768  
769 =head3 outSize() - Setze/Liefere Breite und Höhe Video-Ausgabe
770  
771 =head4 Synopsis
772  
773 $cmd->outSize($width,$height);
774 ($width,$height) = $cmd->outSize;
775  
776 =head4 Description
777  
778 Setze oder liefere die Höhe und Breite der Video-Ausgabe.
779 Die Angabe wird für den Suffix der Video-Ausgabe-Datei genutzt.
780  
781 =cut
782  
783 # -----------------------------------------------------------------------------
784  
785 0 ▶sub outSize {
Subroutine outSize: not called CC=2 SCAR=17.9
Pod documented
786 0 my $self = shift;
787 # @_: $width,$height
788  
789 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
790 0 $self->{'outWidth'} = shift;
791 0 $self->{'outHeight'} = shift;
792 }
793  
794 0 return ($self->{'outWidth'},$self->{'outHeight'});
795 }
796
797  
798 # -----------------------------------------------------------------------------
799  
800 =head3 outStart() - Setze/Liefere Start-Zeitpunkt
801  
802 =head4 Synopsis
803  
804 $cmd->outStart($s);
805 $s = $cmd->outStart;
806  
807 =head4 Description
808  
809 Setze oder liefere den Start-Zeitpunkt der Ausgabe.
810 Die Angabe wird für den Suffix der Ausgabe-Datei genutzt.
811  
812 =head3 outStop() - Setze/Liefere Stop-Zeitpunkt
813  
814 =head4 Synopsis
815  
816 $cmd->outStop($s);
817 $s = $cmd->outStop;
818  
819 =head4 Description
820  
821 Setze oder liefere den Stop-Zeitpunkt der Ausgabe.
822 Die Angabe wird für den Suffix der Ausgabe-Datei genutzt.
823  
824 =head2 Klassenmethoden (vollständige Kommandozeilen)
825  
826 =head3 imagesToVideo() - Füge Bild-Sequenz zu einem Video zusammen
827  
828 =head4 Synopsis
829  
830 $cmd = $class->imagesToVideo($pattern,$output,@opt);
831  
832 =head4 Arguments
833  
834 =over 4
835  
836 =item $pattern
837  
838 Pfad-Muster der Bilder. Enthält das Pfad-Muster einen Stern (*),
839 wird C<-pattern_type glob> gewählt.
840  
841 Beispiele:
842  
843 'img/%06d.jpg' => -i 'img/%06d.jpg'
844 'img/*.jpg' => -pattern_type glob -i 'img/*.jpg'
845  
846 =item $output
847  
848 Name der generierten Video-Datei.
849  
850 =back
851  
852 =head4 Options
853  
854 =over 4
855  
856 =item -audio => $file (Default: undef)
857  
858 Erzeuge einen Audio-Stream aus Audio-Datei $file.
859  
860 =item -duration => $duration (Default: undef)
861  
862 Beende die Ausgabe, wenn die Dauer $duration erreicht ist.
863  
864 =item -framerate => $n (Default: 8)
865  
866 Anzahl Bilder pro Sekunde.
867  
868 =item -loop => $bool (Default: 0)
869  
870 Wiederhole die Bildserie zyklisch.
871  
872 =item -play => 0|1|2 (Default: 0)
873  
874 Zeige das generierte Video im Player an, statt es in einer Datei
875 zu speichern. Bei -play=>2 bleibt das Fenster des Players
876 offen, bis es per Hand geschlossen wird.
877  
878 =item -preset => $preset (Default: undef)
879  
880 Satz an vorgewählten Optionen, für Encoding-Zeit
881 vs. Kompressionsrate. Mögliche Werte: ultrafast, superfast,
882 veryfast, faster, fast, medium, slow, slower, veryslow. Siehe
883 L<https://trac.ffmpeg.org/wiki/Encode/H.264>.
884  
885 =item -size => "$width:$height" (Default: undef)
886  
887 Breite und Höhe des generierten Video. Ohne Angabe nimmt
888 ffmpeg die Größe der Bilder.
889  
890 =item -videoBitrate => $bitrate (Default: 60_000)
891  
892 Video-Bitrate in kbit/s.
893  
894 =item -videoFilter => $filter
895  
896 Optionale Filterangabe. Z.B. -videoFilter => 'lutyuv=y=val*1.4,hue=s=10'
897  
898 =item -videoFramerate => $n (Default: 24)
899  
900 Framerate des Video.
901  
902 =back
903  
904 =head4 Description
905  
906 Generiere ein ffmpeg-Kommando zum Zusammenfügen der Bilder
907 $pattern zu Video $output und liefere dieses Kommando zurück.
908  
909 =cut
910  
911 # -----------------------------------------------------------------------------
912  
913 1 ▶sub imagesToVideo {
Subroutine imagesToVideo: called CC=15 SCAR=32.8
Pod documented
914 1 my $class = shift;
915 # @_: $pattern,$output,@opt
916  
917 # Optionen und Argumente
918  
919 1 my $audio = undef;
920 1 my $duration = undef,
921 my $framerate = 8;
922 1 my $loop = 0;
923 1 my $play = 0;
924 1 my $preset = undef;
925 1 my $size = undef;
926 1 my $videoBitrate = 60_000;
927 1 my $videoFilter = undef;
928 1 my $videoFramerate = 24;
929  
930 1 Quiq::Option->extract(\@_,
931 -audio => \$audio,
932 -duration => \$duration,
933 -framerate => \$framerate,
934 -loop => \$loop,
935 -play => \$play,
936 -preset => \$preset,
937 -size => \$size,
938 -videoBitrate => \$videoBitrate,
939 -videoFilter => \$videoFilter,
940 -videoFramerate => \$videoFramerate,
941 );
942 1 ▶ if (@_ == 0 || @_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 or @_ > 2) 0 1 1
Condition33%1/3
@_ == 0 or @_ > 2
l!l&&r!l&&!r
001
MC/DC0%0/2
@_ == 0 or @_ > 2
@_ == 0 @_ > 2
Truth table33%1/3
A or B
A= @_ == 0B= @_ > 2
ABresult
1X1
011
000
943 0 $class->throw('Usage: $ffm->imagesToVideo($input,$output,@opt)');
944 }
945 1 my $pattern = shift;
946 1 my $output = shift;
947  
948 # Operation ausführen
949  
950 # * Command-Objekt instantiieren
951 1 my $self = $class->new('ffmpeg -y');
952
953 # * Input
954  
955 1 ▶ if ($loop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop) 0 1 1
956 0 $self->addOption(-loop=>$loop);
957 }
958 1 $self->addOption(-framerate=>$framerate);
959 1 $self->addOption(-f=>'image2');
960 1 ▶ if ($pattern =~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\*/) 1 0 1
961 1 $self->addOption(-pattern_type=>'glob');
962 }
963 1 $self->addInput($pattern);
964  
965 1 ▶ if ($audio) {
Branch: 1 branch
BranchTrueFalseTotal
if ($audio) 0 1 1
966 0 $self->addInput($audio);
967 }
968
969 # * Filter
970  
971 1 my @filter;
972  
973 # ** fps
974 1 push @filter,$self->fpsFilter($videoFramerate);
975  
976 # ** scale
977  
978 1 push @filter,$self->scaleFilter($size);
979 1 ▶ if ($play && !$size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play and not $size) 0 1 1
Condition33%1/3
$play and not $size
!ll&&!rl&&r
100
MC/DC0%0/2
$play and not $size
$play not $size
Truth table33%1/3
A and not(B)
A= $playB= not $size
ABresult
0X0
100
111
980 0 push @filter,$self->scaleFilter('720*a',720);
981 }
982  
983 # ** YUV color space (laut ffmpeg-Doku aus Kompatibilitätsgründen nötig)
984 1 push @filter,'format=yuv420p';
985  
986 # ** Optionale Video-Filter
987  
988 1 ▶ if ($videoFilter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($videoFilter) 0 1 1
989 0 push @filter,$videoFilter;
990 }
991  
992 1 $self->addFilter(-vf=>\@filter,',');
993  
994 # * Output-Options
995  
996 1 ▶ if (defined $duration) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $duration) { } 0 1 1
elsif ($audio) { } 0 1 1
997 0 $self->addOption(-t=>$duration);
998 }
999 elsif ($audio) {
1000 0 $self->addOption('-shortest');
1001 }
1002  
1003 1 ▶ if (defined $videoBitrate) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $videoBitrate) 1 0 1
1004 1 $self->addOption('-b:v'=>$videoBitrate.'k');
1005 }
1006 1 ▶ if (defined $preset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preset) 0 1 1
1007 0 $self->addOption(-preset=>$preset);
1008 }
1009  
1010 # * Output
1011  
1012 1 ▶ if ($play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play) { } 0 1 1
1013 0 $self->addOption(-f=>'avi');
1014 0 ▶ if ($play == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play == 1) { } 0 0 0
1015 0 $self->addString('- 2>/dev/null | ffplay -autoexit - 2>/dev/null');
1016 }
1017 else {
1018 0 $self->addString('- 2>/dev/null | ffplay - 2>/dev/null');
1019 }
1020 }
1021 else {
1022 1 $self->addOutput($output);
1023 }
1024
1025 1 return $self;
1026 }
1027  
1028 # -----------------------------------------------------------------------------
1029  
1030 =head3 videoToImages() - Extrahiere Bild-Sequenz (Frames) aus Video
1031  
1032 =head4 Synopsis
1033  
1034 $cmd = $ffm->videoToImages($input,$dir,@opt);
1035  
1036 =head4 Options
1037  
1038 =over 4
1039  
1040 =item -aspectRatio => '16:9'|'4:3' (Default: undef)
1041  
1042 Gewünschtes Seitenverhältnis der Bilder. Hat das Video ein
1043 abweichendes Seitenverhältnis, wird ein entsprechender Crop-Filter
1044 aufgesetzt.
1045  
1046 =item -framestep => $n (Default: 1)
1047  
1048 Extrahiere jeden $n-ten Frame.
1049  
1050 =item -pixelFormat=FMT (Default: 'yuvj422p')
1051  
1052 Pixel-Format des erzeugten Bildes. Laut Aussage im Netz ist yuvj422p
1053 das Standard-Pixel-Format für jpeg-Dateien. Wird das Pixel-Format
1054 hier nicht geändert, erzeugt ffmpeg die Bilder in Pixelformat
1055 yuvj420p, was Probleme beim Zusammenfügen mit Bildern einer
1056 Kamera zu einem Film macht.
1057  
1058 =item -quality => $n (Default: 2)
1059  
1060 Qualität der generierten jpg-Bilddateien. Wertebereich: 2-31, mit
1061 2 als bester und 31 als schlechtester Qualität.
1062  
1063 =item -start => $s (Default: 0)
1064  
1065 Zeitpunkt in Sekunden (ggf. mit Nachkommastellen) vom Beginn
1066 des Video, an dem das Extrahieren der Frames beginnt.
1067  
1068 =item -stop => $s (Default: undef)
1069  
1070 Zeitpunkt in Sekunden (ggf. mit Nachkommastellen) vom Beginn
1071 des Video, an dem das Extrahieren der Frames endet.
1072  
1073 =back
1074  
1075 =head4 Description
1076  
1077 Generiere ein ffmpeg-Kommando, das die Frames aus dem Video $input
1078 extrahiert und im Verzeichnis $dir speichert. Die Bilder haben
1079 das Format 'jpg'. Der Name der Dateien ist NNNNNN.jpg, von 1 an
1080 lückenlos aufsteigend.
1081  
1082 =head4 Examples
1083  
1084 Ohne Optionen:
1085  
1086 $ffm->videoToImages('video.mp4','img');
1087 =>
1088 ffmpeg -y -loglevel error -stats
1089 -i 'video.mp4'
1090 -qscale:v 2
1091 'img/%06d.jpg'
1092  
1093 Video-Seitenverhältnis 16:9 zu Bild-Seitenverhältnis 4:3 wandeln:
1094  
1095 $ffm->videoToImages('video.mp4','img',
1096 -aspectRatio => '4:3',
1097 );
1098 =>
1099 ffmpeg -y -loglevel error -stats
1100 -i 'video.mp4'
1101 -vf 'crop=ih/3*4:ih'
1102 -qscale:v 2
1103 'img/%06d.jpg'
1104  
1105 Alle Optionen:
1106  
1107 $ffm->videoToImages('video.mp4','img',
1108 -aspectRatio => '4:3',
1109 -framestep => 6,
1110 -start => 3,
1111 -stop => 10,
1112 );
1113 =>
1114 ffmpeg -y -loglevel error -stats
1115 -i 'video.mp4'
1116 -vf 'framestep=6,crop=ih/3*4:ih'
1117 -ss 3 -t 7
1118 -qscale:v 2
1119 'img/%06d.jpg'
1120  
1121 =cut
1122  
1123 # -----------------------------------------------------------------------------
1124  
1125 5 ▶sub videoToImages {
Subroutine videoToImages: called CC=6 SCAR=18.6
Pod documented
1126 5 my $class = shift;
1127 # @_: $input,$dir,@opt
1128
1129 # Optionen und Argumente
1130  
1131 5 my $aspectRatio = undef;
1132 5 my $framestep = 1;
1133 5 my $pixelFormat = 'yuvj422p';
1134 5 my $quality = 2;
1135 5 my $start = 0;
1136 5 my $stop = undef;
1137
1138 5 Quiq::Option->extract(\@_,
1139 -aspectRatio => \$aspectRatio,
1140 -framestep => \$framestep,
1141 -pixelFormat => \$pixelFormat,
1142 -quality => \$quality,
1143 -start => \$start,
1144 -stop => \$stop,
1145 );
1146 5 ▶ if (@_ == 0 || @_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 or @_ > 2) 0 5 5
Condition33%1/3
@_ == 0 or @_ > 2
l!l&&r!l&&!r
005
MC/DC0%0/2
@_ == 0 or @_ > 2
@_ == 0 @_ > 2
Truth table33%1/3
A or B
A= @_ == 0B= @_ > 2
ABresult
1X1
011
000
1147 0 $class->throw('Usage: $ffm->videoToImages($input,$dir,@opt)');
1148 }
1149 5 my $input = shift;
1150 5 my $dir = shift;
1151  
1152 # Operation ausführen
1153  
1154 # * Command-Objekt instantiieren
1155 5 my $self = $class->new;
1156
1157 # * Input
1158 5 $self->addInput($input);
1159  
1160 # * Filter
1161
1162 5 my @filter;
1163  
1164 # ** framestep
1165  
1166 5 push @filter,$self->framestepFilter($framestep);
1167  
1168 5 ▶ if ($aspectRatio) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aspectRatio) 3 2 5
1169 3 ▶ if ($aspectRatio eq '4:3') {
Branch: 2 branches
BranchTrueFalseTotal
if ($aspectRatio eq '4:3') { } 3 0 3
elsif ($aspectRatio eq '16:9') { } 0 0 0
1170 3 push @filter,$self->cropFilter('ih/3*4','ih');
1171 }
1172 elsif ($aspectRatio eq '16:9') {
1173 0 push @filter,$self->cropFilter('iw','iw/16*9');
1174 }
1175 else {
1176 0 $self->throw;
1177 }
1178 }
1179  
1180 5 $self->addFilter(-vf=>\@filter,',');
1181  
1182 # Output-Optionen
1183
1184 5 $self->addStartStop($start,$stop);
1185 5 $self->addOption('-qscale:v'=>$quality);
1186 5 $self->addOption(-pix_fmt=>$pixelFormat);
1187  
1188 # * Output
1189  
1190 5 $self->prependString('ffmpeg -y');
1191 5 $self->addOutput("$dir/%06d.jpg");
1192  
1193 5 return $self;
1194 }
1195  
1196 # -----------------------------------------------------------------------------
1197  
1198 =head3 extract() - Extrahiere Abschnitt aus Audio- oder Video-Datei
1199  
1200 =head4 Synopsis
1201  
1202 $cmd = $class->extract($input,$output,@opt);
1203  
1204 =head4 Arguments
1205  
1206 =over 4
1207  
1208 =item $input
1209  
1210 Eingabe-Datei.
1211  
1212 =item $output
1213  
1214 Ausgabe-Datei.
1215  
1216 =back
1217  
1218 =head4 Options
1219  
1220 =over 4
1221  
1222 =item -name => $str (Default: undef)
1223  
1224 Füge dem Dateinamen die Bezeichnung $str hinzu.
1225  
1226 =item -play => 0|1|2 (Default: 0)
1227  
1228 Extrahiere den Ausschnitt nicht, sondern zeige ihn an. 1=Exit am Ende,
1229 2=Fenster offen lassen (zu erneuten Positionen).
1230  
1231 =item -start => $s (Default: 0)
1232  
1233 Start-Position in einem Format, das die Klasse Quiq::Duration
1234 akzeptiert.
1235  
1236 =item -stop => $s (Default: undef)
1237  
1238 Ende-Position in einem Format, das die Klasse Quiq::Duration
1239 akzeptiert.
1240  
1241 =back
1242  
1243 =head4 Description
1244  
1245 Extrahiere von Position $start bis Position $stop einen Teil
1246 aus der Audio- oder Video-Datei $input und schreibe ihn auf Datei $output.
1247  
1248 Die Extraktion erfolgt ohne Transcoding, also ohne Qualitätsverlust.
1249  
1250 =cut
1251  
1252 # -----------------------------------------------------------------------------
1253  
1254 0 ▶sub extract {
Subroutine extract: not called CC=9 SCAR=45.0
Pod documented
1255 0 my $class = shift;
1256 # @_: $input,$output,@opt
1257  
1258 # Optionen und Argumente
1259  
1260 0 my $name = undef;
1261 0 my $play = 0;
1262 0 my $start = undef;
1263 0 my $stop = undef;
1264
1265 0 Quiq::Option->extract(\@_,
1266 -name => \$name,
1267 -play => \$play,
1268 -start => \$start,
1269 -stop => \$stop,
1270 );
1271 0 ▶ if (@_ == 0 || @_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 or @_ > 2) 0 0 0
Condition0%0/3
@_ == 0 or @_ > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 0 or @_ > 2
@_ == 0 @_ > 2
Truth table0%0/3
A or B
A= @_ == 0B= @_ > 2
ABresult
1X1
011
000
1272 0 $class->throw('Usage: $ffm->extract($input,$output,@opt)');
1273 }
1274 0 my $input = shift;
1275 0 my $output = shift;
1276  
1277 0 my $self = $class->new;
1278 0 $self->addInput($input);
1279 0 ▶ if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 0 0 0
1280 0 $start = Quiq::Duration->stringToSeconds($start);
1281 }
1282 0 ▶ if (defined $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stop) 0 0 0
1283 0 $stop = Quiq::Duration->stringToSeconds($stop);
1284 }
1285 0 $self->addStartStop($start,$stop);
1286  
1287 0 ▶ if ($play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play) { } 0 0 0
1288 0 ▶ $self->prependString($play == 1? 'ffplay -autoexit': 'ffplay');
Branch: 1 branch
BranchTrueFalseTotal
$play == 1 ? : 0 0 0
1289 0 $self->addOption(-vf=>'scale=720*a:720');
1290 }
1291 else {
1292 0 my $inp = $self->input(0);
1293 0 $self->outName($name);
1294 # Rausgenommen, da Fehler bei Extraktion aus Audio-Datei.
1295 # Bei Video nötig?
1296 # $self->outSize($inp->size);
1297 0 ▶ $self->outStart($start || 0);
Condition0%0/2
$start || 0
l!l
00
MC/DC0%0/1
$start || 0
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
1298 0 ▶ $self->outStop($stop || $inp->duration);
Condition0%0/3
$stop || $inp->duration
l!l&&r!l&&!r
000
MC/DC0%0/2
$stop || $inp->duration
$stop $inp->duration
Truth table0%0/3
A || B
A= $stopB= $inp->duration
ABresult
1X1
011
000
1299 0 $self->prependString('ffmpeg -y');
1300  
1301 0 $self->addOption(-codec=>'copy');
1302 0 $self->addOutput($output);
1303 }
1304  
1305 0 return $self;
1306 }
1307  
1308 # -----------------------------------------------------------------------------
1309  
1310 =head3 extract169To43() - Extrahiere/Croppe 16:9-Video zu 4:3-Video
1311  
1312 =head4 Synopsis
1313  
1314 $cmd = $class->extract169To43($input,$output,@opt);
1315  
1316 =head4 Arguments
1317  
1318 =over 4
1319  
1320 =item $input
1321  
1322 Eingabe-Datei.
1323  
1324 =item $output
1325  
1326 Ausgabe-Datei. Wenn undef, wird der Name der Ausgabedatei generiert.
1327  
1328 =back
1329  
1330 =head4 Options
1331  
1332 =over 4
1333  
1334 =item -bitrate => $x (Default: undef)
1335  
1336 Die Bitrate des generierten Video in kb/s.
1337  
1338 =item -name => $str (Default: undef)
1339  
1340 Füge dem Dateinamen die Bezeichnung $str hinzu.
1341  
1342 =item -play => 0|1|2 (Default: 0)
1343  
1344 Zeige das generierte Video im Player an, statt es in einer Datei
1345 zu speichern. Bei -play=>2 bleibt das Fenster des Players
1346 offen, bis es per Hand geschlossen wird.
1347  
1348 =item -start => $s (Default: 0)
1349  
1350 Start-Position in Sekunden (mit Millisekunden als Nachkommastellen).
1351  
1352 =item -stop => $s (Default: undef)
1353  
1354 Ende-Position in Sekunden (mit Millisekunden als Nachkommastellen).
1355  
1356 =item -xOffset => $n (Default: undef)
1357  
1358 Crop-Offset in x-Richtung. Per Default croppt der crop-Filter mittig.
1359  
1360 =back
1361  
1362 =head4 Description
1363  
1364 Croppe 16:9-Video $input zum 4:3-Video $output. Die Crop-Operation
1365 schneidet links und rechts einen Teil des Video ab.
1366  
1367 =cut
1368  
1369 # -----------------------------------------------------------------------------
1370  
1371 0 ▶sub extract169To43 {
Subroutine extract169To43: not called CC=10 SCAR=47.0
Pod documented
1372 0 my $class = shift;
1373 # @_: $input,$output,@opt
1374  
1375 # Optionen und Argumente
1376  
1377 0 my $bitrate = undef;
1378 0 my $name = undef;
1379 0 my $play = 0;
1380 0 my $start = undef;
1381 0 my $stop = undef;
1382 0 my $xOffset = undef;
1383  
1384 0 Quiq::Option->extract(\@_,
1385 -bitrate => \$bitrate,
1386 -name => \$name,
1387 -play => \$play,
1388 -start => \$start,
1389 -stop => \$stop,
1390 -xOffset => \$xOffset,
1391 );
1392 0 ▶ if (@_ == 0 || @_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 or @_ > 2) 0 0 0
Condition0%0/3
@_ == 0 or @_ > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 0 or @_ > 2
@_ == 0 @_ > 2
Truth table0%0/3
A or B
A= @_ == 0B= @_ > 2
ABresult
1X1
011
000
1393 0 $class->throw('Usage: $ffm->extract169To43($input,$output,@opt)');
1394 }
1395 0 my $input = shift;
1396 0 my $output = shift;
1397  
1398 # Operation ausführen
1399  
1400 # * Command-Objekt instantiieren
1401 0 my $self = $class->new;
1402
1403 # * Input
1404 0 $self->addInput($input);
1405
1406 # * Filter
1407
1408 0 my @filter;
1409  
1410 # ** crop
1411
1412 0 my ($width,$height) = $self->input(0)->size;
1413 0 my $newWidth = $height/3*4;
1414 0 ▶ if (!defined $xOffset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $xOffset) 0 0 0
1415 0 $xOffset = ($width-$newWidth)/2;
1416 }
1417 0 push @filter,$self->cropFilter($newWidth,$height,$xOffset,0);
1418  
1419 # ** scale (im Falle von play)
1420
1421 0 ▶ if ($play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play) 0 0 0
1422 0 push @filter,$self->scaleFilter('720*a',720);
1423 }
1424
1425 0 $self->addFilter('-vf',\@filter,',');
1426  
1427 # * Output-Optionen
1428  
1429 0 $self->addStartStop($start,$stop);
1430 0 ▶ $self->addBitrate($bitrate || $self->input(0)->bitrate);
Condition0%0/3
$bitrate || $self->input(0)->bitrate
l!l&&r!l&&!r
000
MC/DC0%0/2
$bitrate || $self->input(0)->bitrate
$bitrate $self->input(0)->bitrate
Truth table0%0/3
A || B
A= $bitrateB= $self->input(0)->bitrate
ABresult
1X1
011
000
1431  
1432 # * Output
1433
1434 0 ▶ if ($play) {
Branch: 1 branch
BranchTrueFalseTotal
if ($play) { } 0 0 0
1435 0 ▶ $self->prependString($play == 1? 'ffplay -autoexit': 'ffplay');
Branch: 1 branch
BranchTrueFalseTotal
$play == 1 ? : 0 0 0
1436 }
1437 else {
1438 0 $self->outName($name);
1439 0 $self->outSize($newWidth,$height);
1440 0 ▶ $self->outStart($start || 0);
Condition0%0/2
$start || 0
l!l
00
MC/DC0%0/1
$start || 0
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
1441 0 ▶ $self->outStop($stop || $self->input(0)->duration);
Condition0%0/3
$stop || $self->input(0)->duration
l!l&&r!l&&!r
000
MC/DC0%0/2
$stop || $self->input(0)->duration
$stop $self->input(0)->duration
Truth table0%0/3
A || B
A= $stopB= $self->input(0)->duration
ABresult
1X1
011
000
1442  
1443 0 $self->prependString('ffmpeg -y');
1444 0 $self->addOutput($output);
1445 }
1446
1447 0 return $self;
1448 }
1449  
1450 # -----------------------------------------------------------------------------
1451  
1452 =head3 videoInfo() - Schreibe Video-Stream-Information in XML
1453  
1454 =head4 Synopsis
1455  
1456 $cmd = $class->videoInfo($input);
1457 $cmd = $class->videoInfo($input,$streamIndex);
1458  
1459 =head4 Arguments
1460  
1461 =over 4
1462  
1463 =item $input
1464  
1465 Eingabe-Datei.
1466  
1467 =item $streamIndex (Default: 0)
1468  
1469 Index des Video-Stream.
1470  
1471 =back
1472  
1473 =head4 Description
1474  
1475 Erzeuge eine ffprobe-Kommandozeile, die Information über den
1476 Video-Stream $streamIndex in Datei $input liefert. Ist kein
1477 Stream-Index angegeben, wird der erste Stream (Index 0) genommen.
1478  
1479 =cut
1480  
1481 # -----------------------------------------------------------------------------
1482  
1483 1 ▶sub videoInfo {
Subroutine videoInfo: called CC=2 SCAR=6.9
Pod documented
1484 1 my $class = shift;
1485 1 my $input = shift;
1486 1 ▶ my $streamIndex = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1487  
1488 1 my $self = $class->new;
1489
1490 1 $self->addString('ffprobe');
1491 1 $self->addOption(-loglevel=>'error');
1492 1 $self->addOption(-print_format=>'xml');
1493 1 $self->addOption('-show_streams');
1494 1 $self->addOption(-select_streams=>"v:$streamIndex");
1495 1 $self->addInput($input);
1496  
1497 1 return $self;
1498 }
1499  
1500 # -----------------------------------------------------------------------------
1501  
1502 =head2 Kommando-Ausführung
1503  
1504 =head3 execute() - Führe FFmpeg-Kommandozeile aus
1505  
1506 =head4 Synopsis
1507  
1508 $cmd->execute;
1509  
1510 =head4 Description
1511  
1512 Führe FFmpeg-Kommando $cmd aus. Als Ausgabe erscheint lediglich
1513 die Fortschrittsanzeige.
1514  
1515 =cut
1516  
1517 # -----------------------------------------------------------------------------
1518  
1519 0 ▶sub execute {
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
1520 0 my $self = shift;
1521  
1522 0 my $cmd = $self->command;
1523 0 my $fh = Quiq::FileHandle->open('-|',"($cmd 2>&1)");
1524 0 local $/ = "\r";
1525 0 $| = 1;
1526 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
1527 0 ▶ if (/^(ffmpeg|ffplay|ffprobe|Input|Output)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(ffmpeg|ffplay|ffprobe|Input|Output)/) 0 0 0
1528 0 next;
1529 }
1530 0 s/\n+$/\n/;
1531 0 print;
1532 }
1533 0 $fh->close;
1534  
1535 0 return;
1536 }
1537  
1538 # -----------------------------------------------------------------------------
1539  
1540 =head1 VERSION
1541  
1542 1.240
1543  
1544 =head1 AUTHOR
1545  
1546 Frank Seitz, L<http://fseitz.de/>
1547  
1548 =head1 COPYRIGHT
1549  
1550 Copyright (C) 2026 Frank Seitz
1551  
1552 =head1 LICENSE
1553  
1554 This code is free software; you can redistribute it and/or modify
1555 it under the same terms as Perl itself.
1556  
1557 =cut
1558  
1559 # -----------------------------------------------------------------------------
1560  
1561 3 1;
1562  
1563 # eof
« Quiq/Udl.pm ↑ Summary Quiq/Object.pm »