unixffmpegcentosexif

How to keep the orientation number(exif) after converting from a video file to a image file by a ffmpeg command


The command below is working perfectly fine for my environment except it deletes the orientation number (EXIF) of the image file after being converted from a video file.

System info:

I'd like to know how to keep the orientation number(exif) of the image with the command line below(it also have to keep the original purpose of its functionality which is to convert a video to a image from one directory to another.). I'd appreciate if anyone could help me out.

for i in /path/to/inputs/*.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/$(basename "$i" .mp4).jpg"; done

Solution

  • tl;dr

    Update your ffmpeg and it will automatically rotate then you won't have to deal with exif tags.


    rotate side data

    MP4 does not contain exif data, but it can contain rotate side data. This is information a player can use to properly orient the video (not all players or devices support this), even if the video stream itself is not oriented as such. Videos from iPhones for example have this side data.

    ffmpeg auto-rotates

    When encoding, ffmpeg will by default automatically rotate the output depending on the rotate side data. So you may not need to set the exif orientation tag at all. You can disable this behavior by using the -noautorotate option. Note that your ffmpeg (version 2.2) is too old for this behavior and this option, so it does not automatically rotate. I recommend you download a recent ffmpeg version and move it to /usr/local/bin.

    So, given that non-ancient ffmpeg will automatically rotate do you even need to deal with the exif orientation tag? If you answer "yes" then see the sections below.


    viewing rotate side data in MP4 video

    You can use ffprobe to view the rotate side data of the input video:

    ffprobe -loglevel error -select_streams v:0 -show_entries side_data=rotation -of csv=p=0 input.mp4
    

    setting exif orientation tag in JPG image

    You can use exiftool to write the orientation exif tag to the JPG image:

    exiftool -Orientation=6 -n image.jpg
    

    Or a human readable form:

    exiftool -Orientation='Rotate 90 CW' image.jpg
    

    Numerical Orientation values are:

    1 = Horizontal (normal)
    2 = Mirror horizontal
    3 = Rotate 180
    4 = Mirror vertical
    5 = Mirror horizontal and rotate 270 CW
    6 = Rotate 90 CW
    7 = Mirror horizontal and rotate 90 CW
    8 = Rotate 270 CW
    

    As for implementing this in your bash script I think that's worth asking as a separate question.

    viewing exif orientation tag in JPG image

    You can use exiftool to view the orientation:

    $ exiftool -Orientation -S image.jpg
      Orientation: Rotate 90 CW
    $ exiftool -Orientation -n -S image.jpg
      Orientation: 6