Skip to content

Instantly share code, notes, and snippets.

@zealot128
Created October 8, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zealot128/ca1220d2c4a15a5b6e596fc957d38868 to your computer and use it in GitHub Desktop.
Save zealot128/ca1220d2c4a15a5b6e596fc957d38868 to your computer and use it in GitHub Desktop.
ffmpeg cut of from start and back - ruby convert script

Cut of from start/back of file

Useful for postprocessing various youtube videos with jingles/ad "follow us" stuff at the end.

ffmpeg has an easy way to cut off from beginning, but not to say "cut of 20s from the end". So one has to use to do arithmetics with duration. Here, using ruby script.

USAGE

ruby convert.rb 6 22 file_to_video.mp4 out/

will cut off 6s from beginning and 22s from the end of the file and will write the result to out/ folder.

#!/usr/bin/env ruby
if ARGV.length != 4
$stderr.puts "USAGE: #{__FILE__} CutOfFromStart CutOfFromEnd InFile OutDir"
$stderr.puts " example : #{__FILE__} file.mp4 0 7 out/ will cut off 7 seconds from the end"
exit 1
end
require 'shellwords'
def e(arg)
Shellwords.escape(arg)
end
cut_of_front, cut_of_back, in_file, out_file = ARGV
length = `ffprobe -v 0 -show_entries format=duration -of compact=p=0:nk=1 #{e in_file}`.to_f
cmd = "ffmpeg -i #{e in_file} -ss #{cut_of_front.to_i} -to #{length.floor - cut_of_back.to_i} #{e File.join(out_file, in_file)}"
puts cmd
exec cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment