Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video Orientation Incorrect #21

Closed
ttseng opened this issue Jan 21, 2014 · 5 comments
Closed

Video Orientation Incorrect #21

ttseng opened this issue Jan 21, 2014 · 5 comments

Comments

@ttseng
Copy link

ttseng commented Jan 21, 2014

I have a video that is 1080 x 1920. When it gets uploaded via carrierwave-video, the video appears to be rotated 90 degrees counter-clockwise. How can I ensure that the video is not rotated? What types of methods can I use to detect the video's dimensions when the user uploads the file?

This is only an issue when uploading portrait videos.

@ttseng
Copy link
Author

ttseng commented Jan 23, 2014

I found that you can manually rotate images with the ffmpeg command "-vf transpose=1"

I'm trying to detect the orientation of the video within my uploader model to determine whether or not the video needs to be rotated. I tried the dynamic configuration you outlined in the documentation, but my method "encode" is not being called until after the video is transcoded. How can I fix this?

This is what I have:

process :encode

def encode
Rails.logger.debug "in encode"
video = FFMPEG::Movie.new(@file.path)
video_width = video.width
video_height = video.height
Rails.logger.debug "video widthxheight: #{video.width}x#{video.height}"
aspect_ratio = video_width/video_height
if video_height > video_width
# rotate video
Rails.logger.debug "portrait video"
encode_video(:mp4, custom: "-vf transpose=1", aspect: aspect_ratio)
else
encode_video(:mp4, aspect: aspect_ratio)
end
instance_variable_set(:@content_type, "video/mp4")
:set_content_type_mp4
end

In my logs, I get:

Running transcoding...
ffmpeg -y -i /tmp/1390509835-32689-5471/VID_01-16-2014-17-34-35.mp4 -vcodec libx264 -acodec libfaac -s 640x360 -qscale 0 -preset slow -g 30 -aspect 1 /tmp/1390509835-32689-5471/tmpfile.mp4

Transcoding of /tmp/1390509835-32689-5471/VID_01-16-2014-17-34-35.mp4 to /tmp/1390509835-32689-5471/tmpfile.mp4 succeeded

Started POST "/videos" for 127.0.0.1 at 2014-01-23 15:43:55 -0500
Processing by VideosController#create as JS
in encode
video widthxheight: 1920x1080
...

@rheaton
Copy link
Owner

rheaton commented Feb 28, 2014

Would it be possible to use the before_transcode callback to do this?

@ttseng
Copy link
Author

ttseng commented Feb 28, 2014

Didn't get a chance to update this with my fix! I ended up using the MiniExiftool gem to get the rotation of the video. Here's the full uploader:

require 'carrierwave/processing/mime_types'
require 'rubygems'
require 'mini_exiftool'

class VideoPathUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
include CarrierWave::Video::Thumbnailer
include CarrierWave::MimeTypes

process :encode

def encode
video = MiniExiftool.new(@file.path)
orientation = video.rotation
model.rotation = orientation
Rails.logger.debug("orientation: #{orientation}")
Rails.logger.debug("video wxh #{video.imagewidth} #{video.imageheight}")

if orientation == 90 && video.imagewidth.to_f > video.imageheight.to_f
  Rails.logger.debug("rotating video")
  aspect_ratio = video.imageheight.to_f / video.imagewidth.to_f 
  encode_video(:mp4, custom: "-vf transpose=1", resolution: :same, 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

@ttseng ttseng closed this as completed Feb 28, 2014
@rheaton
Copy link
Owner

rheaton commented Feb 28, 2014

Awesome! Thank you.

@damujay
Copy link

damujay commented Aug 24, 2020

I am facing the issue with a portrait video.The thumbnail is rotating right. I am using the above code.Can you suggest me on that.

when i am removing custom attribute working fine but image is a blur and stretch more.

please tell me what type of custom attribut need to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants