Jaco du Plessis

How To Fix Rotated Samsung Videos

video ffmpeg samsung

Recently I made a video using some clips a friend shot on his Samsung S8. About half of the videos were rotated by 90 degrees. I've never seen a problem like this before on any phone camera, so this is a pretty bad regression on Samsung's part. I cannot think how such a bug was not result by Quality Assurance testing. Anyway, here's how to fix those files using ffmpeg.

Using ffprobe, we can see that the problem is rotation metadata written to the file:

$ ffprobe skeef.mp4

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'skeef.mp4':
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2017-12-09T05:04:23.000000Z
    com.android.version: 7.0
  Duration: 00:00:32.17, start: 0.000000, bitrate: 17320 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 17056 kb/s, SAR 1:1 DAR 16:9, 30.02 fps, 30 tbr, 90k tbn, 180k tbc (default)
      rotate          : 90
      creation_time   : 2017-12-09T05:04:23.000000Z
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -90.00 degrees
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
      creation_time   : 2017-12-09T05:04:23.000000Z
      handler_name    : SoundHandle

Assuming all the affected file are in a single folder, run the following to create copies of every video in /tmp/ with the rotation metadata removed:

for f in *.mp4; do ffmpeg -i $f -metadata:s:v:0 rotate=0 -c:a copy -c:v copy /tmp/$f; done

Then just copy them to the working directory, overwriting the old videos:

cp /tmp/*.mp4 .

This is a much better solution than adding rotation filters in your video editor. You might be wondering: can't I just edit the metadata without creating new copies? The answer is no. Because we're removing metadata, the offset at which the video and audio start is also changed and therefore requires a new container.

Here is a link to the video I made:

Have a comment? Email me at jaco@jacoduplessis.co.za.