Justin Bradley
Justin Bradley

Reputation: 241

FFmpeg C API set encoded frame’s rotation

Using the FFmpeg C API I have encoding and decoding a video working. However, the re-encoded video stream does not maintain the original video's orientation (rotation). So vertical videos have been flipped horizontal.

I’m not sure how to resolve this. Is there a metadata field that gets set? Using MediaInfo I see the original video has a metadata field ’Rotation : 90°’ and the new video does not. Or does each encoded frame need to be rotated vertically?

I’ve looked at the decode frame's side_data but it is empty.

for (j = 0; j < decoded_frame->nb_side_data; j++) {
    AVFrameSideData *sd = decoded_frame->side_data[j];
    if(sd->type == AV_FRAME_DATA_DISPLAYMATRIX) {
        LOGI("=> displaymatrix: rotation of %.2f degrees", av_display_rotation_get((int32_t *)sd->data));
     }
}

Upvotes: 0

Views: 1348

Answers (1)

Justin Bradley
Justin Bradley

Reputation: 241

I resolved this by adding 'Rotation' to the output video stream's metadata.

av_dict_copy(&output_stream->metadata, input_stream->metadata, AV_DICT_DONT_OVERWRITE);

There is a good explanation of the rotation metadata field here: Correct Smartphone Video Orientation and How To Rotate iOS and Android Videos with ffmpeg

Upvotes: 1

Related Questions