开始想着ffmpeg应该可以,最后试验ffmpeg只支持mkv, 不支持mp4写入

FFmpeg's Matroska muxer allows random tags, so

ffmpeg -i input -c copy
       -metadata key1=value1
       -metadata:s:v key2=value2
       -metadata:s:a:0 key3=value3
out.mkv

mkv就可以写成功:

ffmpeg -i in-black.mp4 -metadata custom_meta=1 -codec copy in-black.mkv

mp4确实写不进去:

ffmpeg -i 1106211062.mp4 -metadata:s:v custom_meta=1 1106211062-out.mp4
ffmpeg -i in-black.mp4 -metadata custom_meta=1 -codec copy in-black-out.mp4

最后没有其他办法,想到了在用的jcodec支持meta写入,jcodec是一个lisence为FreeBSD的jar,用org.jcodec.movtool.MetadataEditor就可以,代码如下:

String file = "/storage/emulated/0/DCIM/Camera/VID_20190927_193230.mp4";
try {               
    MetadataEditor metadataEdit = MetadataEditor.createFrom(new File(file));
    Map<String, MetaValue> keyedMeta = metadataEdit.getKeyedMeta();
    keyedMeta.put("custom-meta", MetaValue.createInt(1));
    keyedMeta.put("custom-meta-2", MetaValue.createInt(2));
    keyedMeta.put("custom-meta-3", MetaValue.createInt(3));
    metadataEdit.save(true);

} catch (Exception e) {
    Log.w(TAG, "error \n", e);
}                   

在手机的小demo上运行,然后pull出来,确实是可以的:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2019-09-27T11:32:43.000000Z
    com.android.version: 9
    custom-meta     : 1
    custom-meta-2   : 2
    custom-meta-3   : 3
  Duration: 00:00:11.81, start: 0.000000, bitrate: 20460 kb/s```
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