I was able to solve the problem by using the mini_exiftool gem. After installing exiftool on my computer (using brew install exiftool), I was able to get the orientation and aspect ratio of the uploaded video and use that to determine whether or not to apply a transform to the video using ffmpeg. Here is my final uploader:
require 'carrierwave/processing/mime_types'
require 'rubygems'
require 'mini_exiftool'
class VideoPathUploader < CarrierWave::Uploader::Base
process :encode
def encode
video = MiniExiftool.new(@file.path)
orientation = video.rotation
if orientation == 90
# rotate video
Rails.logger.debug "portrait video"
aspect_ratio = video.imageheight.to_f / video.imagewidth.to_f
encode_video(:mp4, custom: "-vf transpose=1", aspect: aspect_ratio)
else
aspect_ratio = video.imagewidth.to_f / video.imageheight.to_f
encode_video(:mp4, resolution: :same, aspect: aspect_ratio)
end
instance_variable_set(:@content_type, "video/mp4")
:set_content_type_mp4
end
end
Also, in case it's helpful, I also had to install exiftool on Heroku to use it with my Rails app. I did this by using the following buildpack:
https://github.com/benalavi/buildpack-exiftool
After installing the buildpack, I still had to manually specify the path for exiftool (it's supposed to do this automatically when it installs the buildpack, but it didn't do it for me). I did this by manually setting the path:
heroku config:set PATH=*all_your_other_paths*:vendor/exiftool-9.40/bin