#!/usr/bin/env ruby

video_path = ARGV[0]
thumbnail_path = ARGV[1]
time = ARGV[2] || "00:00:00.000"

unless video_path && thumbnail_path
  $stderr.puts "Usage: #{$0} <video-path> <thumbnail-path> [HH:MM:SS.MMM]"
  exit 10
end

unless File.exists?(video_path)
  $stderr.puts "Input video does not exist: #{video_path}"
  exit 20
end

if File.exists?(thumbnail_path)
  $stderr.puts "Cowardly refusing to overwrite existing file at thumbnail path: #{thumbnail_path}"
  exit 30
end

`ffmpeg -i '#{video_path}' -ss '#{time}' -vframes 1 '#{thumbnail_path}'`
exit $?.exitstatus
