Rotate mp4 videos by only writing to xmp

Started by Anxious-Bluebird2318, September 03, 2024, 07:02:27 AM

Previous topic - Next topic

Anxious-Bluebird2318

Hi there,

I am wondering if there is a way to rotate mp4 videos only by touching an xmp file. Based on https://exiftool.org/TagNames/Composite.html it looks like the Rotation field is writable, but I couldn't get it to work based on https://exiftool.org/forum/index.php?topic=11610.0. I haven't found a recent thread.

I am using exiftool 12.77

Here's what I have so far, however, the `rotation` command does nothing in the resulting xmp file:

exiftool -ext mp4 -srcfile %d%f.%e.xmp -Rotation=180 -overwrite_original .
Essentially, I am trying to append a 180 degree rotation to my mp4 videos which may or may not have an existing xmp file.

Is this possible with exiftool? I was hoping to not overwrite my original media (with something like ffmpeg to rotate it) and only use an xmp file.

Cheers for the tool, it's amazing!

StarGeek

Rotation is a Composite tag, which means that it is created on the fly by combining (e.g. compositing) multiple different underlying tags. It does not actually exist in the file.

In this case, checking the Composite tag page, Rotation is created by combining the QuickTime:MatrixStructure and QuickTime:HandlerType tag. Both of which are Quicktime tags.

XMP tags/sidecar files can only hold XMP tags, most of which are on the XMP tags page, but some do appear on other pages (PLUS xmp tags page, Microsoft XMP Tags page, etc). If the description section for a tag group doesn't contain something like "These tags belong to the ExifTool XMP-* family 1 group", it is not an XMP tag and cannot be placed in XMP.

Rotating the video is going to require rewriting the file to adjust appropriate tags, and these are the only ones that a video player is going to read, as very few (none that I can find) will even see XMP data in a video.

As long as the file contains the QuickTime:MatrixStructure and QuickTime:HandlerType tags, you do not need to use ffmpeg. You can write the Rotation with exiftool
exiftool -Rotation=180 /path/to/files/

It is possible for the video not to have the above two tags. In that case, exiftool will not work, as it cannot create these tags. You will need to use ffmpeg, but you do not need to re-encode it. You can copy the streams and use the -metadata option.

A quick search gives this command
ffmpeg -i input-video.mp4 -metadata:s:v rotate="180" -codec copy output-video.mp4

This would work for most simple videos, i.e. one video stream, one audio stream. For a more complicated file that includes more than one audio stream and/or also has subtitles, this would probably work (this is my standard ffmpeg remux command plus the above option)
ffmpeg -hide_banner -i input.mkv -c copy -map 0 -map_metadata 0 -metadata:s:v rotate="180" output.mkv

Of course, test these out before running it in batch.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Anxious-Bluebird2318

Thanks for your reply and explaining how the Composite tags work!

My video player should respect Rotation written, however, I have not gotten to the point where I can actually even play the video yet (as I'm struggling to write the exif data).

Quote from: StarGeek on September 03, 2024, 10:01:10 AMAs long as the file contains the QuickTime:MatrixStructure and QuickTime:HandlerType tags, you do not need to use ffmpeg. You can write the Rotation with exiftool
exiftool -Rotation=180 /path/to/files/

I ran the following on my video files:

exiftool -QuickTime:all -G0 -a -s myfile.MP4
and I get the following parts in the output:

...
[QuickTime]     MatrixStructure                 : 1 0 0 0 1 0 0 0 1
...
[QuickTime]     HandlerType                     : Video Track


so it should be theoretically possible to write a Rotation composite xmp for this file?

I can do this by overwriting the file, as running:

$ exiftool -Rotation=180 myfile.MP4
    1 image files updated
$ exiftool -Rotation myfile.MP4
Rotation                        : 180

shows that it rotated it, however, I cannot get this to work by writing to some XMP. Is this possible?

Trying the original command I gave in my post yields this:

exiftool -ext mp4 -srcfile %d%f.%e.xmp -Rotation=180 -overwrite_original myfile.MP4
Error: Nothing to write - myfile.MP4.xmp
    0 image files updated
    1 files weren't updated due to errors


Based on what you are describing, if I was able to write to an xmp, I should expect QuickTime:MatrixStructure and QuickTime:HandlerType to potentially be updated?

StarGeek

Quote from: Anxious-Bluebird2318 on September 04, 2024, 12:24:01 AM(as I'm struggling to write the exif data).

Minor nitpick, EXIF is a specific type of metadata, not a general term. All EXIF data is metadata, but not all metadata is EXIF data. It's an important detail when dealing with the specifics of metadata.

And actual EXIF data is non-standard in a video file. Camera companies still shove the data into the files, and each one does so in different ways. This is why exiftool can't add EXIF data to a video file and is very limited when it comes to editing EXIF data in a video.

Quoteso it should be theoretically possible to write a Rotation composite xmp for this file?

No, because these tags are Quicktime tags, not XMP tags. Only XMP tags can be written to an XMP sidecar or embedded as XMP.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Anxious-Bluebird2318

Quote from: StarGeek on September 04, 2024, 10:34:38 AMNo, because these tags are Quicktime tags, not XMP tags. Only XMP tags can be written to an XMP sidecar or embedded as XMP.

My apologies for the confusing terminology I am using. I meant to say, it should be theoretically possible to write the relevant Quicktime tags to an XMP sidecar to act as the Rotation composition -- but based on what you have messaged I am now thinking this is not possible, and I will have to use -rotation on the original videos.

In other words, the only way to achieve some sort of rotation inside an XMP sidecar for mp4 videos is to directly write to the file with -Rotation=180. I am a little confused as to why this is the case (I can't find a reference to MatrixStructure anywhere in the XMP tags, PLUS xmp tags page, or Microsoft XMP Tags page you referenced, which is why I presume it's not possible to write to these tags as XMP data) -- but nonetheless I appreciate your comments. At the same time, based on https://exiftool.org/TagNames/QuickTime.html I can't quite see why it's not possible to write the equivalent of a Rotation to an XMP sidecar.

StarGeek

Quote from: Anxious-Bluebird2318 on September 05, 2024, 04:02:03 AMMy apologies for the confusing terminology I am using. I meant to say, it should be theoretically possible to write the relevant Quicktime tags to an XMP sidecar to act as the Rotation composition -- but based on what you have messaged I am now thinking this is not possible, and I will have to use -rotation on the original videos.

It's absolutely possible to create an custom XMP Rotation tag. XMP is very flexible that way. But it would be a custom tag, and there isn't a program in the world that has been coded to read that and rotate the video. Custom tags are exactly that, custom.

QuoteIn other words, the only way to achieve some sort of rotation inside an XMP sidecar for mp4 videos is to directly write to the file with -Rotation=180.

No, it's not possible to put a rotation value into an XMP sidecar unless you create your own custom tag. Editing Rotation writes data to the underlying Quicktime tags.

QuoteI am a little confused as to why this is the case (I can't find a reference to MatrixStructure anywhere in the XMP tags, PLUS xmp tags page, or Microsoft XMP Tags page you referenced, which is why I presume it's not possible to write to these tags as XMP data)

As I said, MatrixStructure is a QuickTime tag, not an XMP one. Names are not automatically transferable between groups. The only place in on the tag names pages is on the Quicktime tags page.

QuoteAt the same time, based on https://exiftool.org/TagNames/QuickTime.html I can't quite see why it's not possible to write the equivalent of a Rotation to an XMP sidecar.

Because Rotation doesn't exist in the XMP group.

I'm not sure how I can explain it better. Each tag group is separate and independent of each other. It's like going to a McDonalds and asking to buy a chainsaw. It's like Apples (XMP) and Oranges (Quicktime). They're both fruits (Metadata), but they are not the same or interchangeable.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).