Skip to content

Instantly share code, notes, and snippets.

@hwgusn
Created September 14, 2019 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwgusn/104148a135044c1c3e442d7445285374 to your computer and use it in GitHub Desktop.
Save hwgusn/104148a135044c1c3e442d7445285374 to your computer and use it in GitHub Desktop.
[RUBY] executable that removes the first n and last m seconds from an MP4 video without re-encoding and saves it to an output directory out_dir.
#!/usr/bin/env ruby
# Removes first startTrim and last endTrim seconds of an MP4 file and stores the resulting file in out_dir.
FFMPEG = "ffmpeg" # => path to the FFmpeg
FFPROBE = "ffprobe" # => path to FFProbe
startTrim = 5
endTrim = 6
if RUBY_VERSION =~ /1\.8[\.\d]*/
puts "Doesn't work with #{RUBY_VERSION}, needs at least 1.9.2"
exit
end
if ARGV.length != 1
puts "Usage: NagOff.rb <input>"
puts " input filename needed"
exit
end
input_file = ARGV[0]
out_dir = "Trimmed"
# ----------------------------------------------------------------------------
begin
puts "This script will remove the first #{startTrim} and last #{endTrim} seconds from a specified video file and place it in a subdirectory of the original named #{out_dir}. The values for startTrim, endTrim, and out_dir can be set in PlayOff.rb. This script must be run from a command prompt in the directory where the original file is located."
#Creates the output directory if it does not exist.
if not File.directory?(out_dir)
puts "Output folder not found; will create."
system("mkdir #{out_dir}")
end
endTrimTemp = 5
endTrimFinal = 10
probeCommand = "#{FFPROBE} -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"#{input_file}\""
cmd = "|" + probeCommand
open(cmd, 'w+') do | subprocess |
subprocess.read.split("\n").each do |output|
puts "#{output}"
endTrimTemp = "#{output}".to_i
endTrimFinal = endTrimTemp-(startTrim+endTrim)
puts "#{endTrimTemp}"
puts "#{endTrimFinal}"
trimCommand = "#{FFMPEG} -ss #{startTrim} -i \"#{input_file}\" -c copy -to #{endTrimFinal} \"#{out_dir}/#{input_file}\""
puts "#{trimCommand}"
system(trimCommand)
puts "Great Success!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment