更改 MP4 视频中的旋转标志(不丢失元数据)

更改 MP4 视频中的旋转标志(不丢失元数据)

我只想更改 .mp4 视频文件中的旋转标志。存在许多类似的问题,大多数答案建议使用

ffmpeg -i input.mp4 -map_metadata 0 -metadata:s:v rotate=90 -codec copy output.mp4

我对这些解决方案的问题是,尽管它复制了大部分元数据,但有些部分会丢失,而且文件更小 700kb。附件是来自 ffprobe 和 exifer 的前后元数据。

另外一个奇怪的发现是 ffmpeg rotate=90 命令进行了逆时针旋转,在 ffprobe 中,在元数据中为 (cw) 270°,但在 displaymatrix 侧数据中为 (ccw)90°。

= equal attributes
* altered attributes
- removed attributes
+ added attributes

ffprobe 输入.mp4

*Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '.\input.mp4':
=  Metadata:
*    major_brand     : avc1
*    minor_version   : 0
*    compatible_brands: avc1isom
=    creation_time   : 2016-10-29T12:10:41.000000Z
-    firmware        : HD3.11.03.03
*  Duration: 00:00:06.01, start: 0.000000, bitrate: 32072 kb/s
=    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt709), 1920x1440 [SAR 1:1 DAR 4:3], 30945 kb/s, 47.95 fps, 47.95 tbr, 720k tbn, 95.90 tbc (default)
=    Metadata:
=      creation_time   : 2016-10-29T12:10:41.000000Z
*      handler_name    :  GoPro AVC
-      encoder         : GoPro AVC encoder
-    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
-    Metadata:
-      creation_time   : 2016-10-29T12:10:41.000000Z
-      handler_name    :  GoPro AAC

ffprobe 输出.mp4

*Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '.\output.mp4':
=  Metadata:
*    major_brand     : isom
*    minor_version   : 512
*    compatible_brands: isomiso2avc1mp41
=    creation_time   : 2016-10-29T12:10:41.000000Z
+    encoder         : Lavf58.10.100
*  Duration: 00:00:06.01, start: 0.000000, bitrate: 30948 kb/s
=    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt709), 1920x1440 [SAR 1:1 DAR 4:3], 30945 kb/s, 47.95 fps, 47.95 tbr, 720k tbn, 95.90 tbc (default)
=    Metadata:
+      rotate          : 270
=      creation_time   : 2016-10-29T12:10:41.000000Z
*      handler_name    : VideoHandler
+    Side data:
+      displaymatrix: rotation of 90.00 degrees

exiftool 输入.mp4

=ExifTool Version Number         : 10.87
*File Name                       : input.mp4
=Directory                       : C:/
*File Size                       : 23 MB
*File Modification Date/Time     : 2016:10:29 13:10:48+02:00
*File Access Date/Time           : 2018:03:22 22:51:11+01:00
*File Creation Date/Time         : 2018:03:22 22:51:11+01:00
=File Permissions                : rw-rw-rw-
=File Type                       : MP4
=File Type Extension             : mp4
=MIME Type                       : video/mp4
*Major Brand                     : MP4 Base w/ AVC ext [ISO 14496-12:2005]
*Minor Version                   : 0.0.0
*Compatible Brands               : avc1, isom
=Movie Header Version            : 0
=Create Date                     : 2016:10:29 12:10:41
=Modify Date                     : 2016:10:29 12:10:41
*Time Scale                      : 90000
=Duration                        : 6.01 s
=Preferred Rate                  : 1
=Preferred Volume                : 100.00%
=Preview Time                    : 0 s
=Preview Duration                : 0 s
=Poster Time                     : 0 s
=Selection Time                  : 0 s
=Selection Duration              : 0 s
=Current Time                    : 0 s
*Next Track ID                   : 3
-Firmware Version                : HD3.11.03.03
-Lens Serial Number              : LW13121003000141
-Camera Serial Number Hash       : 4833422b363033313530636432356400
=Track Header Version            : 0
=Track Create Date               : 2016:10:29 12:10:41
=Track Modify Date               : 2016:10:29 12:10:41
=Track ID                        : 1
=Track Duration                  : 6.01 s
=Track Layer                     : 0
=Track Volume                    : 0.00%
=Image Width                     : 1920
=Image Height                    : 1440
=Graphics Mode                   : srcCopy
=Op Color                        : 0 0 0
=Compressor ID                   : avc1
=Source Image Width              : 1920
=Source Image Height             : 1440
=X Resolution                    : 72
=Y Resolution                    : 72
-Compressor Name                 : .GoPro AVC encoder
=Bit Depth                       : 24
-Color Representation            : nclc 1 1 1
-Gamma                           : 2.2
=Video Frame Rate                : 47.952
-Matrix Structure                : 1 0 0 0 1 0 0 0 1
=Media Header Version            : 0
=Media Create Date               : 2016:10:29 12:10:41
=Media Modify Date               : 2016:10:29 12:10:41
-Media Time Scale                : 48000
-Media Duration                  : 5.99 s
*Handler Type                    : Audio Track
-Handler Description             : .GoPro AAC
-Balance                         : 0
-Audio Format                    : mp4a
-Audio Channels                  : 2
-Audio Bits Per Sample           : 16
-Audio Sample Rate               : 48000
-Movie Data Size                 : 24045320
-Movie Data Offset               : 32768
*Avg Bitrate                     : 32 Mbps
=Image Size                      : 1920x1440
=Megapixels                      : 2.8
*Rotation                        : 0

