video captured from iphone gets rotated when converted to .mp4 using ffmpeg

后端 未结 10 1755
名媛妹妹
名媛妹妹 2020-12-01 02:31

When I try to upload videos captured from my iPhone in my app, the server performs a conversion from .mov to .mp4 so that it can be played in other platforms. However the pr

相关标签:
10条回答
  • 2020-12-01 02:42

    For sake of completeness, the reason this is happening is that iPhones only actually capture video in one fixed orientation. The measured orientation is then recorded in Apple-specific metadata.

    The effect is that Quicktime Player reads the metadata and rotates the video to the correct orientation during playback, but other software (e.g., VLC) does not and shows it as oriented in the actual codec data.

    This is why rotate=90 (or vflip, or transpose, or etc.) will work for some people, but not others. Depending on how the camera is held during recording, the rotation necessary could be 90, 180, or even 270 degrees. Without reading the metadata, you're just guessing at how much rotation is necessary and the change that fixes one video will fail for another.

    0 讨论(0)
  • 2020-12-01 02:47

    For including into web pages my portrait-format videos from iPhone, I just discovered the following recipe for getting .mp4 files in portrait display.

    Step 1: In QuickTime Player, Export your file to 480p (I assume that 720p or 1080p would work as well). You get a .mov file again.

    Step 2: Take the new file in QT Player, and export to “iPad, iPhone…”. You get a .m4v file.

    Step 3: I’m using Miro Video Converter, but probably any readily-available converter at all will work, to get your .mp4 file.

    Works like a (long-winded) charm.

    0 讨论(0)
  • 2020-12-01 02:47

    Although the topic is old. Hope this will help some one:

    Get ffmpeg latest version : https://www.ffmpeg.org/download.html

    The command that worked for me (to flip 180 degrees):

    ffmpeg -noautorotate -i input.mp4 -filter:v "rotate=PI" output.mp4
    

    When the degrees are determined by -filter:v "PI/180*degrees"

    for example -filter:v "45*PI/180" for 45 degrees

    A nice explanation is here https://superuser.com/questions/578321/how-to-rotate-a-video-180-with-ffmpeg

    0 讨论(0)
  • 2020-12-01 02:48

    Use the vflip filter

    ffmpeg -i input.mov -vf "vflip" output.mp4
    

    Rotate did not work for me and transpose=1 was rotating 90 degrees

    0 讨论(0)
  • 2020-12-01 02:50

    So - I too ran into this issue, and here my $0.02 on it:

    1.) some videos DO have Orientation/Rotation metadata, some don't: MTS (sony AVHCD) or the AVIs I have - DO NOT have an orientation tag. MOVs and MP4s (ipad/iphone or samsung galaxy note2) DO HAVE it.

    you can check the setting via 'exiftool -Rotation file'.
    My videos often have 90 or 180 as the rotation.
    

    2.) ffmpeg - regardless of the man-page with the metadata-tag, just doesn't EVER seem to set it in the output file. - the rotation-tag is ALWAYS '0'. it correctly reports it in the output - but it's never set right to be reported by exiftool. - But hey - at least it's there and always 0.

    3.) rotation angles: if you want rotate +/- 90: transpose=1 for clockwise 90, 2 ccw now if you need 180 degree - just add this filter TWICE. remember - it's a filter-chain you specify. :-) - see further down.

    4.) rotate then scale: this is tricky - because you quickly get into MP4 output format violations. Let's say you have a 1920x1080 MOV. rotate by 90 gives 1080x1920 then we rescale to -1:720 -> 1080*(720/1920) = 405 horiz And 405 horizontal is NOT divisable by 2 - ERROR. fix this manually. FIXING THIS automatically - requires a bit of shell-script work.

    5.) scale then rotate: you could do it this way - but then you end up with 720x1280. yuck. But the filter-example here would be: "-vf yadif=1,scale=-1:720,transpose=1" It's just not what I want - but could work quite OK.

    Putting it all together: - NOTE - 'intentionally WRONG Rotation-tag', just to demonstrate - it won't show up AT ALL in the output ! This will take the input - and rotate it by 180 degree, THEN RESCALE IT - resetting the rotation-tag. - typically iphone/ipad2 can create 180deg rotated material. you just can leave '-metadata Rotation=x' out the line...

    /usr/bin/ffmpeg -i input-movie.mov -timestamp 2012-06-23 08:58:10 -map_metadata 0:0 -metadata Rotation=270 -sws_flags lanczos -vcodec libx264 -x264opts me=umh -b 2600k -vf yadif=1,transpose=1,transpose=1,scale=1280:720 -f mp4 -y output-movie.MP4

    I have multiple devices - like a settop box, ipad2, note2, and I convert ALL my input-material (regardless whether it's mp4,mov,MTS,AVI) to 720p mp4, and till now ALL the resulting videos play correct (orientation,sound) on every dev.

    Hope it helps.

    0 讨论(0)
  • 2020-12-01 02:55

    What you can also do is remove the QuickTime specific metadata when rotate the .mov. This will make sure that the video is rotated the same way in VLC and QuickTime

    ffmpeg -i in.mov -vf "transpose=1" -metadata:s:v:0 rotate=0 out.mov
    

    Here's the documentation on the -metadata option (from http://ffmpeg.org/ffmpeg.html):

    -metadata[:metadata_specifier] key=value (output,per-metadata)
    

    Set a metadata key/value pair.

    An optional metadata_specifier may be given to set metadata on streams or chapters. See -map_metadata documentation for details.

    This option overrides metadata set with -map_metadata. It is also possible to delete metadata by using an empty value.

    For example, for setting the title in the output file:

     ffmpeg -i in.avi -metadata title="my title" out.flv 
    

    To set the language of the first audio stream:

     ffmpeg -i INPUT -metadata:s:a:1 language=eng OUTPUT
    
    0 讨论(0)
提交回复
热议问题