Skip to content

Instantly share code, notes, and snippets.

@zealot128
Created May 12, 2016 07:19
Show Gist options
  • Select an option

  • Save zealot128/69694e10ff7ec044431207880a63008a to your computer and use it in GitHub Desktop.

Select an option

Save zealot128/69694e10ff7ec044431207880a63008a to your computer and use it in GitHub Desktop.
recode all videos in current folder with ffmpeg (decreasing filesize by up to 80%)
# can also be *.flv or *.wmv etc.
files = Dir['*.mp4']
# min size
mb = 10
def size_mb(file)
(File.size(file).to_f / 1024 / 1024).round
end
# only select files that have no encoded version and are larger than `mb`
large = files.reject{|i| files.include?( "encoded_#{i}" ) }.reject{|i| i['encoded']}.select{|i| size_mb(i) > mb }
count = large.count
large.each_with_index do |file, i|
puts "[#{Time.now.to_s}] Converting #{file} (#{i+1}/#{count})..."
f = Shellwords.escape(file)
# preset veryfast, ca. 1:1 time encoding on MacAir and small files
# level 4 + profile main: most devices
# crf 25 -> Quality level, 0 -> Best, 23 -> default
# map_matadata -> Copy meta tags over
system "ffmpeg -i #{f} -preset veryfast -c:v libx264 -profile:v main -level 4 -c:a libfaac -q:a 100 -crf 25 -map_metadata 0 encoded_#{f} 2>/dev/null"
puts "[#{Time.now.to_s}] Before: #{size_mb(file)}mb; After: #{size_mb("encoded_#{file}")}mb"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment