How to Rotate Video Using Gyro Metadata in FFmpeg

This article explains how to dynamically rotate a video using its embedded gyroscopic metadata with FFmpeg. You will learn how FFmpeg handles automatic orientation, how to apply dynamic rotation using external sensor data, and how to utilize advanced open-source plugins for frame-by-frame gyro stabilization and rotation.

Understanding Gyro Metadata and FFmpeg

When recording video on smartphones, drones, or action cameras, the device’s internal gyroscope records orientation data. This metadata can be static (a single flag indicating the camera was held sideways) or dynamic (continuous frame-by-frame sensor readings tracking movement).

Depending on whether your metadata is static or dynamic, you will need to use different FFmpeg approaches.

Method 1: Automatic Static Rotation (Default Behavior)

If your camera writes a static orientation tag (such as 90, 180, or 270 degrees) based on the initial gyroscope reading, modern versions of FFmpeg will automatically rotate the video during playback or re-encoding.

To ensure this automatic rotation is applied and baked into the video stream, use the following command:

ffmpeg -i input.mp4 -vf "autorotate" output.mp4

If you want to disable this automatic behavior and keep the original raw pixel orientation, use the -nosources flag or disable the video filter:

ffmpeg -noautorotate -i input.mp4 -c copy output.mp4

Method 2: Dynamic Frame-by-Frame Rotation Using the Rotate Filter

For true dynamic rotation where the angle changes over time (e.g., correcting a horizon during a flight), you must feed dynamic values into FFmpeg’s rotate video filter.

FFmpeg’s rotate filter accepts expressions. If you have extracted your gyroscope roll angles into a time-based math formula, you can pass it directly to the filter:

ffmpeg -i input.mp4 -vf "rotate='t*PI/10'" output.mp4

In this example, the video dynamically rotates continuously over time (t represents the timestamp in seconds).

For actual gyroscope logs, you must map the time-stamped angles (in radians) using an expression. The rotate filter parameters include: * a / angle: The angle in radians by which to rotate the video clockwise. * ow and oh: Output width and height. Dynamic rotation can crop corners; you can use ow='hypot(iw,ih)' and oh='out_w' to prevent cropping by expanding the canvas.

Example with canvas compensation:

ffmpeg -i input.mp4 -vf "rotate=angle='sin(t)':ow='hypot(iw,ih)':oh='out_w':fillcolor=black" output.mp4

Method 3: Advanced Dynamic Gyro Rotation via Gyroflow

Standard FFmpeg cannot natively parse complex continuous binary gyro data tracks (like GoPro GPMF or Sony RTMD metadata) directly into the rotate filter without third-party extraction.

To achieve professional, dynamic, gyro-based stabilization and rotation, the open-source tool Gyroflow provides an FFmpeg video filter plugin (vf_gyroflow).

Step 1: Install the Gyroflow FFmpeg Plugin

You must use an FFmpeg build compiled with the --enable-libgyroflow flag.

Step 2: Run the Dynamic Rotation Command

Once installed, you can pass the video and its gyroscope data directly through FFmpeg. The filter automatically parses the camera telemetry and dynamically rotates/stabilizes the frames:

ffmpeg -i input.mp4 -vf "gyroflow=gyro_file=telemetry.gcsv:smoothing=1.0" output.mp4

If the gyro metadata is embedded directly inside the video container (like in GoPro Creator Edition files), you do not need to specify an external gyro_file:

ffmpeg -i input.mp4 -vf "gyroflow" output.mp4

This method provides the most accurate dynamic rotation as it utilizes the exact sensor sampling rate of your camera’s hardware gyroscope.