u1zd9q
Last Updated: October 19, 2018
·
2.739K
· trevorhinesley

Rotating .mov videos while converting to .mp4 using Panda (or FFMPEG)

Were you so technologically savvy that you filmed a video vertically using a device that doesn't auto-generate the proper Exif data to rotate the video, and now you're stuck with a sideways mess? Or you want to fix this issue for some other forward-thinking, aware individual?

Well, this snippet is for you.

I have been working on a site that uses Panda for its video uploading and encoding. When users would upload videos taken by their iPhone, they would rotate fine (because videos filmed using Apple products generally include the proper metadata that tells the player to rotate it whichever direction it was filmed), but videos uploaded from older/other devices didn't always offer the same functionality. Our site was using JW Player, which doesn't allow for video rotation inside it, so we needed to re-encode the videos to rotate them.

After tons of research, and basically being told by Panda that we would need to use a third-party encoding plug-in to rotate videos (no offense to Panda, they have great customer support and I highly recommend their service), one of our lead developers, Eric Willis, saw that Panda allows you to have encoding Profiles. These Profiles allow you to specify ffmpeg commands, so we used their h264 template and edited it a bit to include a rotation. For a 90-degree, clockwise rotation, I took code from the following SO post:

http://stackoverflow.com/a/4235705/981177

I then expanded on it a bit. We needed to take h264/aac videos and convert them to .mp4.

I took Panda's h264 preset:

Panda::Profile.create!({
  :stack => "corepack-2",
  :name => "h264.SD",
  :width => 480,
  :height => 320,
  :h264_crf => 23,
  :audio_bitrate => 128,
  :extname => ".mp4",
  :audio_sample_rate => 44100,
  :keyframe_interval => 250,
  :command => "ffmpeg -i $input_file$ -threads 0 -c:a libfaac -c:v libx264 -preset medium $video_quality$ $audio_bitrate$ $audio_sample_rate$ $keyframes$ $fps$ -y video_tmp_noqt.mp4\nqt-faststart video_tmp_noqt.mp4 $output_file$"
})

And modified it. By creating a Panda profile with the following as the :command parameter, you can rotate videos 90-degrees clockwise:

"ffmpeg -i $input_file$ -threads 0 -vf transpose=1 -c:a libfaac -c:v libx264 -preset medium -crf 23 $audio_bitrate$ $audio_sample_rate$ $keyframes$ $fps$ -y video_tmp_noqt.mp4\nqt-faststart video_tmp_noqt.mp4 $output_file$"

To rotate videos 90-degrees counter-clockwise:

"ffmpeg -i $input_file$ -threads 0 -vf transpose=2 -c:a libfaac -c:v libx264 -preset medium -crf 23 $audio_bitrate$ $audio_sample_rate$ $keyframes$ $fps$ -y video_tmp_noqt.mp4\nqt-faststart video_tmp_noqt.mp4 $output_file$"

Also, transpose can be set to 0, 1, 2, 3, depending on what kind of rotation you want. The functions of each are described in detail here:

http://phayz.wordpress.com/2011/10/28/howto-rotate-a-video-using-ffmpeg/

One more thing--if you can get it to work, -crf 23 can be replaced with $video_quality$ (which is supposed to be replaced with either video_bitrate or h264_crf depending on whether or not one of those parameters has a value), but Panda wouldn't read my h264_crf attribute using this, so it was leaving $video_quality$ blank and would break the encoding process. I notified them and they are looking into the issue.

Thanks again to rwilliams on StackOverflow, phayz, and Panda for the preset, I just wanted to expand on them a bit to show people how to do video rotation using Panda if you needed.

Hope this helps someone else!

-T

3 Responses
Add your response

16130

Though there are many video rotator which can rotate MOV, it change the video quality.

Luckily, I find iDealshare VideoGo, it can rotate MOV video 90 degrees clockwise, 180 degrees or 270 degrees without changing the video quality.

It support rotate MOV as well as keep the same video format or convert MOV to other video format.

Just google Step by Step Guide on How to Rotate MOV withi iDealshare VideoGo, you will find a detailed guide.

It even can rotate MP4, AVI, MKV, MPG, WMV and etc

over 1 year ago ·
16131
31017

another option:I use Avdshare Video Converter to rotate MP4, WMV, AVCHD, ASF, MPG, 3GP, etc as well as rotate MOV. Follow the steps:https://www.avdshare.com/rotate-mov-file-90-degrees-180-degrees-or-270-degrees

5 months ago ·