1

I have a large video file (2.2 GB) and one part of the video is not oriented in the correct way: the first part is fine, but the second part is rotated by 180 degrees (480*1066 pixels). To solve the problem I used FFmpeg.

  1. I used this command to split the video file into two parts

    ffmpeg -i input.mp4 -t 00:14:36 -c copy 1.mp4 -ss 00:14:36 -c copy 2.mp4

  2. I changed the metadata of the second video file so that it matches the orientation of the first one when read:

    ffmpeg -i 2.mp4 metadata:v:s:0 "rotate=180" -c copy 20.mp4

  3. I merged the 2 files together with this command:

    ffmpeg -f concat -i merge.txt -c copy OUTPUT-MERGED.mkv

Where merge.txt lists the two files, 1.mp4 and 20.mp4.

The problem is, this does not work ; changing the metadata of the second video file does not seem to be taken into account when ffmpeg merges the two files at the end, the result is strictly the same as the input video file.

Is there a way to do what I want without encoding one of the two files?

The input file has these infos:

Metadata:
  rotate          : 90
  creation_time   : 2021-05-07T09:26:43.000000Z
  handler_name    : VideoHandle
Side data:
  displaymatrix: rotation of -90.00 degrees

And the two separate files have these infos:

1.mp4

Metadata:
  rotate          : 90
  handler_name    : VideoHandle
Side data:
  displaymatrix: rotation of -90.00 degrees

20.mp4

Metadata:
      rotate          : 180
      handler_name    : VideoHandle
    Side data:
      displaymatrix: rotation of -180.00 degrees

2 Answers2

3

That is not the way to rotate part of the file. Metadata apply to the whole file. If you split the file, then change metadata of the second part, these metadata will be lost if you concatenate that part back to the first part.

For rotating part of the video, your only option is to reencode that part. You will need to rotate the image, and subsequently make it fit into the frame size of the first part, by inserting black bars or cropping the rotated image. The result must be reencoded using the same codex as the first part to a new videofile. That new videofile can then be appended to the first part.

vanadium
  • 82,909
  • 6
  • 116
  • 186
  • Hello, thanks for your answer. Okay, the metadata will be lost after the concatenation process, got it. The two videos have the same dimensions, so there won't be black bars. Does this fact change anything or should I still reencode the second file? – Yonah Grondin May 17 '21 at 19:14
  • As the metadata get lost, you know the answer. – vanadium May 18 '21 at 07:55
0

You can do it with a player such as ffplay or mpv:

ffplay -vf "rotate=180*PI/180:enable='between(t,3,6)'" input.mp4
  • Since it is just rotating upon playback this rotates the video without re-encoding. No output file is made.

  • Unit is in radians. Change the first instance of 180 to whatever rotation degree you want. For 180 degrees you can just use rotate=PI.

  • See docs about the rotate filter and enable option for more info.

llogan
  • 11,518
  • 43
  • 54