Skip to content

Instantly share code, notes, and snippets.

@Qata
Last active January 4, 2017 05:13
Show Gist options
  • Save Qata/749301f81c6940ecbf70bd5ff7262810 to your computer and use it in GitHub Desktop.
Save Qata/749301f81c6940ecbf70bd5ff7262810 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#Convert a video into chunks of a given size
chunk = ARGV.shift.to_f
file = ARGV.shift
extension = File.extname(file)
basename = File.join(File.dirname(file), File.basename(file, extension))
durations = `ffmpeg -i '#{file}' 2>&1`.match(/Duration: ([\d:.]*)/).captures.first.split(':')
seconds = durations[0].to_f * 3600 + durations[1].to_f * 60 + durations[2].to_f
n_chunks = (seconds / chunk).ceil
(0...n_chunks).each { |chunk_number|
filename = "#{basename} (Part #{chunk_number + 1} of #{n_chunks})#{extension}"
puts `ffmpeg -i '#{file}' -ss #{chunk * chunk_number} -t #{chunk} '#{filename}'`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment