使用 exiftool 和 ffmpeg 去除视频的旋转参数
macOS 升级到最新版本(26.0.1)后,基于 Datavyu + ffmpeg 的视频读取出错误的视频旋转方向,原因是 datavyu 调用 ffmpeg 没有正确使用旋转参数。鉴于 datavyu 上次发布还是在 2022 年,目测短期内不会修复这个问题。只能本地处理视频的 Rotation 为 0。
解决方案:
- 先用 exiftool 清除 Rotation(设置为0);
- 使用 ffpmeg 旋转视频。
1个G的视频在 M4 芯片上需要跑大概 5 分钟。
下面是处理脚本:
demo.mp4
是我本地的视频文件,请根据你的情况设置。
# 查看视频的 Rotation,90 表示旋转了 90 度。
exiftool -Rotation -Rotate -VideoRotation -n demo.mp4
# 使用 exiftool 移除视频的 Rotation 信息
exiftool -track1:matrixstructure="1 0 0 0 1 0 0 0 1" -rotation=0 demo.mp4
此时会看到两个文件,其中 demo.mp4 是处理后的,demo.mp4_original 则是原始视频。
再次使用 exiftool 查看 Rotation 信息,可以看到 Rotation 已经被清除了。
$ exiftool -Rotation -Rotate -VideoRotation -n demo.mp4
Rotation : 0
但此时视频的方向是不对的(如果视频方向是对的,那就不用进行下一步了)。
下面开始旋转视频,比较费时间:
# demo.mp4 是原始视频,demo2.mp4 是转换后的视频,根据实际情况修改
ffmpeg -i demo.mp4 -vf "transpose=1,setsar=1" -c:v libx264 -crf 18 -preset veryfast -c:a copy demo2.mp4
过几分钟处理完成,生成 demo2.mp4 文件,这个就是最后的成功,移除了旋转方向信息的视频,可以正常用了。