Change rotation flag in MP4 video (without losing metadata)

exiftool version 10.89 now has support to rotate mp4 videos without changing other metadata.

Usage:

exiftool -rotation=90 video.mp4

To get true lossless rotation, I couldn't find a solution, so I grabbed a hex editor (eg HxD) and analyzed the rotated video files.

  • open mp4 with hex editor and search for vide to find the metadata of the video track
  • some rows above (for my files mostly 9, sometimes 12) you should see trak...\tkhd
  • in between there should be an @ sign (HEX 40)
  • in the two rows before it the rotation matrix is stored
  • no rotation:

    00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 180°:

    FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 90° cw:

    00 00 00 00 00 01 00 00 00 00 00 00 FF FF 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    40 
    
  • 90° ccw:

    00 00 00 00 FF FF 00 00 00 00 00 00 00 01 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    40
    

Alter the file as you need it, and it should be rotated in players that support the rotation flag (most current players do).

In case your video contains stereo audio, this is obviously not switched, so in case you want the sound to match with video rotation (180°), you need to switch the left and right channels.