当前位置: 首页 > news >正文

ffmpeg-mov-metadate不识别Bug修复

文章目录

    • BUG起因
    • 类似问题反馈
    • 问题解决
      • 具体步骤:
    • 阅读过文章
    • ffmpeg命令行解析调试流程记录
    • movenc.c源码分析

BUG起因

在ffmpeg参数默认可识别的metadata参数如下:

具体可见libavformat/movenc.c->mov_write_udta_tag()

        mov_write_string_metadata(s, pb_buf, "\251ART", "artist",      0);mov_write_string_metadata(s, pb_buf, "\251nam", "title",       0);mov_write_string_metadata(s, pb_buf, "\251aut", "author",      0);mov_write_string_metadata(s, pb_buf, "\251alb", "album",       0);mov_write_string_metadata(s, pb_buf, "\251day", "date",        0);mov_write_string_metadata(s, pb_buf, "\251swr", "encoder",     0);// currently ignored by mov.cmov_write_string_metadata(s, pb_buf, "\251des", "comment",     0);// add support for libquicktime, this atom is also actually read by mov.cmov_write_string_metadata(s, pb_buf, "\251cmt", "comment",     0);mov_write_string_metadata(s, pb_buf, "\251gen", "genre",       0);mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright",   0);mov_write_string_metadata(s, pb_buf, "\251mak", "make",        0);mov_write_string_metadata(s, pb_buf, "\251mod", "model",       0);mov_write_string_metadata(s, pb_buf, "\251xyz", "location",    0);mov_write_string_metadata(s, pb_buf, "\251key", "keywords",    0);mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");

而原视频中的参数都是:

com.apple.quicktime.location.accuracy.ho : 
com.apple.quicktime.location.ISO6709     : 
com.apple.quicktime.make                 : 
com.apple.quicktime.model                : 
com.apple.quicktime.software             : 
com.apple.quicktime.creationdate         :

这些参数不在ffmpeg的atom列表中 所以ffmpeg会当作自定义medatada参数处理,可以使用脚本测试:

#!/bin/bash
list=( title author album_artist album grouping composer year track comment genre copyright description synopsis show episode_id network lyrics )
for i in ${list[@]};doecho "use -> $i"./ffmpeg -i input_old.mov -metadata ${i}="YYYY-MM-DD HH:MM:SS" -c copy output_mdta_time.mov -y && mediainfo output_mdta_time.mov | grep "YYYY-MM-DD HH:MM:SS" --colorsleep 1
done

那么就需要使用-movflags use_metadata_tags参数来添加自定义metadata在使用ffmpeg对视频添加metadata或者保存metadata时处理之后视频在QuickTime中无法正确读取metadata->key比如:

$ ./ffmpeg -i input_old.mov -c copy -movflags use_metadata_tags -metadata name="YJ" output.mov -y

此时通过ffprobe、mediainfo等工具查看是没有问题的:

$ mediainfo output.mov  
General
Complete name                            : output.mov
Format                                   : MPEG-4
Format profile                           : QuickTime
Codec ID                                 : qt   0000.02 (qt  )
File size                                : 785 KiB
Duration                                 : 780 ms
Overall bit rate                         : 8 245 kb/s
major_brand                              : qt
minor_version                            : 0
compatible_brands                        : qt
com.apple.quicktime.creationdate         : xxx//clear
com.apple.quicktime.location.accuracy.ho : xxx//clear
com.apple.quicktime.location.ISO6709     : xxx//clear
com.apple.quicktime.make                 : Apple
com.apple.quicktime.model                : iPhonexxx //clear
com.apple.quicktime.software             : xxx//clear
name                                     : YJ
encoder                                  : Lavf58.29.100

但是QuickTime获取视频属性、以及IOS开发使用AVAsset中会出现如下情况:

//ios->switch
let _asset = AVAsset(url: videoURL)
let metadata = _asset.metadata
print("start\(metadata.count)")
for item in metadata {if let identifier = item.identifier?.rawValue,let value = item.value {print("Identifier: \(identifier), Value: \(value)")}
}
print("end")//Print : 
start11
Identifier: itsk/%00%00%00%01, Value: qt  
Identifier: itsk/%00%00%00%02, Value: 0
Identifier: itsk/%00%00%00%03, Value: qt  
Identifier: itsk/%00%00%00%04, Value: 2023-03-25T14:57:05+0800
Identifier: itsk/%00%00%00%05, Value: 35.000000
Identifier: itsk/%00%00%00%06, Value: xxxx
Identifier: itsk/%00%00%00%07, Value: Apple
Identifier: itsk/%00%00%00%08, Value: xxx
Identifier: itsk/%00%00%00%09, Value: xxx
Identifier: itsk/%00%00%00%0A, Value: YJ
Identifier: itsk/%00%00%00%0B, Value: xxx
end

类似问题反馈

https://trac.ffmpeg.org/ticket/4209

  1. 添加-movflags use_metadata_tags将允许您添加和继承自定义元数据。至少ff*工具将在输出中显示这些标签

  2. 我刚刚用iPhone X HEVC视频测试了ffmpeg 4.0.3版本。
    命令很简单:

     ffmpeg -i IMG_9988.MOV -movflags use_metadata_tags -c copy aaaa.mov 
    

    我可以用ffprobe在目标文件(GPS、模型等)上看到原始元数据:

    com.apple.quicktime.创建日期:2018-11-28T17:47:53-0200com.apple.quicktime.location.ISO6709:-23.5424-046.6576/com.apple.quicktime.make:苹果com.apple.quicktime.model:iPhone Xcom.apple.quicktime.software:12.1
    

    但苹果堆栈不会在目标文件上显示元数据。这是一张屏幕截图,左侧显示右侧的源和目标

https://exiftool.org/forum/index.php?topic=11782.0

ffmpeg似乎支持数量有限的.mp4格式的iTunes标签。'-map_metadata 0’不保留自定义/任意元密钥。为此目的的开关是“-movflags use_metadata_tags”,但这些是以非常规方式编写的,Quicktime Player无法识别它们(我目前使用的是Mojave QT–Catalina QT有些不同)

https://www.linuxmi.com/ffmpeg-shili.html

ffmpeg -i source.mov \-map_metadata 0 -movflags use_metadata_tags \
linuxmi.mp4

由于某些视频存储自定义元数据,此命令可能无法正确复制所有数据。

https://www.mail-archive.com/libav-user@ffmpeg.org/msg12683.html

在编码视频文件时向容器添加元数据

我在向mp4或mov容器添加元数据时遇到一些问题,不确定是否>这是avformat中的问题,或者如果我做错了什么。
在查看了二进制ffmpeg后,我自己找到了答案,它具有同样的问题,如果做:ffmpeg -i input.mp4 -metadata hello='world' -c copy output.mp4...网上有更多关于命令行工具的信息比图书馆 :)...我在这里找到了:https://superuser.com/questions/1208273/how-to-add-new-and-non-defined-metadata-to-an-mp4-file...输出用户元数据ffmpeg需要以下选项:-movflags use_metadata_tags...所以我这样更改了avformat_write_header()调用:av_dict_set(&options, "movflags", "use_metadata_tags", 0);/* 编写流标头(如果有的话)。*/int ret = avformat_write_header(oc_, &options);// 字典复制到write_header中,我们应该释放这是我们的副本av_dict_free(&options);...现在我的metadatsa出现在输出文件中!我希望这对有问题的人有所帮助,那不是非常明显的行为

https://video.stackexchange.com/questions/23741/how-to-prevent-ffmpeg-from-dropping-metadata

现在我发现use_metadata_tags选项是个坏主意,因为它不是标准-例如Plex不再读取任何元数据。有趣的是,我试图将更多的元数据导入Plex,但这个选项实际上恰恰相反。在上面的代码中,我可以看到MDTA的方式是写入所有键,然后写入所有值,而我想大多数程序都期望列表中的键/值对,因此不知道如何读取MDTA元数据。

支持最多的解决方案:

exiftool -TagsFromFile source.mp4 -All:All target.mp4

问题解决

为了看清楚ffmpeg对metadata做了什么这里使用exiftool工具进行比较

$ exiftool -v input_old.mov > 1 && exiftool -v output.mov > 2
$ vim -d 1 2

注意看下面(左边为原始视频、右边为ffmpeg编辑后视频metadata)区别:

    Movie (SubDirectory) -->       │    Movie (SubDirectory) -->+ [Movie directory]            │    + [Movie directory]// .....| UserData (SubDirectory) -->  │    | UserData (SubDirectory) -->| Unknown_free =               │    | + [UserData directory]| Meta (SubDirectory) -->      │    | | Meta (SubDirectory) -->| + [Meta directory]           │    | | + [Meta directory]| | Handler (SubDirectory) --> │    | | | Handler (SubDirectory) --| | + [BinaryData directory, 26│    | | | + [BinaryData directory,| | | HandlerClass =           │    | | | | HandlerClass =| | | HandlerType = mdta       │    | | | | HandlerType = mdta| | | HandlerVendorID =        │    | | | | HandlerVendorID =| | | HandlerDescription =     │    | | | | HandlerDescription =| | Keys (SubDirectory) -->    │    | | | Keys (SubDirectory) -->| | + [Keys directory]         │    | | | + [Keys directory]| | | Added ItemList Tag 1.1 = │    | | | | Added ItemList Tag 1.1| | | Added ItemList Tag 1.2 = │    | | | | Added ItemList Tag 1.2| | | Added ItemList Tag 1.3 = │    | | | | Added ItemList Tag 1.3| | | Added ItemList Tag 1.4 = │    | | | | Added ItemList Tag 1.4| | | Added ItemList Tag 1.5 = │    | | | | Added ItemList Tag 1.5| | | Added ItemList Tag 1.6 = │    | | | | Added ItemList Tag 1.6| | ItemList (SubDirectory) -->│    | | | | Added ItemList Tag 1.7| | + [ItemList directory]     │    | | | | Added ItemList Tag 1.8| | | LocationAccuracyHorizonta│    | | | | Added ItemList Tag 1.9| | | GPSCoordinates = xxx.    │    | | | | Added ItemList Tag 1.10| | | Make = Apple             │    | | | | Added ItemList Tag 1.11| | | Model = iPhxxx.          │    | | | ItemList (SubDirectory) -| | | Software = xxxxxx        │    | | | + [ItemList directory]| | | CreationDate = 2023-03-25│    | | | | MajorBrand = qt

结果是ffmpeg将Meta Box放在了Movie->UserData->Meta Box的位置。而这个位置在QuickTime中是无法识别的,QuickTime的具体解释如下:

https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/Metadata/Metadata.html

Storage Location in a QuickTime File
Within a QuickTime file, metadata can be stored within a movie atom (‘moov’), a track atom (‘trak’) or a media atom (‘mdia’). Only one metadata atom is allowed for each location. If there is user data and metadata stored in the same location, and both declare the same information, for example, declare a copyright notice, the metadata takes precedence

具体的"Metadata Structure"构造在ffmpeg->libavformat/movenc.c文件中可见:

  /* meta data tags */static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,AVFormatContext *s){int size = 0;int64_t pos = avio_tell(pb); s:avio_wb32(pb, 0); /* size */ s: val:ffio_wfourcc(pb, "meta"); s:avio_wb32(pb, 0);if (mov->flags & FF_MOV_FLAG_USE_MDTA) {mov_write_mdta_hdlr_tag(pb, mov, s);                                   mov_write_mdta_keys_tag(pb, mov, s);mov_write_mdta_ilst_tag(pb, mov, s);}else {/* iTunes metadata tag */mov_write_itunes_hdlr_tag(pb, mov, s);mov_write_ilst_tag(pb, mov, s);}size = update_size(pb, pos);return size;}

所以只需要将Movie->UserData->Meta Box转移到Movie->Meta Box即可正确识别

具体步骤:

  1. 修改libavformat/movenc.c如下:
$ git diff movenc.cint64_t pos = avio_tell(pb);avio_wb32(pb, 0); /* size */ffio_wfourcc(pb, "meta");
-    avio_wb32(pb, 0);
+    //TODO -> update2
+    //avio_wb32(pb, 0); //Offset to -> moov/metaif ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
+        /*
+         * TODO update1
+         *创建一个box,udta box 的大小应该包括 8 个字节的 box 头:
+         *- 4 个字节的 box 大小
+         *- 4 个字节的 box 类型
+         */
+    if (!(mov->mode & MODE_3GP) &&
+        !(mov->mode == MODE_MOV && !(mov->flags & FF_MOV_FLAG_USE_MDTA))) {
+        avio_wb32(pb, 8); //置空moov->uata box
+        ffio_wfourcc(pb, "udta");
+        avio_write(pb, buf, size); //将moov->uata->meta box偏移到moov->meta box
+    }else{avio_wb32(pb, size + 8);ffio_wfourcc(pb, "udta");avio_write(pb, buf, size);}
+
+    }av_free(buf);
  1. 重新编译make
  2. 再次执行保存metadata
$ ./ffmpeg -i input_old.mov -c copy -movflags use_metadata_tags -metadata name="YJ" output.mov -y
  1. 测试
//ios->switch
let _asset = AVAsset(url: videoURL)
let metadata = _asset.metadata
print("start\(metadata.count)")
for item in metadata {if let identifier = item.identifier?.rawValue,let value = item.value {print("Identifier: \(identifier), Value: \(value)")}
}
print("end")//Print : 
start10
Identifier: mdta/major_brand, Value: qt  
Identifier: mdta/minor_version, Value: 0
Identifier: mdta/compatible_brands, Value: qt  
Identifier: mdta/com.apple.quicktime.creationdate, Value: 2023-03-25T14:57:05+0800
Identifier: mdta/com.apple.quicktime.location.accuracy.horizontal, Value: 35.000000
Identifier: mdta/com.apple.quicktime.location.ISO6709, Value: xxxx
Identifier: mdta/com.apple.quicktime.make, Value: Apple
Identifier: mdta/com.apple.quicktime.model, Value: iPhoxxx
Identifier: mdta/com.apple.quicktime.software, Value: xxx
Identifier: mdta/name, Value: YJ
Identifier: mdta/encoder, Value: Lavf58.29.100
end

阅读过文章

  • https://www.qiniu.com/qfans/qnso-70261246#comments
  • https://trac.ffmpeg.org/ticket/4209?cversion=2&cnum_hist=25#comment:25
  • https://trac.ffmpeg.org/ticket/4209
  • https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/Metadata/Metadata.html

ffmpeg命令行解析调试流程记录

main

ffmpeg_parse_options

split_commandline -> av_log(NULL, AV_LOG_DEBUG, "Reading option '%s' ...", opt);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hQmJ9qBh-1682697624690)(…/Library/Application Support/typora-user-images/image-20230311144719815.png)]

