Skip to content

Instantly share code, notes, and snippets.

@wiserfirst
Created June 20, 2021 22:37
Show Gist options
  • Save wiserfirst/f3214c475767292f1fd8d6fbadc1e856 to your computer and use it in GitHub Desktop.
Save wiserfirst/f3214c475767292f1fd8d6fbadc1e856 to your computer and use it in GitHub Desktop.
Convert/compress videos in a directory with ffmpeg
#!/usr/bin/env ruby
require 'shellwords'
def usage
puts <<~HEREDOC
Usage:
./video-converter.rb [dir]
to convert mov/avi files to mp4 with H.264 video codec and AAC audio codec"
HEREDOC
end
if ARGV.length > 1
usage
exit 1
end
dir = ARGV.length == 1 ? ARGV[0] : '.'
unless Dir.exist?(dir)
puts "\e[31mDirectory #{dir} not found\e[0m"
exit 1
end
output_dir = './output'
Dir.chdir(dir)
Dir.mkdir(output_dir, 0755) unless Dir.exist?(output_dir)
Dir.glob('*.{avi,mov,mp4}') do |filename|
basename = filename.split('.')[0]
input_filename = Shellwords.shellescape(filename)
output_filename = Shellwords.shellescape("#{output_dir}/#{basename}.mp4")
puts "\n\e[32mConverting #{input_filename} to #{output_filename}\e[0m"
system("ffmpeg -i #{input_filename} -vcodec h264 #{output_filename}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment