1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 |
|
---|
6 | print "\n===== Split up HD Video =====\n";
|
---|
7 | print "Splits any TS files found in the given directory into 600 second\n";
|
---|
8 | print "segments. Requires FFMPEG.\n\n";
|
---|
9 |
|
---|
10 | if (!defined $ARGV[0] || !-d $ARGV[0])
|
---|
11 | {
|
---|
12 | print "usage: video_splitter.pl <dir with ts files>\n";
|
---|
13 | exit();
|
---|
14 | }
|
---|
15 |
|
---|
16 | opendir(DH, $ARGV[0]) or die("Failed to open directory for reading: " . $ARGV[0]);
|
---|
17 | my @files = readdir(DH);
|
---|
18 | closedir(DH);
|
---|
19 | my $counter = 0;
|
---|
20 | foreach my $file (@files)
|
---|
21 | {
|
---|
22 | if ($file =~ /^(.*)\.ts$/)
|
---|
23 | {
|
---|
24 | my $file_prefix = $1;
|
---|
25 | print " * Processing: " . $file . "\n";
|
---|
26 | my $path = $ARGV[0] . '/' . $file;
|
---|
27 | my $mediainfo_command = 'mediainfo --Inform="Video;%Duration/String%" "' . $path . '"';
|
---|
28 | my $raw_duration = `$mediainfo_command`;
|
---|
29 | my $duration = 0;
|
---|
30 | if ($raw_duration =~ /(\d+)h/)
|
---|
31 | {
|
---|
32 | $duration += $1 * 60;
|
---|
33 | }
|
---|
34 | if ($raw_duration =~ /(\d+)mn/)
|
---|
35 | {
|
---|
36 | $duration += $1;
|
---|
37 | }
|
---|
38 | print " - video is $duration minutes\n";
|
---|
39 | if ($duration > 0)
|
---|
40 | {
|
---|
41 | my $offset = 0;
|
---|
42 | my $step = 10;
|
---|
43 | while (($offset < 60) && ($offset + $step < $duration))
|
---|
44 | {
|
---|
45 | $counter++;
|
---|
46 | my $output_file = sprintf($file_prefix . '-%03d.ts', $counter);
|
---|
47 | my $output_path = $ARGV[0] . '/' . $output_file;
|
---|
48 | my $ffmpeg_command = sprintf('ffmpeg -ss 00:%02d:00 -t 600 -i "%s" -vcodec copy -acodec copy -scodec copy "%s" > /dev/null 2>&1', $offset, $path, $output_path);
|
---|
49 | print " - cmd: $ffmpeg_command\n";
|
---|
50 | print " - generating " . $output_file . "\n";
|
---|
51 | `$ffmpeg_command`;
|
---|
52 | #rint "DEBUG: $result\n\n";
|
---|
53 | $offset += $step;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | print "Complete!\n\n";
|
---|
59 | exit;
|
---|
60 | 1;
|
---|