mp4meta.cpp

来自「6410BSP1」· C++ 代码 · 共 1,005 行 · 第 1/2 页

CPP
1,005
字号
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is MPEG4IP. * * The Initial Developer of the Original Code is Cisco Systems Inc. * Portions created by Cisco Systems Inc. are * Copyright (C) Cisco Systems Inc. 2001.  All Rights Reserved. * * Contributor(s): *      M. Bakker     mbakker at nero.com * * Apple iTunes Metadata handling *//** The iTunes tagging seems to support any tag field name but there are some predefined fields, also known from the QuickTime format predefined fields (the ones I know of until now): - ﹏am : Name of the song/movie (string) - 〢RT : Name of the artist/performer (string) - ﹚rt : Name of the writer (string) - ゛lb : Name of the album (string) - ヾay : Year (4 bytes, e.g. "2003") (string) - ﹖oo : Tool(s) used to create the file (string) - ヽmt : Comment (string) - ゞen : Custom genre (string) - ゞrp : Grouping (string) - trkn : Tracknumber (8 byte string)           16 bit: empty           16 bit: tracknumber           16 bit: total tracks on album           16 bit: empty - disk : Disknumber (8 byte string)           16 bit: empty           16 bit: disknumber           16 bit: total number of disks           16 bit: empty - gnre : Genre (16 bit genre) (ID3v1 index + 1) - cpil : Part of a compilation (1 byte, 1 or 0) - tmpo : Tempo in BPM (16 bit) - covr : Cover art (xx bytes binary data) - ---- : Free form metadata, can have any name and any data**/#include "mp4common.h"bool MP4File::GetMetadataByIndex(u_int32_t index,                                 const char** ppName,                                 u_int8_t** ppValue, u_int32_t* pValueSize){    char s[256];    sprintf(s, "moov.udta.meta.ilst.*[%u].data.metadata", index);    GetBytesProperty(s, ppValue, pValueSize);    sprintf(s, "moov.udta.meta.ilst.*[%u]", index);    MP4Atom* pParent = m_pRootAtom->FindAtom(s);    *ppName = pParent->GetType();    /* check for free form tagfield */    if (memcmp(*ppName, "----", 4) == 0)    {        u_int8_t* pV;        u_int32_t VSize = 0;        char *pN;        sprintf(s, "moov.udta.meta.ilst.*[%u].name.metadata", index);        GetBytesProperty(s, &pV, &VSize);        pN = (char*)malloc((VSize+1)*sizeof(char));        memset(pN, 0, (VSize+1)*sizeof(char));        memcpy(pN, pV, VSize*sizeof(char));        *ppName = pN;    }    return true;}bool MP4File::CreateMetadataAtom(const char* name){    char s[256];    char t[256];    sprintf(t, "udta.meta.ilst.%s.data", name);    sprintf(s, "moov.udta.meta.ilst.%s.data", name);    AddDescendantAtoms("moov", t);    MP4Atom *pMetaAtom = m_pRootAtom->FindAtom(s);    if (!pMetaAtom)        return false;    /* some fields need special flags set */    if ((uint8_t)name[0] == 0251)    {        pMetaAtom->SetFlags(0x1);    } else if ((memcmp(name, "cpil", 4) == 0) || (memcmp(name, "tmpo", 4) == 0)) {        pMetaAtom->SetFlags(0x15);    }    MP4Atom *pHdlrAtom = m_pRootAtom->FindAtom("moov.udta.meta.hdlr");    MP4StringProperty *pStringProperty = NULL;    MP4BytesProperty *pBytesProperty = NULL;    ASSERT(pHdlrAtom);    pHdlrAtom->FindProperty(        "hdlr.handlerType", (MP4Property**)&pStringProperty);    ASSERT(pStringProperty);    pStringProperty->SetValue("mdir");    u_int8_t val[12];    memset(val, 0, 12*sizeof(u_int8_t));    val[0] = 0x61;    val[1] = 0x70;    val[2] = 0x70;    val[3] = 0x6c;    pHdlrAtom->FindProperty(        "hdlr.reserved2", (MP4Property**)&pBytesProperty);    ASSERT(pBytesProperty);    pBytesProperty->SetReadOnly(false);    pBytesProperty->SetValue(val, 12);    pBytesProperty->SetReadOnly(true);    return true;}bool MP4File::DeleteMetadataAtom(const char* name, bool try_udta){    MP4Atom *pMetaAtom = NULL;    char s[256];    sprintf(s, "moov.udta.meta.ilst.%s", name);    pMetaAtom = m_pRootAtom->FindAtom(s);    if (pMetaAtom == NULL && try_udta) {      sprintf(s, "moov.udta.%s", name);      pMetaAtom = m_pRootAtom->FindAtom(s);    }    /* if it exists, delete it */    if (pMetaAtom)    {        MP4Atom *pParent = pMetaAtom->GetParentAtom();        pParent->DeleteChildAtom(pMetaAtom);        delete pMetaAtom;        return true;    }    return false;}bool MP4File::SetMetadataString (const char *atom, const char *value){  char atomstring[40];  MP4Atom *pMetaAtom;  MP4BytesProperty *pMetadataProperty = NULL;  sprintf(atomstring, "moov.udta.meta.ilst.%s.data", atom);  pMetaAtom = m_pRootAtom->FindAtom(atomstring);    if (!pMetaAtom)    {      if (!CreateMetadataAtom(atom))    return false;            pMetaAtom = m_pRootAtom->FindAtom(atomstring);    }  pMetaAtom->FindProperty("data.metadata", (MP4Property**)&pMetadataProperty);  ASSERT(pMetadataProperty);    pMetadataProperty->SetValue((u_int8_t*)value, strlen(value));    return true;}bool MP4File::GetMetadataString (const char *atom, char **value, bool try_udta){    unsigned char *val = NULL;    u_int32_t valSize = 0;    char atomstring[60];    sprintf(atomstring, "moov.udta.meta.ilst.%s.data.metadata", atom);    *value = NULL;    if (try_udta == false) {      GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);    } else {      bool got_it = false;      try {    GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);    got_it = true;      }      catch (MP4Error* e) {    delete e;      }      if (got_it == false) {    sprintf(atomstring, "moov.udta.%s.metadata", atom);    GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);      }    }    if (valSize > 0)    {        *value = (char*)malloc((valSize+1)*sizeof(unsigned char));        memset(*value, 0, (valSize+1)*sizeof(unsigned char));        memcpy(*value, val, valSize*sizeof(unsigned char));        return true;    }     return false;}bool MP4File::SetMetadataName(const char* value){  return SetMetadataString("\251nam", value);}bool MP4File::GetMetadataName(char** value){  return GetMetadataString("\251nam", value, true);}bool MP4File::DeleteMetadataName(){  return DeleteMetadataAtom("\251nam", true);}bool MP4File::SetMetadataWriter(const char* value){  return SetMetadataString("\251wrt", value);}bool MP4File::GetMetadataWriter(char** value){  return GetMetadataString("\251wrt", value);}bool MP4File::DeleteMetadataWriter(){  return DeleteMetadataAtom("\251wrt");}bool MP4File::SetMetadataAlbum(const char* value){  return SetMetadataString("\251alb", value);}bool MP4File::GetMetadataAlbum(char** value){  return GetMetadataString("\251alb", value);}bool MP4File::DeleteMetadataAlbum(){  return DeleteMetadataAtom("\251alb");}bool MP4File::SetMetadataArtist(const char* value){  return SetMetadataString("\251ART", value);}bool MP4File::GetMetadataArtist(char** value){  return GetMetadataString("\251ART", value);}bool MP4File::DeleteMetadataArtist(){  return DeleteMetadataAtom("\251ART");}bool MP4File::SetMetadataTool(const char* value){  return SetMetadataString("\251too", value);}bool MP4File::GetMetadataTool(char** value){  return GetMetadataString("\251too", value);}bool MP4File::DeleteMetadataTool(){  return DeleteMetadataAtom("\251too");}bool MP4File::SetMetadataComment(const char* value){  return SetMetadataString("\251cmt", value);}bool MP4File::GetMetadataComment(char** value){  return GetMetadataString("\251cmt", value, true);}bool MP4File::DeleteMetadataComment(){  return DeleteMetadataAtom("\251cmt", true);}bool MP4File::SetMetadataYear(const char* value){  //if (strlen(value) != 4) return false;  return SetMetadataString("\251day", value);}bool MP4File::GetMetadataYear(char** value){  return GetMetadataString("\251day", value);}bool MP4File::DeleteMetadataYear(){  return DeleteMetadataAtom("\251day");}bool MP4File::SetMetadataTrack(u_int16_t track, u_int16_t totalTracks){    unsigned char t[9];    const char *s = "moov.udta.meta.ilst.trkn.data";    MP4BytesProperty *pMetadataProperty = NULL;    MP4Atom *pMetaAtom = NULL;        pMetaAtom = m_pRootAtom->FindAtom(s);    if (!pMetaAtom)    {        if (!CreateMetadataAtom("trkn"))            return false;        pMetaAtom = m_pRootAtom->FindAtom(s);    }    memset(t, 0, 9*sizeof(unsigned char));    t[2] = (unsigned char)(track>>8)&0xFF;    t[3] = (unsigned char)(track)&0xFF;    t[4] = (unsigned char)(totalTracks>>8)&0xFF;    t[5] = (unsigned char)(totalTracks)&0xFF;    pMetaAtom->FindProperty("data.metadata", (MP4Property**)&pMetadataProperty);    ASSERT(pMetadataProperty);    pMetadataProperty->SetValue((u_int8_t*)t, 8);    return true;}bool MP4File::GetMetadataTrack(u_int16_t* track, u_int16_t* totalTracks){    unsigned char *val = NULL;    u_int32_t valSize = 0;    const char *s = "moov.udta.meta.ilst.trkn.data.metadata";    *track = 0;    *totalTracks = 0;    GetBytesProperty(s, (u_int8_t**)&val, &valSize);    if (valSize == 8) {      *track = (u_int16_t)(val[3]);      *track += (u_int16_t)(val[2]<<8);      *totalTracks = (u_int16_t)(val[5]);      *totalTracks += (u_int16_t)(val[4]<<8);            return true;    }     return false;}bool MP4File::DeleteMetadataTrack(){  return DeleteMetadataAtom("trkn");}bool MP4File::SetMetadataDisk(u_int16_t disk, u_int16_t totalDisks){    unsigned char t[7];    const char *s = "moov.udta.meta.ilst.disk.data";    MP4BytesProperty *pMetadataProperty = NULL;    MP4Atom *pMetaAtom = NULL;        pMetaAtom = m_pRootAtom->FindAtom(s);    if (!pMetaAtom)    {        if (!CreateMetadataAtom("disk"))            return false;        pMetaAtom = m_pRootAtom->FindAtom(s);    }    memset(t, 0, 7*sizeof(unsigned char));    t[2] = (unsigned char)(disk>>8)&0xFF;    t[3] = (unsigned char)(disk)&0xFF;    t[4] = (unsigned char)(totalDisks>>8)&0xFF;    t[5] = (unsigned char)(totalDisks)&0xFF;    pMetaAtom->FindProperty("data.metadata", (MP4Property**)&pMetadataProperty);    ASSERT(pMetadataProperty);    pMetadataProperty->SetValue((u_int8_t*)t, 6);    return true;}bool MP4File::GetMetadataDisk(u_int16_t* disk, u_int16_t* totalDisks){    unsigned char *val = NULL;    u_int32_t valSize = 0;    const char *s = "moov.udta.meta.ilst.disk.data.metadata";    *disk = 0;    *totalDisks = 0;    GetBytesProperty(s, (u_int8_t**)&val, &valSize);    if (valSize == 6 || valSize == 8) {      *disk = (u_int16_t)(val[3]);      *disk += (u_int16_t)(val[2]<<8);      *totalDisks = (u_int16_t)(val[5]);      *totalDisks += (u_int16_t)(val[4]<<8);      return true;    }    return true;}bool MP4File::DeleteMetadataDisk(){  return DeleteMetadataAtom("disk");}static const char* ID3v1GenreList[] = {    "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",    "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies",    "Other", "Pop", "R&B", "Rap", "Reggae", "Rock",    "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks",    "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",    "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House",    "Game", "Sound Clip", "Gospel", "Noise", "AlternRock", "Bass",    "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock",    "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",    "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",    "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",    "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi",    "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical",    "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing",    "Fast-Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",    "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",    "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",    "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus",    "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",    "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet",    "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",    "Goa", "Drum & Bass", "Club House", "Hardcore", "Terror",    "Indie", "BritPop", "NegerPunk", "Polsk Punk", "Beat",    "Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary C",    "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop",    "SynthPop",};int GenreToString(char** GenreStr, const int genre){  if (genre > 0 &&       genre <= (int)(sizeof(ID3v1GenreList)/sizeof(*ID3v1GenreList)))    {        *GenreStr = (char*)malloc((strlen(ID3v1GenreList[genre-1])+1)*sizeof(char));        memset(*GenreStr, 0, (strlen(ID3v1GenreList[genre-1])+1)*sizeof(char));        strcpy(*GenreStr, ID3v1GenreList[genre-1]);        return 0;    } else {        *GenreStr = (char*)malloc(2*sizeof(char));        memset(*GenreStr, 0, 2*sizeof(char));        return 1;    }}int StringToGenre(const char* GenreStr){    unsigned int i;    for (i = 0; i < sizeof(ID3v1GenreList)/sizeof(*ID3v1GenreList); i++)    {        if (strcasecmp(GenreStr, ID3v1GenreList[i]) == 0)            return i+1;    }    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?