Last active
September 28, 2021 05:22
-
-
Save carlosmcevilly/1881721 to your computer and use it in GitHub Desktop.
split a .mov file without loss of quality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# command from: | |
# http://www.linuxquestions.org/questions/linux-newbie-8/how-do-you-split-mpg-files-using-a-ffmpeg-command-542607/ | |
# -vcodec copy copies the raw data | |
# -ss start seconds? | |
# -to ? | |
my $infile = shift; | |
my $length = shift; | |
die "usage: $0 <infile>\n\nexample: $0 IMG_1234.MOV\n\n" unless (defined($infile)); | |
$length ||= 15; | |
my $filebase = $infile; | |
$filebase =~ s/\.MOV//; | |
my $duration = get_duration_seconds($infile); | |
my $segnum = 0; | |
my $position = 0; | |
while ($duration > 0) { | |
$segnum++; | |
my $seg_length = ($duration > $length) ? $length : $duration; | |
my $partfile = $infile; | |
$partfile =~ s/(.*)\.(MOV|mov)/$1-part$segnum.mov/g; | |
my $cmd = "ffmpeg -ss $position -i $infile -t $seg_length -vcodec copy $partfile"; | |
print "running [$cmd]\n"; | |
`$cmd`; | |
$position += $seg_length; | |
$duration -= $seg_length; | |
} | |
sub get_duration_seconds { | |
my $file = shift; | |
my $hundredths = 0; | |
my $duration_seconds = 0; | |
my $resultfile = "ffmpeg.stderr"; | |
system("ffmpeg -i $file 1> ffmpeg.stdout 2> $resultfile"); | |
open(my $fh, $resultfile) or die "$0 error reading file $resultfile"; | |
while (my $line=<$fh>) { | |
# Duration: 00:02:23.36, start: 0.000000, bitrate: 10574 kb/s | |
if ($line =~ /^\s*Duration:\s+(\d+):(\d\d):(\d\d).(\d\d)/) { | |
my $hours = $1; | |
my $mins = $2; | |
my $seconds = $3; | |
my $hundredths = $4; | |
$seconds++ if ($hundredths); | |
$duration_seconds = ($hours * (60*60)) + ($mins * 60) + $seconds; | |
last; | |
} | |
} | |
return $duration_seconds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment