blib/lib/App/FfmpegUtils.pm

filter: Statementstmt 12.1% 43 / 355 Branchbran 0.0% 0 / 186 Conditioncond 0.0% 0 / 122 MC/DCmcdc 0.0% 0 / 64 Subroutinesub 41.1% 7 / 17 Podpod 100% 5 / 5 totaltotal 7.3% 55 / 749 CC 142 SCAR 72.5
CC
142
Cov
6%
cut_duration_from_video
2352.0
cut_video_by_duration
1892.0
reencode_video_with_libx264
702.0
CRAP 1409.0
↑ Summary
1 package App::FfmpegUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Perinci::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Perinci::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 1 our $DATE = '2026-08-26'; # DATE
14 1 our $DIST = 'App-FfmpegUtils'; # DIST
15 1 our $VERSION = '0.016'; # VERSION
16  
17 1 our %SPEC;
18  
19 1 $SPEC{':package'} = {
20 v => 1.1,
21 summary => 'Utilities related to ffmpeg',
22 };
23  
24 1 our %argspec0_file = (
25 file => {
26 schema => 'filename*',
27 req => 1,
28 pos => 0,
29 },
30 );
31  
32 1 our %argspec0_files = (
33 files => {
34 'x.name.is_plural' => 1,
35 'x.name.singular' => 'file',
36 schema => ['array*' => of => 'filename*'],
37 req => 1,
38 pos => 0,
39 slurpy => 1,
40 },
41 );
42  
43 1 our %argspecopt_ffmpeg_path = (
44 ffmpeg_path => {
45 schema => 'filename*',
46 },
47 );
48  
49 1 our %argspecopt_copy = (
50 copy => {
51 summary => 'Whether to use the "copy" codec (fast but produces inaccurate timings)',
52 schema => 'bool*',
53 },
54 );
55  
56 1 our %argspecopt_copy_default1 = (
57 copy => {
58 summary => 'Whether to use the "copy" codec (fast but produces inaccurate timings)',
59 schema => 'bool*',
60 default => 1,
61 },
62 );
63  
64 1 our %argspecsopt_duration = (
65 start => {
66 schema => ['any*', of=>['duration*', 'percent_str*']],
67 default => 0,
68 cmdline_aliases => {s=>{}},
69 },
70 end => {
71 schema => ['any*', of=>['duration*', 'percent_str*']],
72 cmdline_aliases => {e=>{}},
73 },
74 duration => {
75 schema => ['any*', of=>['duration*', 'percent_str*']],
76 cmdline_aliases => {d=>{}},
77 },
78 );
79  
80 1 our %argspecopt_overwrite = (
81 overwrite => {
82 schema => 'bool*',
83 cmdline_aliases => {O=>{}},
84 },
85 );
86  
87 1 my @presets = qw/ultrafast superfast veryfast faster fast medium slow slower veryslow/;
88  
89 0 ▶sub _nearest {
Subroutine _nearest: not called CC=1 SCAR=6.9
90 0 sprintf("%d", $_[0]/$_[1]) * $_[1];
91 }
92  
93 0 ▶sub _convert_pct {
Subroutine _convert_pct: not called CC=2 SCAR=17.9
94 0 my ($num, $total) = @_;
95 0 ▶ if ($num =~ s/%\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ s/%\z//) { } 0 0 0
96 0 $num/100 * $total;
97 } else {
98 0 $num;
99 }
100 }
101  
102 $SPEC{reencode_video_with_libx264} = {
103 v => 1.1,
104 summary => 'Re-encode video (using ffmpeg and libx264)',
105 description => <<'MARKDOWN',
106  
107 This utility runs *ffmpeg* to re-encode your video files using the libx264
108 codec. It is a wrapper to simplify invocation of ffmpeg. It selects the
109 appropriate ffmpeg options for you, allows you to specify multiple files, and
110 picks appropriate output filenames. It also sports a `--dry-run` option to let
111 you see ffmpeg options to be used without actually running ffmpeg.
112  
113 This utility is usually used to reduce the file size (and optionally video
114 width/height) of videos so they are smaller, while minimizing quality loss.
115 Smartphone-produced videos are often high bitrate (e.g. >10-20Mbit) and not yet
116 well compressed, so they make a good input for this utility. The default setting
117 is roughly similar to how Google Photos encodes videos (max 1080p).
118  
119 The default settings are:
120  
121 -v:c libx264
122 -preset veryslow (to get the best compression rate, but with the slowest encoding time)
123 -crf 28 (0-51, subjectively sane is 18-28, 18 ~ visually lossless, 28 ~ visually acceptable)
124  
125 when a downsizing is requested (using the `--downsize-to` option), this utility
126 first checks each input video if it is indeed larger than the requested final
127 size. If it is, then the `-vf scale` option is added. This utility also
128 calculates a valid size for ffmpeg, since using `-vf scale=-1:720` sometimes
129 results in failure due to odd number.
130  
131 Audio streams are copied, not re-encoded.
132  
133 Output filenames are:
134  
135 ORIGINAL_NAME.crf28.mp4
136  
137 or (if downsizing is done):
138  
139 ORIGINAL_NAME.480p-crf28.mp4
140  
141 MARKDOWN
142 args => {
143 %argspec0_files,
144 %argspecopt_ffmpeg_path,
145 crf => {
146 schema => ['int*', between=>[0,51]],
147 },
148 scale => {
149 schema => 'str*',
150 default => '1080^>',
151 description => <<'MARKDOWN',
152  
153 Scale video to specified size. See <pm:Math::Image::CalcResized> or
154 <prog:calc-image-resized-size> for more details on scale specification. Some
155 examples include:
156  
157 The default is `1080^>` which means to shrink to 1080p if video size is larger
158 than 1080p.
159  
160 To disable scaling, set `--scale` to '' (empty string), or specify
161 `--dont-scale` on the CLI.
162  
163 MARKDOWN
164 cmdline_aliases => {
165 0 ▶ dont_scale => {summary=>"Alias for --scale ''", is_flag=>1, code=>sub {$_[0]{scale} = ''}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 ▶ no_scale => {summary=>"Alias for --scale ''", is_flag=>1, code=>sub {$_[0]{scale} = ''}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 },
168 },
169 preset => {
170 schema => ['str*', in=>\@presets],
171 default => 'veryslow',
172 cmdline_aliases => {
173 0 ▶ (map {($_ => {is_flag=>1, summary=>"Shortcut for --preset=$_", code=>do { my $p = $_; sub { $_[0]{preset} = $p }}})} @presets),
Subroutine __ANON__: not called CC=1 SCAR=0.1
174 },
175 },
176 frame_rate => {
177 summary => 'Set frame rate, in fps',
178 schema => 'ufloat*',
179 cmdline_aliases => {r=>{}},
180 },
181 audio_sample_rate => {
182 summary => 'Set audio sample rate, in Hz',
183 schema => 'uint*',
184 cmdline_aliases => {sample_rate=>{}},
185 },
186 %argspecopt_overwrite,
187 },
188 features => {
189 dry_run => 1,
190 },
191 examples => [
192 {
193 summary => 'The default setting is to shrink to 1080p if video is larger than 1080p',
194 src => '[[prog]] *',
195 src_plang => 'bash',
196 test => 0,
197 'x.doc.show_result' => 0,
198 },
199 {
200 summary => 'Do not scale/shrink',
201 src => '[[prog]] --dont-scale *',
202 src_plang => 'bash',
203 test => 0,
204 'x.doc.show_result' => 0,
205 },
206 {
207 summary => 'Shrink to 480p if video is larger than 480p, but make the reencoding "visually lossless"',
208 src => "[[prog]] --scale '480^>' --crf 18 *",
209 src_plang => 'bash',
210 test => 0,
211 'x.doc.show_result' => 0,
212 },
213 ],
214 };
215 0 ▶sub reencode_video_with_libx264 {
Subroutine reencode_video_with_libx264: not called CC=26 SCAR=65.5
Pod documented
216 0 require File::Which;
217 0 require IPC::System::Options;
218 0 require Media::Info;
219  
220 0 my %args = @_;
221  
222 0 ▶ my $ffmpeg_path = $args{ffmpeg_path} // File::Which::which("ffmpeg");
Condition0%0/3
$args{"ffmpeg_path"} // File::Which::which("ffmpeg")
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ffmpeg_path"} // File::Which::which("ffmpeg")
$args{"ffmpeg_path"} File::Which::which("ffmpeg")
Truth table0%0/3
A // B
A= $args{"ffmpeg_path"}B= File::Which::which("ffmpeg")
ABresult
1X1
011
000
223 0 my $scale = $args{scale};
224  
225 0 ▶ unless ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"-dry_run"}) 0 0 0
226 0 ▶ return [400, "Cannot find ffmpeg in path"] unless defined $ffmpeg_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ffmpeg_path 0 0 0
227 0 ▶ return [400, "ffmpeg path $ffmpeg_path is not executable"] unless -f $ffmpeg_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $ffmpeg_path 0 0 0
228 }
229  
230 0 for my $file (@{$args{files}}) {
231 0 log_info "Processing file %s ...", $file;
232  
233 0 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
234 0 log_error "No such file %s, skipped", $file;
235 0 next;
236 }
237  
238 0 my $res = Media::Info::get_media_info(media => $file);
239 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
240 0 log_error "Can't get media information fod %s: %s - %s, skipped",
241 $file, $res->[0], $res->[1];
242 0 next;
243 }
244 0 my $video_info = $res->[2];
245  
246 0 ▶ my $crf = $args{crf} // 28;
Condition0%0/2
$args{"crf"} // 28
l!l
00
MC/DC0%0/1
$args{"crf"} // 28
$args{"crf"}
Truth table0%0/2
A
A= $args{"crf"}
Aresult
11
00
247 my @ffmpeg_args = (
248 "-i", $file,
249 0 ▶ ($args{overwrite} ? "-y":"-n"),
Branch: 1 branch
BranchTrueFalseTotal
$args{'overwrite'} ? : 0 0 0
250 );
251  
252 0 my $scale_suffix;
253 SCALE: {
254 0 ▶ last unless defined $scale && length $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale and length $scale 0 0 0
Condition0%0/3
defined $scale and length $scale
!ll&&!rl&&r
000
MC/DC0%0/2
defined $scale and length $scale
defined $scale length $scale
Truth table0%0/3
A and B
A= defined $scaleB= length $scale
ABresult
0X0
100
111
255 0 require Math::Image::CalcResized;
256 0 my $calcres = Math::Image::CalcResized::calc_image_resized_size(
257 size => "$video_info->{video_width}x$video_info->{video_height}",
258 resize => $scale,
259 );
260 0 ▶ return [400, "Can't scale using '$scale': $calcres->[0] - $calcres->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $calcres->[0] == 200 0 0 0
261 unless $calcres->[0] == 200;
262  
263 0 ▶ my ($scaled_width, $scaled_height) = $calcres->[2] =~ /(.+)x(.+)/
Branch: 1 branch
BranchTrueFalseTotal
() = $calcres->[2] =~ /(.+)x(.+)/ or return [500, "calc_image_resized_size() doesn't return new WxH ($calcres->[2])"] 0 0 0
264 or return [500, "calc_image_resized_size() doesn't return new WxH ($calcres->[2])"];
265 last unless $scaled_width != $video_info->{video_width} ||
266 0 ▶ $scaled_height != $video_info->{video_height};
Branch: 1 branch
BranchTrueFalseTotal
unless $scaled_width != $video_info->{"video_width"} or $scaled_height != $video_info->{"video_height"} 0 0 0
Condition0%0/3
$scaled_width != $video_info->{"video_width"} or $scaled_height != $video_info->{"video_height"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$scaled_width != $video_info->{"video_width"} or $scaled_height != $video_info->{"video_height"}
$scaled_width != $video_info->{"video_width"} $scaled_height != $video_info->{"video_height"}
Truth table0%0/3
A or B
A= $scaled_width != $video_info->{"video_width"}B= $scaled_height != $video_info->{"video_height"}
ABresult
1X1
011
000
267 0 ($scale_suffix = $calcres->[3]{'func.human_specific'}) =~ s/\W+/_/g;
268 0 push @ffmpeg_args, "-vf", sprintf(
269 "scale=%d:%d",
270 _nearest($scaled_width, 2), # make sure divisible by 2 (optimum is divisible by 16, then 8, then 4)
271 _nearest($scaled_height, 2),
272 );
273 } # SCALE
274  
275 0 my $output_file = $file;
276 0 ▶ my $ext = $scale_suffix ? ".$scale_suffix-crf$crf.mp4" : ".crf$crf.mp4";
Branch: 1 branch
BranchTrueFalseTotal
$scale_suffix ? : 0 0 0
277 0 ▶ $output_file =~ s/(\.\w{3,4})?\z/($1 eq ".mp4" ? "" : $1) . $ext/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '.mp4' ? : 0 0 0
278  
279 0 my $audio_is_copy = 1;
280 0 ▶ $audio_is_copy = 0 if defined $args{audio_sample_rate};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"audio_sample_rate"} 0 0 0
281  
282 push @ffmpeg_args, (
283 "-c:v", "libx264",
284 "-crf", $crf,
285 "-preset", ($args{preset} // 'veryslow'),
286 (defined $args{frame_rate} ? ("-r", $args{frame_rate}) : ()),
287 "-c:a", ($audio_is_copy ? "copy" : "aac"),
288 0 ▶ (defined $args{audio_sample_rate} ? ("-ar", $args{audio_sample_rate}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $args{'frame_rate'} ? : 0 0 0
$audio_is_copy ? : 0 0 0
defined $args{'audio_sample_rate'} ? : 0 0 0
Condition0%0/2
$args{"preset"} // "veryslow"
l!l
00
MC/DC0%0/1
$args{"preset"} // "veryslow"
$args{"preset"}
Truth table0%0/2
A
A= $args{"preset"}
Aresult
11
00
289 $output_file,
290 );
291  
292 0 ▶ if ($args{-dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"-dry_run"}) 0 0 0
293 0 log_info "[DRY-RUN] Running $ffmpeg_path with args %s ...", \@ffmpeg_args;
294 0 next;
295 }
296  
297 IPC::System::Options::system(
298 0 {log=>1},
299 $ffmpeg_path, @ffmpeg_args,
300 );
301 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
302 0 ▶ my ($exit_code, $signal, $core_dump) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
303 0 log_error "ffmpeg for $file failed: exit_code=$exit_code, signal=$signal, core_dump=$core_dump";
304 }
305 }
306  
307 0 [200];
308 }
309  
310 $SPEC{split_video_by_duration} = {
311 1 v => 1.1,
312 summary => 'Split video by duration into parts',
313 description => <<'MARKDOWN',
314  
315 This utility uses **ffmpeg** (particularly the `-t` and `-ss`) option to split a
316 longer video into shorter videos. For example, if you have `long.mp4` with
317 duration of 1h12m and you run it through this utility with `--every 15min` then
318 you will have 5 new video files: `long.1of5.mp4` (15min), `long.2of5.mp4`
319 (15min), `long.3of5.mp4` (15min), `long.4of5.mp4` (15min), and `long.5of5.mp4`
320 (12min).
321  
322 Compared to using `ffmpeg` directly, this wrapper offers convenience of
323 calculating the times (`-ss`) option for you, handling multiple files,
324 automatically choosing output filename, and tab completion.
325  
326 MARKDOWN
327 args => {
328 %argspec0_files,
329 # XXX start => {},
330 every => {
331 schema => ['any*', of=>['duration*', 'percent_str*']],
332 },
333 parts => {
334 schema => 'posint*',
335 },
336 %argspecopt_copy,
337 # XXX merge_if_last_part_is_shorter_than => {},
338 # XXX output_filename_pattern
339 overwrite => {
340 schema => 'bool*',
341 cmdline_aliases => {O=>{}},
342 },
343 },
344 args_rels => {
345 req_one => [qw/every parts/],
346 },
347 examples => [
348 {
349 summary => 'Split video per 15 minutes',
350 src_plang => 'bash',
351 src => '[[prog]] --every 15min foo.mp4',
352 test => 0,
353 'x.doc.show_result' => 0,
354 },
355 {
356 summary => 'Split video per 30s for WhatsApp status',
357 src_plang => 'bash',
358 src => '[[prog]] foo.mp4 30s',
359 test => 0,
360 'x.doc.show_result' => 0,
361 },
362 {
363 summary => 'Split video into 4 equiduration parts',
364 src_plang => 'bash',
365 src => '[[prog]] foo.mp4 --parts 4',
366 test => 0,
367 'x.doc.show_result' => 0,
368 },
369 {
370 summary => 'Another way to split into equiduration parts using percentage in --every',
371 src_plang => 'bash',
372 src => '[[prog]] foo.mp4 --every 25%',
373 test => 0,
374 'x.doc.show_result' => 0,
375 },
376 ],
377 features => {
378 dry_run => 1,
379 },
380 deps => {
381 prog => "ffmpeg", # XXX allow FFMPEG_PATH
382 },
383 links => [
384 {url=>'prog:srtsplit', summary=>'Split .srt by duration, much like this utility'},
385 ],
386 };
387 0 ▶sub split_video_by_duration {
Subroutine split_video_by_duration: not called CC=21 SCAR=61.4
Pod documented
388 0 require POSIX;
389  
390 0 my %args = @_;
391 0 my $files = $args{files};
392 0 my $every = $args{every};
393 0 my $parts = $args{parts};
394  
395 0 my $envres = envresmulti();
396 0 my $j = -1;
397 0 for my $file (@$files) {
398  
399 0 $j++;
400 0 log_info "Processing file %s ...", $file;
401  
402 0 require Media::Info;
403 0 my $res = Media::Info::get_media_info(media => $file);
404 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
405 0 $envres->add_result($res->[0], "Can't get info for video $file: $res->[1]", {item_id=>$j});
406 0 next;
407 }
408  
409 0 my $total_dur = $res->[2]{duration};
410 0 ▶ unless ($total_dur) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total_dur) 0 0 0
411 0 $envres->add_result(412, "Duration of video $file is zero", {item_id=>$j});
412 0 next;
413 }
414  
415 0 my $part_dur;
416 0 ▶ if (defined $parts) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $parts) { } 0 0 0
elsif (defined $every) { } 0 0 0
417 0 ▶ return [400, "Please specify a positive number in --parts"] unless $parts >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $parts >= 1 0 0 0
418 0 $part_dur = $total_dur / $parts;
419 } elsif (defined $every) {
420 0 ▶ if ($every =~ s/%\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($every =~ s/%\z//) { } 0 0 0
421 0 ▶ $every > 0 && $every <= 100 or return [400, "For percentage in --every, please specify a number between 0 and 100"];
Branch: 1 branch
BranchTrueFalseTotal
$every > 0 and $every <= 100 or return [400, "For percentage in --every, please specify a number between 0 and 100"] 0 0 0
Condition0%0/3
$every > 0 and $every <= 100
!ll&&!rl&&r
000
MC/DC0%0/2
$every > 0 and $every <= 100
$every > 0 $every <= 100
Truth table0%0/3
A and B
A= $every > 0B= $every <= 100
ABresult
0X0
100
111
422 0 $part_dur = $total_dur * ($every/100);
423 } else {
424 0 ▶ $every > 0 or return [400, "Please specify a positive number in --every"];
Branch: 1 branch
BranchTrueFalseTotal
$every > 0 or return [400, "Please specify a positive number in --every"] 0 0 0
425 0 $part_dur = $every;
426 }
427 } else {
428 0 return [400, "Please specify either --every or --parts"];
429 }
430  
431 0 my $num_parts = POSIX::ceil($total_dur / $part_dur);
432 0 ▶ my $fmt = $num_parts >= 1000 ? "%04d" : $num_parts >= 100 ? "%03d" : $num_parts >= 10 ? "%02d" : "%d";
Branch: 3 branches
BranchTrueFalseTotal
$num_parts >= 1000 ? : 0 0 0
$num_parts >= 100 ? : 0 0 0
$num_parts >= 10 ? : 0 0 0
433  
434 0 ▶ unless ($num_parts >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num_parts >= 2) 0 0 0
435 0 $envres->add_result(304, "No split necessary for video $file", {item_id=>$j});
436 0 next;
437 }
438  
439 0 require IPC::System::Options;
440 0 for my $i (1..$num_parts) {
441 0 my $part_label = sprintf "${fmt}of%d", $i, $num_parts;
442 0 my $ofile = $file;
443 0 ▶ if ($ofile =~ /\.\w+\z/) { $ofile =~ s/(\.\w+)\z/.$part_label$1/ } else { $ofile .= ".$part_label" }
Branch: 1 branch
BranchTrueFalseTotal
if ($ofile =~ /\.\w+\z/) { } 0 0 0
444 0 my $time_start = ($i-1)*$part_dur;
445 IPC::System::Options::system(
446 {log=>1, dry_run=>$args{-dry_run}},
447 0 ▶ "ffmpeg", ($args{overwrite} ? "-y":"-n"), "-i", $file, ($args{copy} ? ("-c", "copy") : ()), "-ss", $time_start, "-t", $part_dur, $ofile);
Branch: 2 branches
BranchTrueFalseTotal
$args{'overwrite'} ? : 0 0 0
$args{'copy'} ? : 0 0 0
448 0 ▶ my ($exit_code, $signal, $core_dump) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
449 0 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) { } 0 0 0
450 0 $envres->add_result(500, "ffmpeg exited $exit_code (sig $signal) for video $file: $!", {item_id=>$j});
451 } else {
452 0 $envres->add_result(200, "Video $file successfully split", {item_id=>$j});
453 }
454 }
455  
456 } # for $file
457  
458 0 $envres->as_struct;
459 }
460  
461 $SPEC{cut_video_by_duration} = {
462 1 v => 1.1,
463 summary => 'Get a portion (time range) of a video',
464 description => <<'MARKDOWN',
465  
466 This utility uses *ffmpeg* (particularly the `-t` and `-ss`) option to get a
467 portion (time range) of a video.
468  
469 Compared to using `ffmpeg` directly, this wrapper offers convenience of more
470 flexibility in specifying times and duration (e.g. '15s' as well as '00:10' as
471 well as 'PT1M30S'), specifying only 'end' and 'duration', handling multiple
472 files, automatically choosing output filename, and tab completion.
473  
474 MARKDOWN
475 args => {
476 %argspec0_files,
477 %argspecsopt_duration,
478 %argspecopt_copy,
479 overwrite => {
480 schema => 'bool*',
481 cmdline_aliases => {O=>{}},
482 },
483 },
484 examples => [
485 {
486 summary => 'Specify start only, the result is 100s.cut_40_to_100.mp4',
487 argv => ['100s.mp4', '-s', '40s'],
488 test => 0,
489 'x.doc.show_result' => 0,
490 },
491 {
492 summary => 'Specify duration only (using percentage) of two files, the results are 100s.cut_0_to_30.mp4 & 50s.cut_0_to_15.mp4',
493 argv => ['100s.mp4', '50s.mp4', '-d', '30%'],
494 test => 0,
495 'x.doc.show_result' => 0,
496 },
497 {
498 summary => 'Specify end only (using h:m:s notation), the result is 100s.cut_0_to_63.mp4',
499 argv => ['100s.mp4', '-e', '00:01:03'],
500 test => 0,
501 'x.doc.show_result' => 0,
502 },
503 {
504 summary => 'Specify start & end only (using h:m:s notation), the result is 100s.cut_50_to_63.mp4',
505 argv => ['100s.mp4', '-s', '00:00:50', '-e', '00:01:03'],
506 test => 0,
507 'x.doc.show_result' => 0,
508 },
509 {
510 summary => 'Specify start & duration only (using seconds), the result is 100s.cut_50_to_63.mp4',
511 argv => ['100s.mp4', '-s', '50', '-d', '13'],
512 test => 0,
513 'x.doc.show_result' => 0,
514 },
515 {
516 summary => 'Specify end & duration only (using human & ISO 8601 notation), the result is 100s.cut_50_to_63.mp4',
517 argv => ['100s.mp4', '-e', 'PT63S', '-d', '13s'],
518 test => 0,
519 'x.doc.show_result' => 0,
520 },
521 ],
522 features => {
523 dry_run => 1,
524 },
525 deps => {
526 prog => "ffmpeg", # XXX allow FFMPEG_PATH
527 },
528 links => [
529 ],
530 };
531 0 ▶sub cut_video_by_duration {
Subroutine cut_video_by_duration: not called CC=43 SCAR=75.5
Pod documented
532 0 require POSIX;
533  
534 0 my %args = @_;
535 0 my $files = $args{files};
536 0 ▶ my $start0 = $args{start} // 0;
Condition0%0/2
$args{"start"} // 0
l!l
00
MC/DC0%0/1
$args{"start"} // 0
$args{"start"}
Truth table0%0/2
A
A= $args{"start"}
Aresult
11
00
537 0 my $end0 = $args{end};
538 0 my $duration0 = $args{duration};
539  
540 0 my $envres = envresmulti();
541 0 my $j = -1;
542 0 for my $file (@$files) {
543 0 $j++;
544 0 require Media::Info;
545 0 my $res = Media::Info::get_media_info(media => $file);
546 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
547 0 $envres->add_result($res->[0], "Can't get media info for video '$file': $res->[1], skipped", {item_id=>$j});
548 0 next;
549 }
550  
551 my $total_dur = $res->[2]{duration}
552 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $total_dur = $res->[2]{"duration"} or do { $envres->add_result(412, "Duration of video '${file}' is zero, skipped", {"item_id", $j}); next } 0 0 0
553 0 $envres->add_result(412, "Duration of video '$file' is zero, skipped", {item_id=>$j});
554 0 next;
555 };
556  
557 0 my ($start, $end, $duration);
558 0 ▶ if (defined $start0 && !defined $end0 && !defined $duration0) {
Branch: 8 branches
BranchTrueFalseTotal
if (defined $start0 and not defined $end0 and not defined $duration0) { } 0 0 0
elsif (not defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (not defined $start0 || defined $end0 and defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (defined $start0 and not defined $end0 and defined $duration0) { } 0 0 0
elsif (not defined $start0 and defined $end0 and defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and defined $duration0) { } 0 0 0
Condition0%0/6
defined $start0 and not defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and not defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and not defined $end0 and not defined $duration0
defined $start0 not defined $end0 not defined $duration0
Truth table0%0/4
A and not(B) and not(C)
A= defined $start0B= not defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
559 0 $start = _convert_pct($start0, $total_dur);
560 0 $end = $total_dur;
561 0 $duration = $end - $start;
562 ▶ } elsif (!defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
not defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
not defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $start0 and defined $end0 and not defined $duration0
not defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
not(A) and B and not(C)
A= not defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
563 0 $start = 0;
564 0 $end = _convert_pct($end0, $total_dur);
565 0 $duration = $end - $start;
566 ▶ } elsif (!defined $start0 && !defined $end0 && defined $duration0) {
Condition0%0/6
not defined $start0 || defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 || defined $end0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $start0 || defined $end0 and defined $duration0
defined $start0 defined $end0 defined $duration0
Truth table0%0/4
not(A || B) and C
A= defined $start0B= defined $end0C= defined $duration0
ABCresult
1XX0
01X0
0000
0011
567 0 $start = 0;
568 0 $end = _convert_pct($duration0, $total_dur);
569 0 $duration = $duration0;
570 ▶ } elsif (defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and not defined $duration0
defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
A and B and not(C)
A= defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
571 0 $start = _convert_pct($start0, $total_dur);
572 0 $end = _convert_pct($end0, $total_dur);
573 0 $duration = $end - $start;
574 ▶ } elsif (defined $start0 && !defined $end0 && defined $duration0) {
Condition0%0/6
defined $start0 and not defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 and not defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and not defined $end0 and defined $duration0
defined $start0 not defined $end0 defined $duration0
Truth table0%0/4
A and not(B) and C
A= defined $start0B= not defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
575 0 $start = _convert_pct($start0, $total_dur);
576 0 $duration = _convert_pct($duration0, $total_dur);
577 0 $end = $start + $duration;
578 ▶ } elsif (!defined $start0 && defined $end0 && defined $duration0) {
Condition0%0/6
not defined $start0 and defined $end0 and defined $duration0
!ll&&!rl&&r
000
not defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $start0 and defined $end0 and defined $duration0
not defined $start0 defined $end0 defined $duration0
Truth table0%0/4
not(A) and B and C
A= not defined $start0B= defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
579 0 $end = _convert_pct($end0, $total_dur);
580 0 $duration = _convert_pct($duration0, $total_dur);
581 0 $start = $end - $duration;
582 ▶ } elsif (defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and not defined $duration0
defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
A and B and not(C)
A= defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
583 0 $start = _convert_pct($start0, $total_dur);
584 0 $end = _convert_pct($end0, $total_dur);
585 0 $duration = $end - $start;
586 ▶ } elsif (defined $start0 && defined $end0 && defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and defined $duration0
defined $start0 defined $end0 defined $duration0
Truth table0%0/4
A and B and C
A= defined $start0B= defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
587 0 return [409, "Please only specify 'start' & 'end', 'start' & 'duration', or 'end' & 'duration', not all three"];
588 } else {
589 0 return [500, "Internal bug"];
590 }
591  
592 0 ▶ do { $envres->add_result(400, "Start time ($start) for video '$file' is negative, skipped", {item_id=>$j}); next } if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
593 0 ▶ do { $envres->add_result(400, "End time ($end) for video '$file' is negative, skipped", {item_id=>$j}); next } if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 0 0
594 0 ▶ do { $envres->add_result(400, "Non-positive duration ($duration) for video '$file', skipped", {item_id=>$j}); next } if $duration <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($duration <= 0) 0 0 0
595 0 ▶ do { $envres->add_result(400, "Start time ($start) is larger than end time ($end) for video '$file', skipped", {item_id=>$j}); next } if $end < $start;
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $start) 0 0 0
596 0 ▶ do { $envres->add_result(400, "Start time ($start) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $start > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $total_dur) 0 0 0
597 0 ▶ do { $envres->add_result(400, "End time ($end) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $start > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $total_dur) 0 0 0
598 0 ▶ do { $envres->add_result(400, "Duration ($duration) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $duration > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($duration > $total_dur) 0 0 0
599  
600 0 ▶ if ($start == 0 && $end == $total_dur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0 and $end == $total_dur) 0 0 0
Condition0%0/3
$start == 0 and $end == $total_dur
!ll&&!rl&&r
000
MC/DC0%0/2
$start == 0 and $end == $total_dur
$start == 0 $end == $total_dur
Truth table0%0/3
A and B
A= $start == 0B= $end == $total_dur
ABresult
0X0
100
111
601 0 $envres->add_result(304, "No cutting necessary for video '$file' (whole length requested), skipped", {item_id=>$j});
602 0 next;
603 }
604  
605 0 log_trace "Video %s, start=%s, duration=%s, end=%s", $file, $start, $duration, $end;
606 0 require IPC::System::Options;
607 0 my $label = sprintf 'cut_%s_to_%s', $start, $end;
608 0 my $ofile = $file;
609 0 ▶ if ($ofile =~ /\.\w+\z/) { $ofile =~ s/(\.\w+)\z/.$label$1/ } else { $ofile .= ".$label" }
Branch: 1 branch
BranchTrueFalseTotal
if ($ofile =~ /\.\w+\z/) { } 0 0 0
610 IPC::System::Options::system(
611 {log=>1, dry_run=>$args{-dry_run}},
612 0 ▶ "ffmpeg", ($args{overwrite} ? "-y":"-n"), "-i", $file, ($args{copy} ? ("-c", "copy") : ()), "-ss", $start, "-t", $duration, $ofile);
Branch: 2 branches
BranchTrueFalseTotal
$args{'overwrite'} ? : 0 0 0
$args{'copy'} ? : 0 0 0
613 0 ▶ my ($exit_code, $signal, $core_dump) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
614 0 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) { } 0 0 0
615 0 $envres->add_result(500, "ffmpeg exited $exit_code (sig $signal) for video $file: $!", {item_id=>$j});
616 } else {
617 0 $envres->add_result(200, "Video '$file' successfully cut", {item_id=>$j});
618 }
619  
620 } # for $file
621  
622 0 $envres->as_struct;
623 }
624  
625 $SPEC{cut_duration_from_video} = {
626 1 v => 1.1,
627 summary => 'Cut (censor out) a duration out of a video',
628 description => <<'MARKDOWN',
629  
630 This utility uses *ffmpeg* (particularly the `-t` and `-ss`) option to cut a
631 portion (time range) out of a video. It can be used to remove an unwanted scene
632 from a video.
633  
634 Compared to using `ffmpeg` directly, this wrapper offers convenience of more
635 flexibility in specifying times and duration (e.g. '15s' as well as '00:10' as
636 well as 'PT1M30S'), specifying only 'end' and 'duration', handling multiple
637 files, automatically choosing output filename, and tab completion.
638  
639 Alternatives:
640  
641 1. If you just want to play a video and censor out certain parts, you can use
642 create a playlist of segments called an EDL file. See for example:
643  
644 <https://github.com/mpv-player/mpv/blob/master/DOCS/edl-mpv.rst>
645  
646 MARKDOWN
647 args => {
648 %argspec0_files,
649 %argspecsopt_duration,
650 %argspecopt_copy,
651 },
652 args_rels => {
653 req_one => [qw/start duration/],
654 },
655 examples => [
656 {
657 summary => 'Specify start & end only (using h:m:s notation), the result is 100s.cut_50_to_63.mp4',
658 argv => ['100s.mp4', '-s', '00:00:50', '-e', '00:01:03'],
659 test => 0,
660 'x.doc.show_result' => 0,
661 },
662 {
663 summary => 'Specify start & duration only (using seconds), the result is 100s.cut_50_to_63.mp4',
664 argv => ['100s.mp4', '-s', '50', '-d', '13'],
665 test => 0,
666 'x.doc.show_result' => 0,
667 },
668 {
669 summary => 'Specify end & duration only (using human & ISO 8601 notation), the result is 100s.cut_50_to_63.mp4',
670 argv => ['100s.mp4', '-e', 'PT63S', '-d', '13s'],
671 test => 0,
672 'x.doc.show_result' => 0,
673 },
674 ],
675 features => {
676 dry_run => 1,
677 },
678 deps => {
679 prog => "ffmpeg", # XXX allow FFMPEG_PATH
680 },
681 links => [
682 ],
683 };
684 0 ▶sub cut_duration_from_video {
Subroutine cut_duration_from_video: not called CC=48 SCAR=77.6
Pod documented
685 0 require File::Temp;
686 0 require POSIX;
687  
688 0 my %args = @_;
689 0 my $files = $args{files};
690 0 my $start0 = $args{start};
691 0 my $end0 = $args{end};
692 0 my $duration0 = $args{duration};
693  
694 # TODO: factour out common code with cut_video_by_duration
695 0 my $envres = envresmulti();
696 0 my $j = -1;
697 FILE:
698 0 for my $file (@$files) {
699 0 $j++;
700 0 require Media::Info;
701 0 my $res = Media::Info::get_media_info(media => $file);
702 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
703 0 $envres->add_result($res->[0], "Can't get media info for video '$file': $res->[1], skipped", {item_id=>$j});
704 0 next;
705 }
706  
707 my $total_dur = $res->[2]{duration}
708 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $total_dur = $res->[2]{"duration"} or do { $envres->add_result(412, "Duration of video '${file}' is zero, skipped", {"item_id", $j}); next } 0 0 0
709 0 $envres->add_result(412, "Duration of video '$file' is zero, skipped", {item_id=>$j});
710 0 next;
711 };
712  
713 0 my ($start, $end, $duration);
714 0 ▶ if (defined $start0 && !defined $end0 && !defined $duration0) {
Branch: 8 branches
BranchTrueFalseTotal
if (defined $start0 and not defined $end0 and not defined $duration0) { } 0 0 0
elsif (not defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (not defined $start0 || defined $end0 and defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (defined $start0 and not defined $end0 and defined $duration0) { } 0 0 0
elsif (not defined $start0 and defined $end0 and defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and not defined $duration0) { } 0 0 0
elsif (defined $start0 and defined $end0 and defined $duration0) { } 0 0 0
Condition0%0/6
defined $start0 and not defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and not defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and not defined $end0 and not defined $duration0
defined $start0 not defined $end0 not defined $duration0
Truth table0%0/4
A and not(B) and not(C)
A= defined $start0B= not defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
715 0 $start = _convert_pct($start0, $total_dur);
716 0 $end = $total_dur;
717 0 $duration = $end - $start;
718 ▶ } elsif (!defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
not defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
not defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $start0 and defined $end0 and not defined $duration0
not defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
not(A) and B and not(C)
A= not defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
719 0 $start = 0;
720 0 $end = _convert_pct($end0, $total_dur);
721 0 $duration = $end - $start;
722 ▶ } elsif (!defined $start0 && !defined $end0 && defined $duration0) {
Condition0%0/6
not defined $start0 || defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 || defined $end0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $start0 || defined $end0 and defined $duration0
defined $start0 defined $end0 defined $duration0
Truth table0%0/4
not(A || B) and C
A= defined $start0B= defined $end0C= defined $duration0
ABCresult
1XX0
01X0
0000
0011
723 0 $start = 0;
724 0 $end = _convert_pct($duration0, $total_dur);
725 0 $duration = $duration0;
726 ▶ } elsif (defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and not defined $duration0
defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
A and B and not(C)
A= defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
727 0 $start = _convert_pct($start0, $total_dur);
728 0 $end = _convert_pct($end0, $total_dur);
729 0 $duration = $end - $start;
730 ▶ } elsif (defined $start0 && !defined $end0 && defined $duration0) {
Condition0%0/6
defined $start0 and not defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 and not defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and not defined $end0 and defined $duration0
defined $start0 not defined $end0 defined $duration0
Truth table0%0/4
A and not(B) and C
A= defined $start0B= not defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
731 0 $start = _convert_pct($start0, $total_dur);
732 0 $duration = _convert_pct($duration0, $total_dur);
733 0 $end = $start + $duration;
734 ▶ } elsif (!defined $start0 && defined $end0 && defined $duration0) {
Condition0%0/6
not defined $start0 and defined $end0 and defined $duration0
!ll&&!rl&&r
000
not defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $start0 and defined $end0 and defined $duration0
not defined $start0 defined $end0 defined $duration0
Truth table0%0/4
not(A) and B and C
A= not defined $start0B= defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
735 0 $end = _convert_pct($end0, $total_dur);
736 0 $duration = _convert_pct($duration0, $total_dur);
737 0 $start = $end - $duration;
738 ▶ } elsif (defined $start0 && defined $end0 && !defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and not defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and not defined $duration0
defined $start0 defined $end0 not defined $duration0
Truth table0%0/4
A and B and not(C)
A= defined $start0B= defined $end0C= not defined $duration0
ABCresult
0XX0
10X0
1100
1111
739 0 $start = _convert_pct($start0, $total_dur);
740 0 $end = _convert_pct($end0, $total_dur);
741 0 $duration = $end - $start;
742 ▶ } elsif (defined $start0 && defined $end0 && defined $duration0) {
Condition0%0/6
defined $start0 and defined $end0 and defined $duration0
!ll&&!rl&&r
000
defined $start0 and defined $end0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $start0 and defined $end0 and defined $duration0
defined $start0 defined $end0 defined $duration0
Truth table0%0/4
A and B and C
A= defined $start0B= defined $end0C= defined $duration0
ABCresult
0XX0
10X0
1100
1111
743 0 return [409, "Please only specify 'start' & 'end', 'start' & 'duration', or 'end' & 'duration', not all three"];
744 } else {
745 0 return [500, "Internal bug"];
746 }
747  
748 0 ▶ do { $envres->add_result(400, "Start time ($start) for video '$file' is negative, skipped", {item_id=>$j}); next } if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
749 0 ▶ do { $envres->add_result(400, "End time ($end) for video '$file' is negative, skipped", {item_id=>$j}); next } if $end < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 0 0
750 0 ▶ do { $envres->add_result(400, "Non-positive duration ($duration) for video '$file', skipped", {item_id=>$j}); next } if $duration <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($duration <= 0) 0 0 0
751 0 ▶ do { $envres->add_result(400, "Start time ($start) is larger than end time ($end) for video '$file', skipped", {item_id=>$j}); next } if $end < $start;
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $start) 0 0 0
752 0 ▶ do { $envres->add_result(400, "Start time ($start) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $start > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $total_dur) 0 0 0
753 0 ▶ do { $envres->add_result(400, "End time ($end) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $start > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $total_dur) 0 0 0
754 0 ▶ do { $envres->add_result(400, "Duration ($duration) exceeds the total duration of video for video '$file', skipped", {item_id=>$j}); next } if $duration > $total_dur;
Branch: 1 branch
BranchTrueFalseTotal
if ($duration > $total_dur) 0 0 0
755  
756 0 ▶ if ($start == 0 && $end == $total_dur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0 and $end == $total_dur) 0 0 0
Condition0%0/3
$start == 0 and $end == $total_dur
!ll&&!rl&&r
000
MC/DC0%0/2
$start == 0 and $end == $total_dur
$start == 0 $end == $total_dur
Truth table0%0/3
A and B
A= $start == 0B= $end == $total_dur
ABresult
0X0
100
111
757 0 $envres->add_result(409, "Can't cut out the whole video duration", {item_id=>$j});
758 0 next;
759 }
760  
761 0 log_trace "Video %s, start=%s, duration=%s, end=%s", $file, $start, $duration, $end;
762 0 require IPC::System::Options;
763  
764 0 my $label1 = sprintf 'cut_out_%s_to_%s_left', $start, $end;
765 0 my $ofile1 = $file;
766 0 ▶ if ($ofile1 =~ /\.\w+\z/) { $ofile1 =~ s/(\.\w+)\z/.$label1$1/ } else { $ofile1 .= ".$label1" }
Branch: 1 branch
BranchTrueFalseTotal
if ($ofile1 =~ /\.\w+\z/) { } 0 0 0
767 IPC::System::Options::system(
768 {log=>1, dry_run=>$args{-dry_run}},
769 0 ▶ "ffmpeg", "-i", $file, ($args{copy} ? ("-c", "copy") : ()), "-ss", 0, "-t", $start, $ofile1);
Branch: 1 branch
BranchTrueFalseTotal
$args{'copy'} ? : 0 0 0
770 0 ▶ my ($exit_code1, $signal1, $core_dump1) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
771 0 ▶ if ($exit_code1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code1) 0 0 0
772 0 $envres->add_result(500, "ffmpeg exited $exit_code1 (sig $signal1) for video $file (left part): $!", {item_id=>$j});
773 0 next FILE;
774 }
775  
776 0 my $label2 = sprintf 'cut_out_%s_to_%s_right', $start, $end;
777 0 my $ofile2 = $file;
778 0 ▶ if ($ofile2 =~ /\.\w+\z/) { $ofile2 =~ s/(\.\w+)\z/.$label2$1/ } else { $ofile2 .= ".$label2" }
Branch: 1 branch
BranchTrueFalseTotal
if ($ofile2 =~ /\.\w+\z/) { } 0 0 0
779 IPC::System::Options::system(
780 {log=>1, dry_run=>$args{-dry_run}},
781 0 ▶ "ffmpeg", "-i", $file, ($args{copy} ? ("-c", "copy") : ()), "-ss", $start+$duration, "-t", $total_dur-$duration, $ofile2);
Branch: 1 branch
BranchTrueFalseTotal
$args{'copy'} ? : 0 0 0
782 0 ▶ my ($exit_code2, $signal2, $core_dump2) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
783 0 ▶ if ($exit_code2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code2) 0 0 0
784 0 $envres->add_result(500, "ffmpeg exited $exit_code2 (sig $signal2) for video $file (right part): $!", {item_id=>$j});
785 0 next FILE;
786 }
787  
788 0 my ($tempfh, $tempname) = File::Temp::tempfile(DIR => $CWD);
789 0 print $tempfh "file $ofile1\n";
790 0 print $tempfh "file $ofile2\n";
791 0 close $tempfh;
792  
793 0 my $label3 = sprintf 'cut_out_%s_to_%s', $start, $end;
794 0 my $ofile3 = $file;
795 0 ▶ if ($ofile3 =~ /\.\w+\z/) { $ofile3 =~ s/(\.\w+)\z/.$label3$1/ } else { $ofile3 .= ".$label2" }
Branch: 1 branch
BranchTrueFalseTotal
if ($ofile3 =~ /\.\w+\z/) { } 0 0 0
796 IPC::System::Options::system(
797 {log=>1, dry_run=>$args{-dry_run}},
798 0 "ffmpeg", "-f", "concat", "-i", $tempname, "-c", "copy", $ofile3,
799 );
800 0 ▶ my ($exit_code3, $signal3, $core_dump3) = ($? < 0 ? $? : $? >> 8, $? & 127, $? & 128);
Branch: 1 branch
BranchTrueFalseTotal
$? < 0 ? : 0 0 0
801 0 ▶ if ($exit_code3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code3) 0 0 0
802 0 $envres->add_result(500, "ffmpeg exited $exit_code3 (sig $signal3) for video $file (merging): $!", {item_id=>$j});
803 0 next FILE;
804 }
805  
806 0 unlink $ofile1;
807 0 unlink $ofile2;
808 0 unlink $tempname;
809  
810 0 $envres->add_result(200, "Video $file successfully censored", {item_id=>$j});
811 } # for $file
812  
813 0 $envres->as_struct;
814 }
815  
816 $SPEC{join_audio} = {
817 1 v => 1.1,
818 summary => 'Join two or more audios (e.g. MP3 files)',
819 description => <<'MARKDOWN',
820  
821 This utility uses *ffmpeg* to join two or more audio files (e.g. MP3 files) into
822 one.
823  
824 MARKDOWN
825 args => {
826 %argspec0_files,
827 %argspecopt_copy_default1,
828 output => {
829 schema => 'filename*',
830 cmdline_aliases => {o=>{}},
831 default => 'joined.mp3', # XXX should follow extension of input
832 },
833 %argspecopt_overwrite,
834 },
835 examples => [
836 {
837 summary => 'Join 3 MP3 files, output in joined.mp3',
838 argv => ['1.mp3', '2.mp3', '3.mp3'],
839 test => 0,
840 'x.doc.show_result' => 0,
841 },
842 ],
843 features => {
844 dry_run => 1,
845 },
846 deps => {
847 prog => "ffmpeg", # XXX allow FFMPEG_PATH
848 },
849 links => [
850 ],
851 };
852 0 ▶sub join_audio {
Subroutine join_audio: not called CC=7 SCAR=40.3
Pod documented
853 0 require Cwd;
854 0 require File::Temp;
855 0 require IPC::System::Options;
856  
857 0 my %args = @_;
858 0 my $files = $args{files};
859 0 ▶ unless (@$files >= 2) { return [400, "Please specify two or more audio files"] }
Branch: 1 branch
BranchTrueFalseTotal
unless (@$files >= 2) 0 0 0
860 0 my $copy = $args{copy};
861 0 ▶ my $output = $args{output} // 'joined.mp3';
Condition0%0/2
$args{"output"} // "joined.mp3"
l!l
00
MC/DC0%0/1
$args{"output"} // "joined.mp3"
$args{"output"}
Truth table0%0/2
A
A= $args{"output"}
Aresult
11
00
862 0 my $overwrite = $args{overwrite};
863  
864 0 ▶ if (-f $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $output) 0 0 0
865 0 ▶ if ($overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overwrite) { } 0 0 0
866 0 log_info "Will overwrite output file '$output'";
867 } else {
868 0 return [409, "Refusing to overwrite existing output file '$output', use --overwrite to overwrite"];
869 }
870 }
871  
872 0 my ($tempfh, $tempname) = File::Temp::tempfile(DIR => $CWD);
873 0 for my $file (@$files) {
874 0 print $tempfh "file '", Cwd::abs_path($file), "'\n";
875 }
876 0 close $tempfh;
877  
878 0 ▶ IPC::System::Options::system(
Branch: 1 branch
BranchTrueFalseTotal
$copy ? : 0 0 0
879 {log=>1, die=>1},
880 "ffmpeg", "-f", "concat",
881 "-safe", 0,
882 "-i", $tempname,
883 ($copy ? ("-c", "copy") : ()),
884 $output,
885 );
886  
887 0 [200];
888 }
889  
890 1 1;
891 # ABSTRACT: Utilities related to ffmpeg
892  
893 __END__
↑ Summary