Android:读写.mp4元数据-Tag

问题描述 投票:6回答:2

我想读取和编辑(写入)mp4元数据。特别是我想在android中读/写Tag元数据,如下图所示。

Mp4 Metadata Picture

我在互联网上搜索了这个,发现mp4Parser,但我认为mp4Parser不会写标题关键字。

对于.jpg文件,我们使用代表标题的XPKeyword元数据。同样如何为.mp4文件做同样的事情。

android metadata mp4
2个回答
8
投票

Android SDK包含类MediaMetadataRetriever以检索文件中的所有元数据值,您可以阅读有关MetaData值here的更多信息。

public void readMetaData() {
    File sdcard = Environment.getExternalStorageDirectory();
    File  file = new File(sdcard,"/Android/data/myfile.mp4");

    if (file.exists()) {
        Log.i(TAG, ".mp4 file Exist");

        //Added in API level 10
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(file.getAbsolutePath());
            for (int i = 0; i < 1000; i++){
                //only Metadata != null is printed!
                if(retriever.extractMetadata(i)!=null) {
                    Log.i(TAG, "Metadata :: " + retriever.extractMetadata(i));
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception : " + e.getMessage());
        }
    } else {
        Log.e(TAG, ".mp4 file doesn´t exist.");
    }
}

要编辑/编写元数据,Android SDK没有任何方法,可能是版权问题,但您可以使用以下选项:

https://github.com/sannies/mp4parser

http://multimedia.cx/eggs/supplying-ffmpeg-with-metadata/

https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/FFmpegFrameRecorder.java


0
投票

我希望现在还为时不晚,但如果你想在MP4文件上添加/编辑/删除元数据字段,你可以使用JCodec的元数据编辑类。

有一个由Java API支持的CLI工具。 CLI位于org.jcodec.movtool.MetadataEditorMain,API位于org.jcodec.movtool.MetadataEditor

了解更多信息:http://jcodec.org/docs/working_with_mp4_metadata.html

所以基本上当你想要添加一些元数据时,你需要知道它对应的键。找到答案的一种方法是检查已经拥有所需元数据的示例文件。为此,您可以运行JCodec的CLI工具,该工具将打印出所有现有的元数据字段(带有值的键):

./metaedit <file.mp4>

然后,当您知道要使用的密钥时,可以使用相同的CLI工具:

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

或者通过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

要从文件中删除元数据字段:

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

要将字符串转换为整数fourcc,您可以使用以下内容:

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

如果你想编辑/删除android元数据,你需要使用一组不同的功能(因为它的存储方式与iTunes元数据不同):

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

或者通过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
© www.soinside.com 2019 - 2024. All rights reserved.