Skip to content

Instantly share code, notes, and snippets.

@tomredsky
Created April 20, 2018 13:09
Show Gist options
  • Save tomredsky/13dff6e087874ca663926d8b13723ae0 to your computer and use it in GitHub Desktop.
Save tomredsky/13dff6e087874ca663926d8b13723ae0 to your computer and use it in GitHub Desktop.
ffmpeg command
ffmpeg -i /var/media/footage/k0/07/62/73/k0076273-hd.mov -v error -y -strict -2 -vcodec h264 -profile:v main -pix_fmt yuvj420p -crf 16 -movflags +faststart -b-pyramid none -weightp none -mixed-refs 0 -8x8dct 0 -trellis 0 -vf scale=384:-1 [scaled]; movie=/etc/perl/share/384x384wm_o36.png [logo]; [scaled][logo] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2:rgb=1 [out] /tmp/k0076273-preview-wm.mp4
@tomredsky
Copy link
Author

In case it helps, below is most of the code. It's the run_ffmpeg() that uses system() to call ffmpeg with the relevant args.

We recently upgraded ffmpeg to v3.4.2 and now it does like the arguments used in the for overlay
overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2:rgb=1

They are valid arguments; documentation is available for overlay_{w/h}

It's basically saying (main_width - overalay_width)/ divided in half.

my $ffprobe = '/usr/local/bin/ffprobe';

my $ffmpeg = '/usr/local/bin/ffmpeg';
my $ffmpegOptions = [ qw/
    -v error
    -y
    -strict -2
    -vcodec h264
    -profile:v main
    -pix_fmt yuvj420p
    -crf 16
    -movflags +faststart
    -b-pyramid none
    -weightp none
    -mixed-refs 0
    -8x8dct 0
    -trellis 0
/ ];
my $mouseoverOptions = [ qw/
    -an
    -t 00:00:45
    -x264opts level=1.2:bframes=1
/ ];
my $watermarkRoot = '/etc/perl/share';

my $pWmSuffix = 'wm_o36.png';
my $pWmPos = '(main_w-overlay_w)/2:(main_h-overlay_h)/2';
my $mWmH = 'wm-horizontal-128x17.png';
my $mWmPosH = 'main_w-overlay_w:main_h-overlay_h';
my $mWmV = 'wm-vertical-17x128.png';
my $mWmPosV = 'main_w-overlay_w:0';
my $scaleAndWatermarkParameters = {
# ratio => [ preview scale, preview wm, preview wm position, mouseover scale, mouseover wm, mouseover wm position, [ frame size, ... ] ]
    '16:9' => [ '512:-1', "512x288$pWmSuffix", $pWmPos, '256:-1',  $mWmH, $mWmPosH, [ '1920x1080', '1280x720', '1024x576', '864x486' ] ],
    '4:3'  => [ '448:-1', "448x336$pWmSuffix", $pWmPos, '256:-1',  $mWmH, $mWmPosH, [ '1440x1080',  '960x720',  '768x576', '640x480' ] ],
    '1:1'  => [ '384:-1', "384x384$pWmSuffix", $pWmPos, '224:224', $mWmH, $mWmPosH, [ '1080x1080',  '720x720',  '576x576', '480x480' ] ],
    '3:4'  => [ '-1:384', "vert$pWmSuffix",    $pWmPos,  '-1:256', $mWmV, $mWmPosV, [ '1080x1440',  '720x969',  '576x768', '480x640' ] ],
    '9:16' => [ '-1:384', "vert$pWmSuffix",    $pWmPos,  '-1:256', $mWmV, $mWmPosV, [ '1080x1920', '720x1280', '576x1024', '486x864' ] ],
};
my $sizeToRatio = { };
while ( my ($ar, $info) = each %$scaleAndWatermarkParameters ) {
    $sizeToRatio->{$_} = $ar
        for @{ $info->[-1] };
}

..

