forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_split_ffmpeg.pl
More file actions
74 lines (54 loc) · 1.76 KB
/
video_split_ffmpeg.pl
File metadata and controls
74 lines (54 loc) · 1.76 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 24 August 2025
# https://github.com/trizen
# Split a video file into multiple parts of length `n` seconds, or into `n` equal parts.
# Requires: ffmpeg
use 5.036;
use Getopt::Long qw(GetOptions);
my $parts = undef;
my $duration = undef;
my $output_template = "PART_%04d.mp4";
sub usage($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [video.mp4]
options:
--parts=i : split into `i` equal parts
--duration=i : split into segments of length `i` seconds
--template=s : output filename template (default: $output_template)
--help : display this message and exit
example:
# Split video.mp4 into 3 equal parts
perl $0 --parts=3 video.mp4
# Split video.mp4 into equal parts of 10 seconds length
perl $0 --duration=10 video.mp4
EOT
exit($exit_code);
}
GetOptions(
"duration=i" => \$duration,
"parts=i" => \$parts,
"template=s" => \$output_template,
"h|help" => sub { usage() },
)
or die("Error in command line arguments\n");
if (!defined($parts) and !defined($duration)) {
usage(1);
}
my $input_video = shift(@ARGV) // usage(2);
if (not -f $input_video) {
die "Not a file <<$input_video>>: $!";
}
if (defined($parts)) {
$duration = `ffprobe -v error -show_entries format=duration -of csv=p=0 \Q$input_video\E`;
chomp($duration);
$duration /= $parts;
}
system(qw(ffmpeg -loglevel fatal -i), $input_video, qw(-acodec copy -f segment -segment_time),
$duration, qw(-vcodec copy -reset_timestamps 1 -map 0), $output_template);
if ($? == 0) {
say ":: Done!";
}
else {
die "Something went wrong! ffmpeg exit code: $?";
}