Can I set rotation field for a video stream with FFmpeg?
Asked Answered
M

1

42

I have a video file. I open it with MediaInfo utility and I can see a video stream in this file having attribute Rotation 90 (along with other attributes such as CodecID, bitrate etc).

Now I have another video file which does not have that attribute Rotation 90, it does not have the Rotation attribute at all.

Can I use ffmpeg.exe so that it produces output file with Rotation 90 attribute added and with no other changes? I don't really want to do any transform, just want to set the Rotation attribute.

I've tried the -metadata option to no avail.

Metamerism answered 11/3, 2013 at 9:30 Comment(0)
B
92

This works with recent FFmpeg:

ffmpeg -i input.mp4 -c copy -metadata:s:v:0 rotate=90 output.mp4

This will stream copy the bitstreams, so no encoding is performed. Only the metadata of the first video stream (v:0) is changed here and the player will show the video in a rotated way. (Not all players will support this.)

Additional notes:

  • If you want to "physically" rotate the video, you have to use the transpose filter. Filtering will require re-encoding, so you will have to remove -c copy.

  • If you omit -c copy, and want to encode instead of only re-muxing, then ffmpeg will automatically rotate the video if there is any existing rotate metadata. You can disable this behavior with -noautorotate.

Badalona answered 11/3, 2013 at 10:44 Comment(16)
I have compiled latest code of ffmpeg and there is no error in running this command but still video is not rotating.pls helpModifier
@MohitChauhan Please note that this does not actually rotate the video. It just sets the rotation flag, which causes some players to show it in a rotated way. If you have an issue with a command, please ask a question on Super User and show the command and its full console output.Badalona
@davor While this is an Apple-exclusive feature I only know QuickTime. But then again I don't use Windows or Linux so I cannot give an authoritative answer, sorry.Badalona
@Badalona This is very interesting. Any suggestion for players that actually support this? I am currently trying VLC and XBMC, and none seem to support this. Tried with MKV/MOV/MP4 containers. WMP12 and QuickTime do support this.Ringsmuth
@MohitChauhan To rotate the video 90 degrees clockwise including the dimensions, use the video filter -vf "transpose=1" (or other transpose value, e.g. https://mcmap.net/q/73275/-rotating-videos-with-ffmpeg-closed)Cathryncathy
I just tried this on an x265 video (converted from an x264 source of vertical video on an older iPhone) and it worked beautifully. Plays perfectly with VLC 2.2.1 on OS X.Lingwood
@slhck: I can rotate a video 90 degrees, but when I try to rotate the same videos again using 180 degrees then I the output video has the same orientation like the original movie. I can re-encode the video with -vf "vflip,hflip" but your approach is much quicker. Is it possible to rotate 180°?Salbu
@Salbu If you set -metadata:s:v:0 rotate=180 then the player should show the video rotated. If it doesn't, it is probably a problem with the player itself? Since it's just metadata, the original video will always be left as-is.Badalona
This was exactly what I needed! -metadata:s:v rotate=360 fixed the wrong metadata. It changed from Side data: displaymatrix: rotation of -90.00 degrees to displaymatrix: rotation of -0.00 degrees and is now correct.Persona
i am concatenating two videos using ffmpeg concat demux .The problem is that when i join the two videos ,the first video is maintaining its orientation but the second video's orientation is changed by 90 degrees .Somebody help me pleasePollux
@Pollux Please ask a new question, show the ffmpeg command, and include the full, uncut command line output from ffmpeg.Badalona
@Badalona actually i found that the video with landscape does not have rotation tag in meta data and so it is causing problem.Just wanted to ask that is there any way to join one video in landscape mode and the other in potrait with correct orientationPollux
@Pollux Use a -filter_complex filtergraph, then the rotate filter for one of the clips, and then the concat filter to join the original and the rotated one.Badalona
@Badalona thank you ....that worked .Actually im just curious if there is a way to check the orientation of the video ? i mean any commandPollux
-noautorotate saved me big time! without this, I had a hard time with output video to be all over the place.Blanka
For others coming here who aren't sure what the rotation values actually mean... rotate=90 rotates 90 degrees to the left (counterclockwise), rotate=270 will rotate a video 90 degrees to the right (clockwise)Upchurch

© 2022 - 2024 — McMap. All rights reserved.