exiftool 输出.mp4

=ExifTool Version Number         : 10.87
*File Name                       : output.mp4
=Directory                       : C:/
*File Size                       : 22 MB
*File Modification Date/Time     : 2018:03:23 12:29:19+01:00
*File Access Date/Time           : 2018:03:23 12:28:18+01:00
*File Creation Date/Time         : 2018:03:23 12:28:18+01:00
=File Permissions                : rw-rw-rw-
=File Type                       : MP4
=File Type Extension             : mp4
=MIME Type                       : video/mp4
*Major Brand                     : MP4  Base Media v1 [IS0 14496-12:2003]
*Minor Version                   : 0.2.0
*Compatible Brands               : isom, iso2, avc1, mp41
+Movie Data Size                 : 23232544
+Movie Data Offset               : 48
=Movie Header Version            : 0
=Create Date                     : 2016:10:29 12:10:41
=Modify Date                     : 2016:10:29 12:10:41
*Time Scale                      : 1000
=Duration                        : 6.01 s
=Preferred Rate                  : 1
=Preferred Volume                : 100.00%
=Preview Time                    : 0 s
=Preview Duration                : 0 s
=Poster Time                     : 0 s
=Selection Time                  : 0 s
=Selection Duration              : 0 s
=Current Time                    : 0 s
*Next Track ID                   : 2
=Track Header Version            : 0
=Track Create Date               : 2016:10:29 12:10:41
=Track Modify Date               : 2016:10:29 12:10:41
=Track ID                        : 1
=Track Duration                  : 6.01 s
=Track Layer                     : 0
=Track Volume                    : 0.00%
+Matrix Structure                : 0 1 0 -1 0 0 0 0 1
=Image Width                     : 1920
=Image Height                    : 1440
=Media Header Version            : 0
=Media Create Date               : 2016:10:29 12:10:41
=Media Modify Date               : 2016:10:29 12:10:41
+Media Time Scale                : 720000
+Media Duration                  : 6.01 s
+Media Language Code             : eng
+Handler Description             : VideoHandler
=Graphics Mode                   : srcCopy
=Op Color                        : 0 0 0
=Compressor ID                   : avc1
=Source Image Width              : 1920
=Source Image Height             : 1440
=X Resolution                    : 72
=Y Resolution                    : 72
=Bit Depth                       : 24
+Pixel Aspect Ratio              : 1:1
=Video Frame Rate                : 47.952
*Handler Type                    : Metadata
+Handler Vendor ID               : Apple
+Encoder                         : Lavf58.10.100
*Avg Bitrate                     : 30.9 Mbps
=Image Size                      : 1920x1440
=Megapixels                      : 2.8
*Rotation                        : 270

我在 notepad++ 中比较了 ffmpeg rotate=0 和 rotate=90 .mp4 文件,以识别旋转标志/矩阵,但没有运气,只是胡言乱语。我宁愿只编辑标志(就像 exiftool 对所有类型的元数据都这样做,但不能在视频中进行旋转)而不是将内容流式传输到新容器中,也是为了更快地处理(所有视频都在 NAS 上)。

答案1

exiftool 版本 10.89现在支持旋转 mp4 视频而不改变其他元数据。

用法:

exiftool -rotation=90 video.mp4

答案2

为了获得真正的无损旋转,我找不到解决方案,所以我拿了一个十六进制编辑器(例如氢键) 并分析了旋转的视频文件。

  • 使用十六进制编辑器打开 mp4 并搜索以vide查找视频轨道的元数据
  • 你应该看到上面的几行(我的文件大多是 9 行,有时是 12 行)trak...\tkhd
  • 中间应该有一个@符号(HEX 40)
  • 在它之前的两行存储了旋转矩阵
  • 无旋转:

    00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 180°:

    FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    40
    
  • 顺时针 90°:

    00 00 00 00 00 01 00 00 00 00 00 00 FF FF 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    40 
    
  • 逆时针90°:

    00 00 00 00 FF FF 00 00 00 00 00 00 00 01 00 00 
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    40
    

根据需要更改文件,并且它应该在支持旋转标志的播放器中旋转(大多数当前播放器都支持)。

如果你的视频包含立体声音频,这显然是不会切换的,所以如果你想让声音与视频旋转(180°)相匹配,你需要切换左右声道。

答案3

一些答案表明 exiftool 不能用于此目的,但从最近的版本开始它绝对可以。

exiftool -rotation=0 video.mp4 

这将清除所有旋转标志,并假设相机以正常(横向)方向使用。这确实需要大约一分钟的时间,这令人惊讶,因为它只更改了几个字节。

O:\>exiftool -ver
12.29

T:\>dir 2021*658*.mp4
 Volume in drive T is temp
 Volume Serial Number is 30A1-6AD0

 Directory of T:\

03/06/2021  02:45 PM       327,499,207 20210306_144335-00.00.00.000-00.01.32.658.mp4
               1 File(s)    327,499,207 bytes
               0 Dir(s)  16,700,923,904 bytes free

T:\>exiftool -rotation=0 20210306_144335-00.00.00.000-00.01.32.658.mp4
    1 image files updated

T:\>dir 2021*658*.mp4

12/07/2021  05:19 PM       327,499,207 20210306_144335-00.00.00.000-00.01.32.658.mp4
               1 File(s)    327,499,207 bytes
               0 Dir(s)  16,229,441,536 bytes free

相关内容