Skip to content

Verified way of saving custom metadata in mp4 files

Mradul Dubey edited this page Sep 26, 2022 · 4 revisions

MP4 File Structure for Metadata

The header of a mp4 file (moov) box has movie header(mvhd) and track headers (trak/tkhd) boxes to store metadata. But the format of these boxes is pretty much fixed.

Any other metadata given by user can be saved in the user data box (udata) box.

  • Inside the moov/udata there is a meta box.
  • Each meta box has a given structure: hdlr and ilst. The ilst box is a item list box similar to <li> box in html or map in programming.
  • Each element in ilst has a key box which has the data box.
  • The value is stored in data.Value

Format for custom keys & code changes required

  1. For the metadata stored in moov/udta/ilst/ the length of the keys has to be 4.

  2. Following libmp4 changes:

    2.1 mp4_priv.h changes

     /* Either use one of the existing tags there */
    
       #define MP4_METADATA_TAG_TYPE_ARTIST        0x00415254 /* ".ART" */
       #define MP4_METADATA_TAG_TYPE_TITLE         0x006e616d /* ".nam" */
       #define MP4_METADATA_TAG_TYPE_DATE          0x00646179 /* ".day" */
       #define MP4_METADATA_TAG_TYPE_COMMENT       0x00636d74 /* ".cmt" */
       #define MP4_METADATA_TAG_TYPE_COPYRIGHT     0x00637079 /* ".cpy" */
       #define MP4_METADATA_TAG_TYPE_MAKER         0x006d616b /* ".mak" */
       #define MP4_METADATA_TAG_TYPE_MODEL         0x006d6f64 /* ".mod" */
       #define MP4_METADATA_TAG_TYPE_VERSION       0x00737772 /* ".swr" */
       #define MP4_METADATA_TAG_TYPE_ENCODER       0x00746f6f /* ".too" */
    
    
     /* Or add a new custom tag with the corresponding hex representation of all character's ascii value, like following */
     /* Note that first char will be ignored because while reading a mask (0xFFFFFF) on last 24 bits will be applied */
     #define MP4_METADATA_TAG_TYPE_STS           0x00737473 /* ".sts" */
    

    2.2 In mp4_box_reader.cpp/mp4_box_meta_data_read, add the custom tag in the switch case: image

  3. In the writer/reader code, mp4_mux_add_file_metadata and mp4_demux_get_metadata_strings can be used respectively to add/retrieve the values. Examples are present in Mp4WriterSink and Mp4ReaderSource modules.

Side Note: there is also a moov/meta box which stores all metadata key value pairs with key format starting with com.*.* but I havent been able to get it to work.