这里可以直接快速断电在``fftools/cmdutils.c+823`即可查看每次循环得到的参数

split_commandline后就会解析完成所有参数命令

fftools/ffmpeg_opt.c -> ffmpeg_parse_options->open_files() 会读取输入文件并打印输入文件的数据,比如metadata

fftools/ffmpeg_opt.c+3331-> ret = open_files(&octx.groups[GROUP_OUTFILE], “output”, open_output_file);

movenc.c源码分析

命令如下:

set args -i input_old.mov -movflags use_metadata_tags -c copy output.mov -y

判断用户态释放输入了-use-metadata参数:

  3630	 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,3631	                               AVFormatContext *s)3632	 {3633	     int size = 0;3634	     int64_t pos = avio_tell(pb);3635	     avio_wb32(pb, 0); /* size */3636	     ffio_wfourcc(pb, "meta");3637	     avio_wb32(pb, 0);3638	     if (mov->flags & FF_MOV_FLAG_USE_MDTA) {3639	         mov_write_mdta_hdlr_tag(pb, mov, s);//hdlr 标签用于指定这些自定义元数据的类型,比如使用 'mdta' 表示这是用户自定义元数据。3640	         mov_write_mdta_keys_tag(pb, mov, s);//keys 标签用于指定这些自定义元数据的键名和键类型,比如使用 com.apple.quicktime.description 表示这是 QuickTime 描述信息的键名。3641	         mov_write_mdta_ilst_tag(pb, mov, s);//ilst 标签用于实际存储这些自定义元数据的值。3642	     }

主要是下面几个函数:

          mov_write_mdta_keys_tag(pb, mov, s);mov_write_mdta_ilst_tag(pb, mov, s);
  static int mov_write_mdta_ilst_tag(AVIOContext *pb, MOVMuxContext *m  ov,AVFormatContext *s){AVDictionaryEntry *t = NULL;int64_t pos = avio_tell(pb); s:int count = 1; /* keys are 1-index based */avio_wb32(pb, 0); /* size */ s: val:ffio_wfourcc(pb, "ilst"); s:while (t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX  )) { m: key: prev: flags:int64_t entry_pos = avio_tell(pb); s:avio_wb32(pb, 0); /* size */ s: val:avio_wb32(pb, count); /* key */ s: val:mov_write_string_data_tag(pb, t->value, 0, 1); data: lang: lupdate_size(pb, entry_pos); pos:count += 1; //TODO 这里改了有用}return update_size(pb, pos);}
http://www.lryc.cn/news/64532.html

相关文章:

  • (8)(8.6) 引导程序更新
  • 汽车电路图、原理框图、线束图、元器件布置图的识读技巧与要点
  • ( 数组和矩阵) 667. 优美的排列 II ——【Leetcode每日一题】
  • 【python基础语法七】python内置函数和内置模块
  • 81. read readline readlines 读取文件的三种方法
  • 【社区图书馆】【图书活动第四期】
  • webpack学习指南(上)
  • 刷题记录˃ʍ˂
  • Word2vec原理+实战学习笔记(二)
  • 什么是Java的多线程?
  • “use strict“是什么? 使用它有什么优缺点?
  • 【C++】C++11常用特性总结
  • 泛型——List 优于数组
  • JavaScript中对象的定义、引用和复制
  • JavaScript通过函数异常处理来输入圆的半径,输出圆的面积的代码
  • Ubuntu 安装 Mysql
  • 【五一创作】【Midjourney】Midjourney 连续性人物创作 ② ( 获取大图和 Seed 随机种子 | 通过 seed 随机种子生成类似图像 )
  • 分布式事务 --- Seata事务模式、高可用
  • SQL(基础)
  • 「OceanBase 4.1 体验」OceanBase 4.1社区版的部署及使用体验
  • 计算机操作系统实验:银行家算法模拟
  • 机器学习:多项式拟合分析中国温度变化与温室气体排放量的时序数据
  • 一个 24 通道 100Msps 逻辑分析仪
  • 使用Process Explorer和Dependency Walker排查C++程序中dll库动态加载失败问题
  • 网工Python:如何使用Netmiko的SCP函数进行文件传输?
  • 题目 3166: 蓝桥杯2023年第十四届省赛真题-阶乘的和--不能完全通过,最好情况通过67.
  • ChatGPT- OpenAI 的 模型(Model) 介绍
  • X 态及基于 VCS 的 X-Propagation 检测
  • 数据库之事务隔离级别详解
  • 守护进程、僵尸进程、孤儿进程