Quiq/TimeLapse/Sequence.pm

filter: Statementstmt 25.1% 40 / 159 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 56.5% 13 / 23 Podpod 100% 10 / 10 totaltotal 27.8% 63 / 226 CC 18 SCAR 34.6
CC
18
Cov
21%
generate
110.0
morph
20.0
sha1
12.0
CRAP 31.8
« Quiq/PostgreSql/PgDump.pm ↑ Summary Quiq/LineProcessor.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::TimeLapse::Sequence - Bildsequenz
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 # Klasse laden
16 use %CLASS;
17
18 # Instantiiere Sequence-Objekt
19 $tsq = Quiq::TimeLapse::Sequence->new(\@images);
20
21 # Exportiere Bilddateien in Verzeichnis
22 $tsq->export($dir);
23  
24 =cut
25  
26 # -----------------------------------------------------------------------------
27  
28 package Quiq::TimeLapse::Sequence;
29 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 our $VERSION = '1.240';
36  
37 1 ▶use Quiq::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Quiq::Progress;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Quiq::ImageMagick;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Quiq::FFmpeg;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Image::Size ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # -----------------------------------------------------------------------------
48  
49 =head1 METHODS
50  
51 =head2 Konstruktor
52  
53 =head3 new() - Instantiiere Bildsequenz-Objekt
54  
55 =head4 Synopsis
56  
57 $tsq = $class->new(\@images);
58  
59 =head4 Arguments
60  
61 =over 4
62  
63 =item @images
64  
65 Array von Bilddatei-Objekten
66  
67 =back
68  
69 =head4 Returns
70  
71 Referenz auf das Bildsequenz-Objekt
72  
73 =head4 Description
74  
75 Instantiiere Bildsequenz-Objekt aus den Bilddateien \@images und
76 liefere eine Referenz auf dieses Objekt zurück.
77  
78 =cut
79  
80 # -----------------------------------------------------------------------------
81  
82 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
83 0 my ($class,$imageA) = @_;
84  
85 0 return $class->SUPER::new(
86 imageA => $imageA,
87 );
88 }
89  
90 # -----------------------------------------------------------------------------
91  
92 =head2 Objektmethoden
93  
94 =head3 count() - Anzahl der Bilder
95  
96 =head4 Synopsis
97  
98 $n = $tsq->count;
99  
100 =head4 Returns
101  
102 Integer >= 0
103  
104 =head4 Description
105  
106 Liefere die Anzahl der in der Sequenz enthaltenen Bilddateien.
107  
108 =cut
109  
110 # -----------------------------------------------------------------------------
111  
112 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 return scalar @{$self->{'imageA'}};
115 }
116  
117 # -----------------------------------------------------------------------------
118  
119 =head3 duration() - Dauer der Bildsequenz
120  
121 =head4 Synopsis
122  
123 $duration = $tsq->duration($framerate);
124  
125 =head4 Returns
126  
127 String
128  
129 =head4 Description
130  
131 Berechne die Dauer der Bildsequenz, wenn sie mit Framerate $framerate
132 gerendert wird, und liefere das Ergebnis als Zeitdauer in
133 dem Format C<HhMmS.XXXs> (drei Nachkommastellen).
134  
135 =cut
136  
137 # -----------------------------------------------------------------------------
138  
139 0 ▶sub duration {
Subroutine duration: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($self,$framerate) = @_;
141 0 return Quiq::Duration->secondsToString($self->count/$framerate,3);
142 }
143  
144 # -----------------------------------------------------------------------------
145  
146 =head3 export() - Exportiere Bildsequenz
147  
148 =head4 Synopsis
149  
150 $tsq->export($destDir);
151  
152 =head4 Arguments
153  
154 =over 4
155  
156 =item $destDir
157  
158 Pfad des Zielverzeichnisses.
159  
160 =back
161  
162 =head4 Returns
163  
164 nichts
165  
166 =head4 Description
167  
168 Exportiere die Bildsequenz nach Verzeichnis $destDir. Existiert
169 $destDir nicht, wird es erzeugt. Existiert das Verzeichnis, wird
170 die Bildsequenz angehängt.
171  
172 =cut
173  
174 # -----------------------------------------------------------------------------
175  
176 0 ▶sub export {
Subroutine export: not called CC=2 SCAR=17.9
Pod documented
177 0 my ($self,$dir) = @_;
178  
179 0 Quiq::Path->mkdir($dir,-recursive=>1);
180
181 0 my $max = Quiq::Path->maxFileNumber($dir);
182  
183 0 my $pro = Quiq::Progress->new($self->count,
184 -show => 1,
185 );
186 0 my $i = 0;
187 0 for my $img ($self->images) {
188 0 $i++;
189 0 my $destFile = sprintf '%s/%06d.%s',$dir,$max+$i,$img->type;
190 0 print $pro->msg($i,'%s: i/n x% t/t(t) x/s: %s','link',$destFile);
191 0 Quiq::Path->symlinkRelative($img->path,$destFile);
192 }
193 0 print $pro->msg;
194  
195 0 return;
196 }
197  
198 # -----------------------------------------------------------------------------
199  
200 =head3 morph() - Exportiere Bildsequenz mit Zwischenbildern
201  
202 =head4 Synopsis
203  
204 $tsq->morph($n,$destDir);
205  
206 =head4 Arguments
207  
208 =over 4
209  
210 =item $n
211  
212 Anzahl der Zwischenbilder.
213  
214 =item $destDir
215  
216 Pfad des Zielverzeichnisses.
217  
218 =back
219  
220 =head4 Returns
221  
222 nichts
223  
224 =head4 Description
225  
226 Exportiere die Bildsequenz nach Verzeichnis $destDir mit jeweils
227 $n gemorphten Zwischenbildern. Existiert $destDir nicht, wird es
228 erzeugt. Existiert das Verzeichnis, wird die Bildsequenz
229 angehängt.
230  
231 =cut
232  
233 # -----------------------------------------------------------------------------
234  
235 0 ▶sub morph {
Subroutine morph: not called CC=4 SCAR=30.0
Pod documented
236 0 my ($self,$n,$dir) = @_;
237  
238 0 Quiq::Path->mkdir($dir,-recursive=>1);
239
240 0 my $max = Quiq::Path->maxFileNumber($dir);
241  
242 0 $| = 1;
243 0 my $pro = Quiq::Progress->new(($self->count-1)*($n+1)+1,
244 -show => 1,
245 );
246  
247 0 my $j = 0;
248 0 my $imageA = $self->images;
249 0 ▶ for (my $i = 0; $i < @$imageA-1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$imageA - 1) 0 0 0
250 0 my $img1 = $imageA->[$i];
251 0 my $img2 = $imageA->[$i+1];
252 0 my $ext = $img1->type;
253  
254 0 my $tmpDir = File::Temp::tempdir(CLEANUP=>1);
255
256 0 my $cmd = Quiq::ImageMagick->morph($img1,$img2,
257 "$tmpDir/%02d.$ext",$n);
258 0 $cmd->execute;
259  
260 0 my @files = Quiq::Path->glob("$tmpDir/*");
261 0 ▶ if ($i < @$imageA-2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$imageA - 2) 0 0 0
262 0 pop @files;
263 }
264 0 for my $srcFile (@files) {
265 0 $j++;
266 0 my $destFile = sprintf '%s/%06d.%s',$dir,$max+$j,$ext;
267 0 print $pro->msg($j,'i/n x% t/t(t) x/s: %s',$destFile);
268 0 Quiq::Path->copy($srcFile,$destFile);
269 }
270 }
271 0 print $pro->msg;
272  
273 0 return;
274 }
275  
276 # -----------------------------------------------------------------------------
277  
278 =head3 generate() - Erzeuge Video
279  
280 =head4 Synopsis
281  
282 $tsq->generate($file,@opt);
283  
284 =head4 Arguments
285  
286 =over 4
287  
288 =item $file
289  
290 Die zu erzeugende Video-Datei, z.B. '2018-07-28-anreise.mp4'.
291  
292 =back
293  
294 =head4 Options
295  
296 =over 4
297  
298 =item -dryRun => $bool (Default: 0)
299  
300 Zeige Änderungen, führe sie aber nicht aus.
301  
302 =item -endFrames => $sec (Default: 1)
303  
304 Dauer der am Ende des Clip hinzugefügten "Ende-Frames" in
305 Sekunden. Ist der Wert negativ, wird der Clip auf diese Dauer
306 verlängert (ist der Clip länger, wird der Default genommen). Die
307 End-Frames verlängern den Clip bis zur vollen Sekunde plus $sec-1
308 Sekunden. D.h. der Wert 1 füllt bis zur nächsten vollen Sekunde
309 auf. Bei 0 werden keine End-Frames hinzugefügt (was nicht ratsam
310 ist, da ffmpeg dann am Clip-Ende seltsame Ergebnisse produziert).
311  
312 =item -framerate => $n (Default: 8)
313  
314 Anzahl Bilder pro Sekunde.
315  
316 =item -preset => $preset (Default: 'ultrafast')
317  
318 Satz an vorgewählten Optionen, für Encoding-Zeit
319 vs. Kompressionsrate. Schnellstes Encoding: 'ultrafast', beste
320 Kompression: 'veryslow'. Siehe Quiq::FFmpeg, imagesToVideo().
321  
322 =item -size => "$width:$height" (Default: undef)
323  
324 Geometrie des erzeugten Videos.
325  
326 =item -videoBitrate => $bitrate (Default: 60_000)
327  
328 Video-Bitrate in kbit/s.
329  
330 =item -videoFramerate => $n (Default: 24)
331  
332 Framerate des Video.
333  
334 =back
335  
336 =head4 Returns
337  
338 nichts
339  
340 =head4 Description
341  
342 Erzeuge aus der Bildsequenz das Video $file.
343  
344 =cut
345  
346 # -----------------------------------------------------------------------------
347  
348 0 ▶sub generate {
Subroutine generate: not called CC=10 SCAR=47.0
Pod documented
349 0 my $self = shift;
350 0 my $file = shift;
351  
352 # Optionen
353  
354 0 my $dryRun = 0;
355 0 my $endFrames = 1;
356 0 my $framerate = 8;
357 0 my $preset = 'ultrafast',
358 my $size = undef;
359 0 my $videoBitrate = 200_000;
360 0 my $videoFramerate = 24;
361  
362 0 Quiq::Option->extract(\@_,
363 -dryRun => \$dryRun,
364 -endFrames => \$endFrames,
365 -framerate => \$framerate,
366 -preset => \$preset,
367 -size => \$size,
368 -videoBitrate => \$videoBitrate,
369 -videoFramerate => \$videoFramerate,
370 );
371  
372 # Prüfe Änderungen an der Sequenz mittels SHA1 Hash. Wenn keine
373 # Änderungen vorhanden sind, erzeugen wir den Clip nicht neu.
374
375 0 my $sha1 = $self->sha1($endFrames,$framerate,$preset,$size,$videoBitrate,
376 $videoFramerate);
377 0 (my $sha1File = $file) .= '.sha1';
378 0 ▶ if (-e $sha1File && -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $sha1File and -e $file) 0 0 0
Condition0%0/3
-e $sha1File and -e $file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $sha1File and -e $file
-e $sha1File -e $file
Truth table0%0/3
A and B
A= -e $sha1FileB= -e $file
ABresult
0X0
100
111
379 0 ▶ if ($sha1 eq Quiq::Path->read($sha1File)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha1 eq "Quiq::Path"->read($sha1File)) 0 0 0
380 0 return;
381 }
382 }
383  
384 0 printf "Generating clip: %s\n",$file;
385 0 printf "Clip duration: %s\n",$self->duration($framerate);
386  
387 0 ▶ if ($dryRun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dryRun) 0 0 0
388 0 return;
389 }
390  
391 # Vor Neuerzeugung SHA1-Datei löschen
392 0 Quiq::Path->delete($sha1File);
393  
394 # Bildsequenz exportieren
395
396 0 my $dir = "/tmp/timelapse$$";
397 0 Quiq::Path->delete($dir);
398 0 $self->export($dir);
399
400 0 ▶ if ((my $count = $self->count) && $endFrames) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $count = $self->count and $endFrames) 0 0 0
Condition0%0/3
my $count = $self->count and $endFrames
!ll&&!rl&&r
000
MC/DC0%0/2
my $count = $self->count and $endFrames
my $count = $self->count $endFrames
Truth table0%0/3
A and B
A= my $count = $self->countB= $endFrames
ABresult
0X0
100
111
401 # End-Frames generieren
402  
403 0 my $maxFile = Quiq::Path->maxFilename($dir);
404 0 (my $fmt = $maxFile) =~ s/^(\d+)/%06d/;
405 0 my $maxNumber = $1;
406  
407 0 my $srcFile = "$dir/$maxFile";
408 0 my $destFile = sprintf "%s/$fmt",$dir,$maxNumber+1;
409  
410 0 my ($width,$height) = Image::Size::imgsize($srcFile);
411  
412 0 my $cmd = Quiq::ImageMagick->new;
413 0 $cmd->addCommand('convert');
414 0 $cmd->addElement($srcFile);
415 0 $cmd->addOption(-stroke=>'#ff0000');
416 0 $cmd->addOption(-strokewidth=>16);
417 0 $cmd->addOption(-draw=>sprintf('line 0,0 %s,%s',$width-1,$height-1));
418 0 $cmd->addOption(-draw=>sprintf('line 0,%s %s,0',$height-1,$width-1));
419 0 $cmd->addElement($destFile);
420 0 $cmd->execute;
421  
422 # Mehr als eine Sekunde ist ratsam, damit unter kdenlive
423 # nachträglich Frames ergänzt werden können, ohne den Clip zu
424 # entfernen und wieder hinzufügen zu müssen
425  
426 0 my $duration = $count/$framerate;
427
428 0 ▶ if ($endFrames < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endFrames < 0) 0 0 0
429 0 my $length = abs $endFrames;
430 0 ▶ $endFrames = $length > $duration? $length-int($duration): 1;
Branch: 1 branch
BranchTrueFalseTotal
$length > $duration ? : 0 0 0
431 }
432 0 my $n = (int($duration)+$endFrames)*$framerate-$count;
433  
434 0 printf "End frames: %d\n",$n;
435 0 ▶ for (my $i = 2; $i <= $n+1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n + 1) 0 0 0
436 0 my $link = sprintf $fmt,$maxNumber+$i;
437 0 Quiq::Path->symlink($destFile,"$dir/$link");
438 }
439 }
440  
441 # Video generieren
442  
443 0 my $cmd = Quiq::FFmpeg->imagesToVideo("$dir/*.jpg",$file,
444 -framerate => $framerate,
445 -preset => $preset,
446 -size => $size,
447 -videoBitrate => $videoBitrate,
448 -videoFramerate => $videoFramerate,
449 );
450 0 printf "%s\n",$cmd->command;
451 0 $cmd->execute;
452  
453 # SHA1-Datei schreiben
454 0 Quiq::Path->write($sha1File,$sha1);
455
456 # Exportverzeichnis löschen
457 0 Quiq::Path->delete($dir);
458  
459 0 return;
460 }
461  
462 # -----------------------------------------------------------------------------
463  
464 =head3 images() - Liste der Bilddatei-Objekte
465  
466 =head4 Synopsis
467  
468 @images|$imageA = $tsq->images;
469  
470 =head4 Returns
471  
472 Liste Bilddatei-Objekte. Im Skalarkontext eine Referenz auf die Liste.
473  
474 =head4 Description
475  
476 Liefere die Liste der Bilddatei-Objekte der Sequenz.
477  
478 =cut
479  
480 # -----------------------------------------------------------------------------
481  
482 0 ▶sub images {
Subroutine images: not called CC=2 SCAR=17.9
Pod documented
483 0 my $self = shift;
484 0 my $imageA = $self->{'imageA'};
485 0 ▶ return wantarray? @$imageA: $imageA;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
486 }
487  
488 # -----------------------------------------------------------------------------
489  
490 =head3 sha1() - Hash Digest der Sequenz
491  
492 =head4 Synopsis
493  
494 $sha1 = $tsq->sha1(@keyVal);
495  
496 =head4 Returns
497  
498 SHA1 Digest
499  
500 =head4 Description
501  
502 Berechne den SHA1 Hash-Wert für die Sequenz und liefere diesen zurück.
503 Der Hash-Wert wird gebildet über allen Bilddateien, derem Pfad,
504 deren Größe und Änderungszeitpunkt.
505  
506 Anhand des SHA1 Hash-Werts läßt sich prüfen, ob eine Änderung an
507 der Bildsequenz stattgefunden hat und eine teure Operation wie das
508 (erneute) Rendern eines Video notwendig ist.
509  
510 =cut
511  
512 # -----------------------------------------------------------------------------
513  
514 0 ▶sub sha1 {
Subroutine sha1: not called CC=3 SCAR=24.8
Pod documented
515 0 my $self = shift;
516 # @_: @keyVal
517  
518 0 my $text;
519 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
520 0 $text .= join(',',@_)."\n";
521 }
522 0 for my $img (@{$self->{'imageA'}}) {
523 0 my $path = $img->path;
524 0 my ($size,$mtime) = (stat $path)[7,9];
525 0 $text .= sprintf "%s %s %s\n",$path,$mtime,$size;
526 };
527
528 0 return Digest::SHA::sha1_hex($text);
529 }
530  
531 # -----------------------------------------------------------------------------
532  
533 =head2 Manipulation
534  
535 =head3 pick() - Reduziere auf jede n-te Bilddatei
536  
537 =head4 Synopsis
538  
539 $tsq = $tsq->pick($n);
540  
541 =head4 Returns
542  
543 Bildsquenz-Objekt (für Chaining)
544  
545 =head4 Description
546  
547 Reduziere die Folge der Bilddatei-Objekte auf jedes n-te Element,
548 d.h. entferne alle anderen
549  
550 =cut
551  
552 # -----------------------------------------------------------------------------
553  
554 0 ▶sub pick {
Subroutine pick: not called CC=2 SCAR=17.9
Pod documented
555 0 my ($self,$n) = @_;
556  
557 0 my $imageA = $self->{'imageA'};
558
559 0 ▶ for (my $i = 0; $i < @$imageA; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$imageA) 0 0 0
560 0 splice @$imageA,$i,$n-1;
561 }
562  
563 0 return $self;
564 }
565  
566 # -----------------------------------------------------------------------------
567  
568 =head3 reverse() - Kehre Folge der Bilddatei-Objekt um
569  
570 =head4 Synopsis
571  
572 $tsq = $tsq->reverse;
573  
574 =head4 Returns
575  
576 Bildsquenz-Objekt (für Chaining)
577  
578 =head4 Description
579  
580 Kehre die Folge der Bilddatei-Objekte um, so dass das erste Bild zum
581 letzten wird, das zweite zum vorletzten usw.
582  
583 =cut
584  
585 # -----------------------------------------------------------------------------
586  
587 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
588 0 my $self = shift;
589  
590 0 @{$self->{'imageA'}} = reverse @{$self->{'imageA'}};
591  
592 0 return $self;
593 }
594  
595 # -----------------------------------------------------------------------------
596  
597 =head1 VERSION
598  
599 1.240
600  
601 =head1 AUTHOR
602  
603 Frank Seitz, L<http://fseitz.de/>
604  
605 =head1 COPYRIGHT
606  
607 Copyright (C) 2026 Frank Seitz
608  
609 =head1 LICENSE
610  
611 This code is free software; you can redistribute it and/or modify
612 it under the same terms as Perl itself.
613  
614 =cut
615  
616 # -----------------------------------------------------------------------------
617  
618 1 1;
619  
620 # eof
« Quiq/PostgreSql/PgDump.pm ↑ Summary Quiq/LineProcessor.pm »