Skip to content

Commit e2ceb25

Browse files
committed
new file: Video/video_concat_ffmpeg.pl
1 parent 1b40050 commit e2ceb25

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,8 @@ A nice collection of day-to-day Perl scripts.
12841284
* Time
12851285
* [Calendar](./Time/calendar.pl)
12861286
* [Contdown](./Time/contdown.pl)
1287+
* Video
1288+
* [Video concat ffmpeg](./Video/video_concat_ffmpeg.pl)
12871289
* Visualisators
12881290
* [Binview](./Visualisators/binview.pl)
12891291
* [Disk-stats](./Visualisators/disk-stats.pl)

Video/video_concat_ffmpeg.pl

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/perl
2+
3+
# Author: Daniel "Trizen" Șuteu
4+
# Date: 21 August 2025
5+
# https://github.com/trizen
6+
7+
# Concatenate multiple MP4 video files, given as arguments, into one single file called "CONCATENATED.mp4".
8+
9+
# Requires: ffmpeg
10+
11+
use 5.036;
12+
use File::Temp qw(tempfile tempdir);
13+
use File::Path qw(make_path);
14+
use File::Spec::Functions qw(catfile curdir);
15+
use Image::ExifTool qw(ImageInfo);
16+
17+
my $output_filename = "CONCATENATED.mp4";
18+
my $output_dir = tempdir(CLEANUP => 1, DIR => curdir());
19+
20+
sub new_tempfile {
21+
my ($fh, $filename) = tempfile("tmpfileXXXXX", SUFFIX => '.txt', UNLINK => 1);
22+
return ($fh, $filename);
23+
}
24+
25+
sub make_video_filename($i) {
26+
catfile($output_dir, sprintf('output_%05d.mp4', $i));
27+
}
28+
29+
sub make_ffmpeg_filename_entry($file) {
30+
sprintf("file '%s'\n", $file);
31+
}
32+
33+
sub ffmpeg_concat_files ($filename, $output_filename) {
34+
system('ffmpeg', '-loglevel', 'fatal', '-f', 'concat', '-i', $filename, '-c', 'copy', '-y', $output_filename);
35+
$? == 0 or die "Stopped with exit code = $?";
36+
}
37+
38+
my $mp4_version = undef;
39+
40+
my $i = 1;
41+
my ($fh, $filename) = new_tempfile();
42+
43+
foreach my $file (@ARGV) {
44+
45+
my $info = ImageInfo($file);
46+
my $version = $info->{'MajorBrand'};
47+
48+
$mp4_version //= $version;
49+
50+
if ($version ne $mp4_version) {
51+
$mp4_version = undef;
52+
ffmpeg_concat_files($filename, make_video_filename($i));
53+
($fh, $filename) = new_tempfile();
54+
++$i;
55+
}
56+
57+
print $fh make_ffmpeg_filename_entry($file);
58+
}
59+
60+
ffmpeg_concat_files($filename, make_video_filename($i));
61+
62+
($fh, $filename) = new_tempfile();
63+
64+
foreach my $k (1 .. $i) {
65+
my $file = make_video_filename($k);
66+
print $fh make_ffmpeg_filename_entry($file);
67+
}
68+
69+
close $fh;
70+
ffmpeg_concat_files($filename, $output_filename);

0 commit comments

Comments
 (0)