sub run_ffmpeg {
    my ( $input, $output, $options, $scale, $watermarkPath, $watermarkPosition ) = @_;

    my $watermarkFilter = $watermarkPath ? "[scaled]; movie=$watermarkRoot/$watermarkPath [logo]; [scaled][logo] overlay=$watermarkPosition:rgb=1" : '';
    my $filter = "scale=$scale $watermarkFilter [out]";
    print join ' ', $ffmpeg, ( $ffmpeg, '-i', $input, @$ffmpegOptions, @{ $options || [ ] }, '-vf', $filter, $output ), "\n";
    my $status = system $ffmpeg ( $ffmpeg, '-i', $input, @$ffmpegOptions, @{ $options || [ ] }, '-vf', $filter, $output );
    return $status;
}

sub make_previews {
    my ( $master, $preview, $previewWM, $mouseover, $mouseoverOld ) = @_;

    my $meta = run_ffprobe ($master);
    if (($meta->{codec} // '') eq 'qtrle') {
        say STDERR "$master appears to be an animation, skipping ...";
        return 'animation';
    }
    my ($w, $h) = @{$meta}{qw/width height/};
    my $ar = $sizeToRatio->{"${w}x${h}"} // $meta->{display_aspect_ratio};
    return "unexpected aspect ratio '$ar'"
        unless exists $scaleAndWatermarkParameters->{$ar};
    my (
        $previewScale, $previewWatermark, $previewWatermarkPosition,
        $mouseoverScale, $mouseoverWatermark, $mouseoverWatermarkPosition
    ) = @{ $scaleAndWatermarkParameters->{$ar} };
    my ($status, $overallStatus);
    if ($preview) {
        $status = run_ffmpeg ( $master, $preview, undef, $previewScale, undef, undef );
        warn "preview status = $status" if $status;
        $overallStatus ||= $status;
    }
    if ($previewWM) {
        $status = run_ffmpeg ( $master, $previewWM, undef, $previewScale, $previewWatermark, $previewWatermarkPosition );
        warn "preview (watermarked) status = $status" if $status;
        $overallStatus ||= $status;
    }
    if ($mouseover) {
        $status = run_ffmpeg ( $preview || $master, $mouseover, $mouseoverOptions, $mouseoverScale, $mouseoverWatermark, $mouseoverWatermarkPosition );
        warn "mouseover status = $status" if $status;
        $overallStatus ||= $status;
    }
    # XXX: mouseover .mov, will be phasing this out at some point
    if ($mouseoverOld) {
        $status = run_ffmpeg ( $preview || $master, $mouseoverOld, $mouseoverOptions, $mouseoverScale, $mouseoverWatermark, $mouseoverWatermarkPosition );
        warn "mouseover (old) status = $status" if $status;
        $overallStatus ||= $status;
    }
    return $overallStatus;
}

@robrwo
Copy link

robrwo commented Apr 20, 2018

Is this the generated statement?

I think the bits in square brackets are causing problems

@tomredsky
Copy link
Author

This is the error:

/usr/local/bin/ffmpeg /usr/local/bin/ffmpeg -i /var/media/footage/k0/07/62/73/k0076273-hd.mov -v error -y -strict -2 -vcodec h264 -profile:v main -pix_fmt yuvj420p -crf 16 -movflags +faststart -b-pyramid none -weightp none -mixed-refs 0 -8x8dct 0 -trellis 0 -vf scale=384:-1 "[scaled]; movie=/etc/perl/share/384x384wm_o36.png [logo]; [scaled][logo] overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2:rgb=1" [out] /var/media/footage/k0/07/62/73/k0076273-preview-wm.mp4
[Parsed_overlay_2 @ 0x3dacac0] Option 'rgb' not found
[AVFilterGraph @ 0x399a720] Error initializing filter 'overlay' with args '(main_w-overlay_w)/2:(main_h-overlay_h)/2:rgb=1"'
Error reinitializing filters!
Failed to inject frame into filter network: Option not found
Error while processing the decoded data for stream #0:0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment