TL;DR
Portrait phone videos usually store landscape frames plus a display matrix that says "rotate on playback". We'll reproduce the three ways pipelines break it (wrong orientation from
ffprobe, a landscape ladder that squashes portrait video, and TS stream copies that drop the matrix), then fix each one with a small Python helper and two FFmpeg rules. Tested on FFmpeg 6.1.1, hls.js 1.7.4, Chromium 158.
The symptoms 🎥
Users report one of these after uploading a video recorded with the phone held upright:
- The video plays sideways in your player.
- The video plays upright but squashed into a widescreen box.
- Thumbnails or the player container are the wrong shape.
The original file plays fine everywhere. Something in your pipeline dropped or misread the rotation.
1. Make a test file with a rotation flag
You don't need a phone to reproduce this. Generate a landscape clip, then stamp a rotation onto it with a stream copy so only the metadata changes:
# landscape-coded frames, like a phone sensor writes them ffmpeg -f lavfi -i "testsrc2=size=1920x1080:rate=30:duration=4" \ -f lavfi -i "sine=frequency=440:duration=4" \ -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest raw.mp4 # add a display matrix, no re-encode ffmpeg -display_rotation:v:0 -90 -i raw.mp4 -c copy phone.mp4 Now probe it:
ffprobe -v error -select_streams v:0 \ -show_entries stream=width,height:stream_side_data=rotation \ -of compact=p=0 phone.mp4 width=1920|height=1080|side_datum/display_matrix:rotation=-90 That's the whole bug in one line. width and height are the coded size. The display size is 1080x1920, and you only get it by applying the rotation. Chromium plays phone.mp4 upright and reports videoWidth: 1080, videoHeight: 1920.
⚠️ Note: older scripts read the angle from
tags.rotate. On ffprobe 6.1.1 that tag is empty for this file; the angle lives in side data. If your code checks the tag, it thinks nothing is rotated.
2. Reproduce the breakage
Here's what I ran, and what came out:
# A) plain transcode (FFmpeg autorotates by default) ffmpeg -i phone.mp4 -c:v libx264 -crf 23 -c:a copy transcoded.mp4 # B) a "720p" rung written for landscape sources ffmpeg -i phone.mp4 -vf scale=1280:720 -c:v libx264 -crf 23 -an rung720.mp4 # C) the aspect-preserving half fix ffmpeg -i phone.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 -an rung720b.mp4 # D) passthrough into classic TS HLS ffmpeg -i phone.mp4 -c copy -f hls -hls_time 2 -hls_playlist_type vod hls_ts/index.m3u8 # E) passthrough into fMP4 HLS ffmpeg -i phone.mp4 -c copy -f hls -hls_time 2 -hls_segment_type fmp4 \ -hls_playlist_type vod hls_fmp4/index.m3u8 | Output | ffprobe result | In Chromium 158 |
|---|---|---|
A transcoded.mp4 | 1080x1920, no side data | ✅ upright (rotation baked into pixels) |
B rung720.mp4 | 1280x720 | ❌ portrait content stretched into a landscape box |
C rung720b.mp4 | 406x720 | ⚠️ correct shape, but a tiny "720p" rung |
D hls_ts/index0.ts | 1920x1080, no side data | ❌ sideways (via hls.js 1.7.4) |
E hls_fmp4/init.mp4 | 1920x1080, rotation -90 | ✅ upright (via hls.js 1.7.4) |
Two things to notice:
- FFmpeg autorotates before your filters. In B,
scalereceived upright 1080x1920 frames and stretched them to 1280x720. That's the squashed-person bug. - MPEG-TS can't carry the matrix. A stream copy into TS throws the rotation away. fMP4 keeps it, and Chromium's MSE path honored it in my test.
3. A helper that returns display dimensions
Every piece of code that decides layout, thumbnails or ladders should go through one function that applies the rotation:
# probe_dims.py import json, subprocess, sys def display_dims(path: str) -> dict: out = subprocess.run( ["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height:stream_side_data=rotation", "-of", "json", path], capture_output=True, text=True, check=True).stdout s = json.loads(out)["streams"][0] rotation = 0 for sd in s.get("side_data_list", []): if "rotation" in sd: rotation = int(sd["rotation"]) w, h = s["width"], s["height"] if rotation % 180 != 0: # 90 or 270 (ffprobe reports -90, 90, 180, -180) w, h = h, w return {"coded": f'{s["width"]}x{s["height"]}', "rotation": rotation, "display_width": w, "display_height": h, "orientation": "portrait" if h > w else "landscape" if w > h else "square"} if __name__ == "__main__": for p in sys.argv[1:]: print(p, display_dims(p)) $ python3 probe_dims.py phone.mp4 transcoded.mp4 raw.mp4 phone.mp4 {'coded': '1920x1080', 'rotation': -90, 'display_width': 1080, 'display_height': 1920, 'orientation': 'portrait'} transcoded.mp4 {'coded': '1080x1920', 'rotation': 0, 'display_width': 1080, 'display_height': 1920, 'orientation': 'portrait'} raw.mp4 {'coded': '1920x1080', 'rotation': 0, 'display_width': 1920, 'display_height': 1080, 'orientation': 'landscape'} Same display answer for the original and the transcode, which is the point.
💡 Tip: once this exists, grep your codebase for anything else reading
widthfrom ffprobe output and route it throughdisplay_dims.
4. An orientation-aware ladder 🛠️
Define rungs by the short side. A 720p rung for a portrait video is 720 wide, not 720 tall.
# ladder.py from probe_dims import display_dims RUNGS = [1080, 720, 480] # short-side sizes, not "height" def scale_filters(path: str) -> list[str]: d = display_dims(path) short = min(d["display_width"], d["display_height"]) filters = [] for r in RUNGS: if r > short: continue # never upscale if d["orientation"] == "portrait": filters.append(f"scale={r}:-2") # fix the width, height follows else: filters.append(f"scale=-2:{r}") # fix the height, width follows return filters if __name__ == "__main__": import sys for p in sys.argv[1:]: print(p, scale_filters(p)) $ python3 ladder.py phone.mp4 raw.mp4 phone.mp4 ['scale=1080:-2', 'scale=720:-2', 'scale=480:-2'] raw.mp4 ['scale=-2:1080', 'scale=-2:720', 'scale=-2:480'] Check the fixed rung:
$ ffmpeg -i phone.mp4 -vf scale=720:-2 -c:v libx264 -crf 23 -an rung720fixed.mp4 $ python3 probe_dims.py rung720fixed.mp4 rung720fixed.mp4 {'coded': '720x1280', 'rotation': 0, 'display_width': 720, 'display_height': 1280, 'orientation': 'portrait'} Because FFmpeg autorotates before scale, the filter strings are written against the display orientation. That's why the helper matters: the ladder logic and FFmpeg now agree on which way is up.
5. Keep rotated files off the passthrough path
If you skip transcoding for uploads that are "already fine", add rotation to the eligibility check:
# passthrough.py from probe_dims import display_dims def can_passthrough(path: str) -> bool: d = display_dims(path) if d["rotation"] != 0: return False # TS drops the matrix; fMP4 depends on every player honoring it # ...your existing codec / bitrate / keyframe checks go here return True Re-encoding puts the rotation into the pixels, and pixels survive every container:
$ ffmpeg -i phone.mp4 -c:v libx264 -crf 23 -c:a copy \ -f hls -hls_time 2 -hls_playlist_type vod hls_ts_fixed/index.m3u8 $ python3 probe_dims.py hls_ts_fixed/index0.ts hls_ts_fixed/index0.ts {'coded': '1080x1920', 'rotation': 0, 'display_width': 1080, 'display_height': 1920, 'orientation': 'portrait'} 6. About -noautorotate
-noautorotate turns off the automatic transpose. In my run, a re-encode with it kept landscape frames and carried rotation=-90 into the new MP4:
$ ffmpeg -noautorotate -i phone.mp4 -c:v libx264 -crf 23 -c:a copy noauto.mp4 $ ffprobe -v error -select_streams v:0 -show_entries stream=width,height:stream_side_data=rotation -of compact=p=0 noauto.mp4 width=1920|height=1080|side_datum/display_matrix:rotation=-90 Chromium still played it upright. Handy for editing workflows that want the original orientation, but for delivery I'd rather apply the rotation once and stop depending on metadata.
Things to know
- What I tested: FFmpeg 6.1.1 (Ubuntu build), hls.js 1.7.4, headless Chromium 158 on Linux, with a synthetic H.264 clip whose rotation I set with
-display_rotation. - What I didn't: Safari, iOS and Android native players, HEVC phone files, real phone recordings, newer FFmpeg majors. Rerun section 2 on your own stack; it takes a minute.
- Edge cases exist upstream. FFmpeg's tracker has open tickets about rotation metadata passing through specific filter graphs and inputs (for example #11649). Probe your outputs, don't assume.
What's next
- Run
probe_dims.pyover the last few hundred phone uploads and their renditions. Any output with a non-zero rotation in a TS segment is a sideways video waiting for a support ticket. - Mirrored selfie video uses the same matrix (FFmpeg has
-display_hflipand-display_vfliptoo). If your app records front-camera video, test that path next.