Skip to content

Instantly share code, notes, and snippets.

@jacksenechal
Created February 12, 2011 10:45
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 jacksenechal/823686 to your computer and use it in GitHub Desktop.
Save jacksenechal/823686 to your computer and use it in GitHub Desktop.
A simple script to encode video files for Android using the h264 codec. Requires ruby, grep, and ffmpeg.
#!/usr/bin/ruby
#
# Copyright (C) 2011 by Jack Senechal
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#=================================================================#
# Set these here variables to match your phone's screen resolution
MAX_WIDTH = 480
MAX_HEIGHT = 320
#=================================================================#
VERSION = "0.1"
input = ARGV.first
unless (File.exists? input)
puts "Could not find file #{input}"
exit
end
puts "Android Video Encoder v#{VERSION}\n\n"
str = `ffmpeg -i "#{input}" 2>&1`
line = `echo "#{str}" | grep "PAR.*DAR"`
width = line.gsub(/^.* (\d+)x\d+ .*$/, '\1').to_i
height = line.gsub(/^.* \d+x(\d+) .*$/, '\1/').to_i
dar_x = line.gsub(/^.*DAR (\d+):\d+\].*$/, '\1/').to_i
dar_y = line.gsub(/^.*DAR \d+:(\d+)\].*$/, '\1/').to_i
ratio = dar_y.to_f / dar_x.to_f
output_width = width > MAX_WIDTH ? MAX_WIDTH : width
output_height = ((output_width * ratio) / 2).floor * 2
if output_height > MAX_HEIGHT
output_height = MAX_HEIGHT
output_width = ((output_height * (1/ratio)) / 2).floor * 2
end
output_name = File.basename(input, File.extname(input))
puts "input: #{input}"
puts "dimensions: #{width}x#{height}"
puts "aspect: #{dar_x}:#{dar_y}"
puts "ratio: #{ratio}"
puts "output dimensions: #{output_width}x#{output_height}"
command = %Q{ffmpeg -i "#{input}"
-b 384k -vcodec libx264 -flags +loop+mv4 -cmp 256 -partitions
+parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -subq 7
-trellis 1 -refs 5 -bf 0 -flags2 +mixed_refs -coder 0
-me_range 16 -g 250 -keyint_min 25 -sc_threshold 40
-i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4
-acodec libfaac -ab 128k -ac 2
-s #{output_width}x#{output_height}
-aspect #{dar_x}:#{dar_y}
"#{output_name}.mp4"}.gsub(/\n/, '')
puts "command: #{command}\n\n"
system(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment