-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-video-parts
executable file
·47 lines (40 loc) · 1.14 KB
/
extract-video-parts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $prefix = 'part';
my $suffix = 'mp4';
die "Usage: ".basename($0)." file start1-end1 ...\n" unless @ARGV > 1;
my $video = shift;
die "No file $video.\n" unless -f $video;
my @times = ();
for my $arg (@ARGV) {
$arg =~ s/\s+//g;
if ($arg =~ /^(\d+):(\d+)-(\d+):(\d+)$/) {
my $start_seconds = 60*$1 + $2;
my $end_seconds = 60*$3 + $4;
my $duration = $end_seconds - $start_seconds;
push @times, $start_seconds;
push @times, $duration;
} else {
die "Illegal time specification '$arg', format must be M:SS-M:SS.\n";
}
}
my $i = 1;
my @partfiles;
while (@times) {
my $start_seconds = shift @times;
my $duration = shift @times;
my @cmd = ('ffmpeg');
push @cmd, '-ss', $start_seconds;
push @cmd, '-i', $video;
#push @cmd, '-c', 'copy';
push @cmd, '-t', $duration;
my $partfile = sprintf "%s%0*i.%s", $prefix, length(@times)/2, $i, $suffix;
push @cmd, '-y', $partfile;
push @partfiles, $partfile;
print "Part $i (start $start_seconds, duration $duration) ...\n";
#system(@cmd);
++$i;
}
print "Created part files: ".join(' ', @partfiles), "\n";