Skip to content

Instantly share code, notes, and snippets.

@vitaLee
Last active December 14, 2015 06:59
Show Gist options
  • Save vitaLee/5046620 to your computer and use it in GitHub Desktop.
Save vitaLee/5046620 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def video_rotation(video_path)
`mediainfo #{video_path} | grep -i rotation`.match(/(\d+)/)
$1.to_i
end
def video_dimensions(video_path)
dimensions = `mediainfo #{video_path} | egrep -i 'width|height'`.scan(/([\d\s]+)pixels/)
portrait_video?(video_path) && dimensions.reverse!
dimensions.join('x').delete(' ')
end
def video_transform(video_path)
{
0 => "",
90 => "-vf transpose=1",
180 => "-vf hflip,vflip",
270 => "-vf transpose=2"
}[video_rotation(video_path)]
end
def portrait_video?(video_path)
[90, 270].include? video_rotation(video_path)
end
def process_video(video_path)
output = "#{File.basename(video_path, '.*')}_rotated#{File.extname(video_path)}"
transform = video_transform(video_path)
`ffmpeg -i #{video_path} -metadata:s:v:0 rotate=0 #{transform} #{output}`
end
ARGV.each do |video_path|
process_video video_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment