Skip to content

Instantly share code, notes, and snippets.

@carlosmcevilly
Last active September 28, 2021 05:22
Show Gist options
  • Select an option

  • Save carlosmcevilly/1881721 to your computer and use it in GitHub Desktop.

Select an option

Save carlosmcevilly/1881721 to your computer and use it in GitHub Desktop.

Revisions

  1. carlosmcevilly revised this gist Jul 29, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #!/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 ?
    @@ -22,7 +25,7 @@ while ($duration > 0) {
    my $seg_length = ($duration > $length) ? $length : $duration;
    my $partfile = $infile;
    $partfile =~ s/(.*)\.(MOV|mov)/$1-part$segnum.mov/g;
    my $cmd = "ffmpeg -vcodec copy -ss $position -t $seg_length -i $infile $partfile";
    my $cmd = "ffmpeg -ss $position -i $infile -t $seg_length -vcodec copy $partfile";
    print "running [$cmd]\n";
    `$cmd`;
    $position += $seg_length;
    @@ -50,4 +53,3 @@ sub get_duration_seconds {
    }
    return $duration_seconds;
    }

  2. carlosmcevilly created this gist Feb 22, 2012.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/perl

    # -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 -vcodec copy -ss $position -t $seg_length -i $infile $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;
    }