简体   繁体   English

如何使用mp4parser将元数据写入mp4文件?

[英]How to write metadata to mp4 file using mp4parser?

I'm using mp4parser to mux h264 and aac file which are re-encoded from orginal video file,how can I write the metadata of the original video to the new mp4 file? 我正在使用mp4parser来复用从原始视频文件重新编码的h264和aac文件,如何将原始视频的元数据写入新的mp4文件? Or is there a common method to write metadata to mp4 file? 还是有将元数据写入mp4文件的通用方法?

metadata and MP4 is a really problem. 元数据和MP4是一个真正的问题。 There is no generally supported specification. 没有普遍支持的规范。 But this is only one part of the problem. 但这只是问题的一部分。

  • Prob (1): When to write metadata 问题(1):何时写入元数据
  • Prob (2): What to write 概率(2):写什么

Prob (1) is relatively easy to solve: Just extend the DefaultMp4Builder or the FragmentedMp4Builder on your own and override the 概率(1)相对容易解决:只需自行扩展DefaultMp4Builder或FragmentedMp4Builder并覆盖

protected ParsableBox createUdta(Movie movie) {
    return null;
}

with something meaningful. 有一些有意义的东西。 Eg: 例如:

protected ParsableBox createUdta(Movie movie) {
    UserDataBox udta = new UserDataBox();
    CopyrightBox copyrightBox = new CopyrightBox();
    copyrightBox.setCopyright("All Rights Reserved, me, myself and I, 2015");
    copyrightBox.setLanguage("eng");
    udta.addBox(copyrightBox);
    return udta;
}

some people used that to write apple compatible metadata but even though there are some classes in my code I never really figured out what works and what not. 有些人用它来编写与Apple兼容的元数据,但是即使我的代码中有一些类,我也从未真正弄清楚什么有效,哪些无效。 You might want to have a look into Apple's specification here 您可能要在这里查看Apple的规格

And yes: I'm posting this a year to late. 是的:我将这一消息发布到了一年以后。

It seems that the 'mp4parser' library ( https://code.google.com/p/mp4parser/ ), supports writing Metadata to mp4 files in Android. 似乎“ mp4parser”库( https://code.google.com/p/mp4parser/ )支持在Android中将元数据写入mp4文件。 However, I've found there's little-to-no documentation on how to do this, beyond a few examples in their codebase. 但是,除了他们的代码库中的一些示例外,我发现关于如何执行此操作的文档很少。 I've had some luck with the following example, which writes XML metadata into the 'moov/udta/meta' box: 以下示例为我带来了一些运气,该示例将XML元数据写入“ moov / udta / meta”框中:

https://github.com/copiousfreetime/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/stuff/ChangeMetaData.java https://github.com/copiousfreetime/mp4parser/blob/master/examples/src/main/java/com/googlecode/mp4parser/stuff/ChangeMetaData.java

If you consider the alternatives you might want to look at JCodec for this purpose. 如果您考虑替代方案,则可能需要为此目的查看JCodec。 It now has the org.jcodec.movtool.MetadataEditor API (and a matching CLI org.jcodec.movtool.MetadataEditorMain ). 现在,它具有org.jcodec.movtool.MetadataEditor API(以及匹配的CLI org.jcodec.movtool.MetadataEditorMain )。

Their documentation contains many samples: http://jcodec.org/docs/working_with_mp4_metadata.html 他们的文档包含许多示例: http : //jcodec.org/docs/working_with_mp4_metadata.html

So basically when you want to add some metadata you need to know what key(s) it corresponds to. 因此,基本上,当您想添加一些元数据时,您需要知道其对应的键。 One way to find out is to inspect a sample file that already has the metadata you need. 一种查找方法是检查已经具有所需元数据的样本文件。 For this you can run the JCodec's CLI tool that will just print out all the existing metadata fields (keys with values): 为此,您可以运行JCodec的CLI工具,该工具将仅打印出所有现有的元数据字段(带有值的键):

./metaedit <file.mp4>

Then when you know the key you want to work with you can either use the same CLI tool: 然后,当您知道要使用的密钥时,可以使用相同的CLI工具:

# Changes the author of the movie
./metaedit -f -si ©ART=New\ value file.mov

or the same thing via the Java API: 或通过Java API进行的相同操作:

MetadataEditor mediaMeta = MetadataEditor.createFrom(new
    File("file.mp4"));
Map<Integer, MetaValue> meta = mediaMeta.getItunesMeta();
meta.put(0xa9415254, MetaValue.createString("New value")); // fourcc for '©ART'
mediaMeta.save(false); // fast mode is off

To delete a metadata field from a file: 要从文件中删除元数据字段:

MetadataEditor mediaMeta = MetadataEditor.createFrom(new
    File("file.mp4"));
Map<Integer, MetaValue> meta = mediaMeta.getItunesMeta();
meta.remove(0xa9415254); // removes the '©ART'
mediaMeta.save(false); // fast mode is off

To convert string to integer fourcc you can use something like: 要将字符串转换为整数fourcc,可以使用类似以下内容的方法:

byte[] bytes = "©ART".getBytes("iso8859-1");
int fourcc =
    ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getInt();

If you want to edit/delete the android metadata you'll need to use a different set of fucntion (because it's stored differently than iTunes metadata): 如果要编辑/删除android元数据,则需要使用其他功能集(因为它的存储方式与iTunes元数据的存储方式不同):

./metaedit -sk com.android.capture.fps,float=25.0 file.mp4

OR alternatively the same through the API: 或者通过API相同:

MetadataEditor mediaMeta = MetadataEditor.createFrom(new
    File("file.mp4"));
Map<String, MetaValue> meta = mediaMeta.getKeyedMeta();
meta.put("com.android.capture.fps", MetaValue.createFloat(25.));
mediaMeta.save(false); // fast mode is off

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM