MP4::File::File(const char *file, bool readProperties, TagLib::AudioProperties::ReadStyle propertiesStyle, MP4FileHandle handle) : TagLib::File(file), mp4tag(NULL), properties(NULL) { // debug ("MP4::File: create new file object."); //debug ( file ); /** * Create the MP4 file. */ if(handle == MP4_INVALID_FILE_HANDLE) { mp4file = MP4Read(file); } else { mp4file = handle; } if( isOpen() ) { read(readProperties, propertiesStyle ); } }
main(int argc, char** argv) { if (argc < 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); exit(1); } u_int32_t verbosity = 0 /* MP4_DETAILS_ALL */; MP4FileHandle mp4File = MP4Create(argv[1], verbosity); if (!mp4File) { exit(1); } MP4TrackId urlTrackId = MP4AddTrack(mp4File, "URLF"); printf("urlTrackId %d\n", urlTrackId); u_int8_t i; char url[128]; for (i = 1; i <= 5; i++) { sprintf(url, "http://server.com/foo/bar%u.html", i); MP4WriteSample(mp4File, urlTrackId, (u_int8_t*)url, strlen(url) + 1, (MP4Duration)i); } MP4Close(mp4File); mp4File = MP4Read(argv[1], verbosity); // check that we can find the track again urlTrackId = MP4FindTrackId(mp4File, 0, "URLF"); printf("urlTrackId %d\n", urlTrackId); for (i = 1; i <= 5; i++) { u_int8_t* pSample = NULL; u_int32_t sampleSize = 0; MP4Duration duration; bool rc; rc = MP4ReadSample(mp4File, urlTrackId, i, &pSample, &sampleSize, NULL, &duration); if (rc) { printf("Sample %i duration "D64": %s\n", i, duration, pSample); free(pSample); } else { printf("Couldn't read sample %i\n", i); } } MP4Close(mp4File); exit(0); }
bool ArtUtility::actionList( JobContext& job ) { ostringstream report; const int widx = 3; const int wsize = 8; const int wtype = 9; const string sep = " "; if( _jobCount == 0 ) { report << setw(widx) << right << "IDX" << left << sep << setw(wsize) << right << "BYTES" << left << sep << setw(8) << "CRC32" << sep << setw(wtype) << "TYPE" << sep << setw(0) << "FILE" << '\n'; report << setfill('-') << setw(70) << "" << setfill(' ') << '\n'; } job.fileHandle = MP4Read( job.file.c_str() ); if( job.fileHandle == MP4_INVALID_FILE_HANDLE ) return herrf( "unable to open for read: %s\n", job.file.c_str() ); CoverArtBox::ItemList items; if( CoverArtBox::list( job.fileHandle, items )) return herrf( "unable to get list of covr-box: %s\n", job.file.c_str() ); int line = 0; const CoverArtBox::ItemList::size_type max = items.size(); for( CoverArtBox::ItemList::size_type i = 0; i < max; i++ ) { if( _artFilter != numeric_limits<uint32_t>::max() && _artFilter != i ) continue; CoverArtBox::Item& item = items[i]; const uint32_t crc = crc32( item.buffer, item.size ); report << setw(widx) << right << i << sep << setw(wsize) << item.size << sep << setw(8) << setfill('0') << hex << crc << setfill(' ') << dec << sep << setw(wtype) << left << enumBasicType.toString( item.type ); if( line++ == 0 ) report << sep << setw(0) << job.file; report << '\n'; } verbose1f( "%s", report.str().c_str() ); return SUCCESS; }
MP4Reader(const std::string &file_path) : time_scale(9 * MP4_MSECS_TIME_SCALE) , file_path(file_path) , handle(MP4_INVALID_FILE_HANDLE) , video_track_id(MP4_INVALID_TRACK_ID) , next_video_sample_idx(1) , video_sample(nullptr) , video_timescale(0) , video_sample_max_size(0) , video_sample_number(0) , video_duration(0) , pSeqHeaders(nullptr) , pSeqHeaderSize(nullptr) , pPictHeaders(nullptr) , pPictHeaderSize(nullptr) { handle = MP4Read(this->file_path.c_str()); video_track_id = MP4FindTrackId(handle, 0, MP4_VIDEO_TRACK_TYPE); if (video_track_id != MP4_INVALID_TRACK_ID) { video_timescale = MP4GetTrackTimeScale(handle, video_track_id); video_sample_max_size = MP4GetTrackMaxSampleSize(handle, video_track_id) * 2; video_duration = MP4GetTrackDuration(handle, video_track_id); video_sample = new unsigned char[video_sample_max_size]; video_sample_number = MP4GetTrackNumberOfSamples(handle, video_track_id); if (MP4GetTrackH264SeqPictHeaders(handle, video_track_id, &pSeqHeaders, &pSeqHeaderSize, &pPictHeaders, &pPictHeaderSize)) { printf("Get SPS(%d) and PPS(%d)\n", *pSeqHeaderSize, *pPictHeaderSize); for(int i = 0; (pSeqHeaders[i] && pSeqHeaderSize[i]); i++) { printf("SPS(%d): %02x %02x %02x %02x %02x\n", i, pSeqHeaders[i][0], pSeqHeaders[i][1], pSeqHeaders[i][2], pSeqHeaders[i][3], pSeqHeaders[i][4]); } for(int i = 0; (pPictHeaders[i] && pPictHeaderSize[i]); i++) { printf("PPS(%d): %02x %02x %02x %02x %02x\n", i, pPictHeaders[i][0], pPictHeaders[i][1], pPictHeaders[i][2], pPictHeaders[i][3], pPictHeaders[i][4]); } } } }
void CAVInfo::ReadMP4(const char *file) { MP4FileHandle mp4File = MP4Read(file, MP4_DETAILS_ERROR); if ( !mp4File ) { return; } uint32_t vsize; uint8_t *value; char *pname = "moov.uuid.data"; MP4GetBytesProperty(mp4File, pname, &value, &vsize); if ( vsize ) { MP4_moov_uuid_parse(value, vsize, m_title); free(value); } MP4Close(mp4File); }
int create_media_for_mp4_file (CPlayerSession *psptr, const char *name, int have_audio_driver, control_callback_vft_t *cc_vft) { MP4FileHandle fh; CMp4File *Mp4File1; fh = MP4Read(name, MP4_DETAILS_ERROR); // | MP4_DETAILS_READ | MP4_DETAILS_SAMPLE); if (!MP4_IS_VALID_FILE_HANDLE(fh)) { psptr->set_message("`%s\' is not an mp4 file", name); return -1; } Mp4File1 = new CMp4File(fh); // quicktime is searchable... psptr->set_media_close_callback(close_mp4_file, (void *)Mp4File1); psptr->session_set_seekable(1); int ret; ret = Mp4File1->create_media(psptr, have_audio_driver, cc_vft); if (ret <= 0) return ret; uint offset = 0; char errmsg[512]; uint32_t errlen = sizeof(errmsg) - 1; errmsg[0] = '\0'; if (Mp4File1->get_illegal_video_codec() != 0) { offset = snprintf(errmsg, errlen, "Unknown or unused Video tracks "); } if (have_audio_driver == 0) { offset += snprintf(errmsg + offset, errlen - offset, "%sNo Audio driver - can't play audio", offset == 0 ? "" : "and "); } else if (Mp4File1->get_illegal_audio_codec() != 0) { snprintf(errmsg + offset, errlen - offset, "%sUnknown or unused audio tracks", offset == 0 ? "" : "and "); } psptr->set_message(errmsg); return (1); }
extern "C" char* MP4FileInfo( const char* fileName, MP4TrackId trackId) { MP4FileHandle mp4File = MP4Read(fileName); if (!mp4File) { return NULL; } char* info = MP4Info(mp4File, trackId); MP4Close(mp4File); return info; // caller should free this }
int32_t Mp4FileDefaultAudio(const char* fileName) { MP4FileHandle mp4File = MP4Read(fileName); if (mp4File == MP4_INVALID_FILE_HANDLE) { return -1; } MP4TrackId trackId = MP4FindTrackId(mp4File, 0, MP4_AUDIO_TRACK_TYPE); MP4Close(mp4File); if (trackId == MP4_INVALID_TRACK_ID) { return -1; } return trackId; }
int CMP4Tag::ReadMp4Tag(char *Filename) { MP4FileHandle MP4File; if(!(MP4File=MP4Read(Filename, 0))) { char buf[25+MAX_PATH+1]; sprintf(buf,"ReadMp4Tag: can't open \"%s\"",Filename); MessageBox(NULL,buf,NULL,MB_OK); return 1; } FREE_ARRAY(copyright); MP4GetMetadataTool(MP4File, ©right); FREE_ARRAY(artist); MP4GetMetadataArtist(MP4File, &artist); FREE_ARRAY(writer); MP4GetMetadataWriter(MP4File, &writer); FREE_ARRAY(title); MP4GetMetadataName(MP4File, &title); FREE_ARRAY(album); MP4GetMetadataAlbum(MP4File, &album); MP4GetMetadataTrack(MP4File, (unsigned __int16 *)&trackno, (unsigned __int16 *)&ntracks); MP4GetMetadataDisk(MP4File, (unsigned __int16 *)&discno, (unsigned __int16 *)&ndiscs); MP4GetMetadataCompilation(MP4File, (unsigned __int8 *)&compilation); FREE_ARRAY(year); MP4GetMetadataYear(MP4File, &year); FREE_ARRAY(genre); MP4GetMetadataGenre(MP4File, &genre); FREE_ARRAY(comment); MP4GetMetadataComment(MP4File, &comment); FREE_ARRAY(art.data); MP4GetMetadataCoverArt(MP4File, (unsigned __int8 **)&art.data, (u_int32_t *)&art.size); MP4Close(MP4File); /* FILE *f=fopen("D:\\prova.jpg","wb"); fwrite(artFile,1,artSize,f); fclose(f);*/ return 0; }
int aac_decode(aac_dec_opt *opt) { int result; int def_srate = 0; int outfile_set = 0; int mp4file = 0; char *fnp; char audioFileName[MAX_PATH]; MP4FileHandle infile; /* point to the specified file name */ strcpy(audioFileName, opt->filename); fnp = (char *)strrchr(audioFileName,'.'); if (fnp) fnp[0] = '\0'; strcat(audioFileName, file_ext[opt->file_type]); mp4file = 1; infile = MP4Read(audioFileName, 0); if (!infile) mp4file = 0; if (infile) MP4Close(infile); if (mp4file) { result = decodeMP4file(audioFileName, opt); } else { result = decodeAACfile(audioFileName, def_srate, opt); } return 0; }
bool mediaM4A(Artwork *art, const char *filePath) { MP4FileHandle mp4file = MP4Read(filePath); if (mp4file == MP4_INVALID_FILE_HANDLE) { return false; } uint16_t numvalue, numvalue2; char *value; u_int8_t numbool; art->filetype = FILETYPE_M4A; if (MP4GetMetadataArtist(mp4file, &value) && value != NULL) { art->artist = (QString::fromUtf8(value)).trimmed(); free(value); } if (MP4GetMetadataYear(mp4file, &value) && value != NULL) { art->year = QString(value).toInt(); free(value); } if (MP4GetMetadataAlbum(mp4file, &value) && value != NULL) { art->album = (QString::fromUtf8(value)).trimmed(); free(value); } if (MP4GetMetadataName(mp4file, &value) && value != NULL) { art->track = (QString::fromUtf8(value)).trimmed(); free(value); } if (MP4GetMetadataWriter(mp4file, &value) && value != NULL) { // composer free(value); } if (MP4GetMetadataTrack(mp4file, &numvalue, &numvalue2)) { art->trackNo = numvalue; art->trackCount = numvalue2; } if (MP4GetMetadataDisk(mp4file, &numvalue, &numvalue2)) { art->discNo = numvalue; art->discCount = numvalue2; } if (MP4GetMetadataGenre(mp4file, &value) && value != NULL) { art->genre = (QString::fromUtf8(value)).trimmed(); free(value); } if (MP4GetMetadataCompilation(mp4file, &numbool)) { art->type = numbool ? ARTWORK_COMPILATION : ARTWORK_NORMAL; } /* art->bitrate = 0; art->sampleRate = 0; art->channels = 0; */ MP4Duration duration = MP4GetDuration(mp4file); if (duration != MP4_INVALID_DURATION) { u_int64_t timescale = MP4GetTimeScale(mp4file); u_int64_t msectime = (duration * (u_int64_t) 1000) / timescale; u_int64_t sectime, mintime, hrtime; if (msectime == 0) { hrtime = mintime = sectime = (u_int64_t) 0; } else { hrtime = msectime / (u_int64_t) (3600 * 1000); msectime -= hrtime * (u_int64_t) (3600 * 1000); mintime = msectime / (u_int64_t) (60 * 1000); msectime -= (mintime * (u_int64_t) (60 * 1000)); sectime = msectime / (u_int64_t) (1000); msectime -= sectime * (u_int64_t)(1000); } art->duration = hrtime * 3600 + mintime * 60 + sectime; } art->makeSearchable(); if (!art->hasCover()) { uint8_t *cover = NULL; uint32_t cover_size; if (MP4GetMetadataCoverArt(mp4file, &cover, &cover_size)) { if (saveImage(art, (const char*) cover, cover_size)) { qDebug() << "MP4: invalid image in " << filePath; } } if (cover) { free(cover); } } MP4Close(mp4file); return true; }
int decodeMP4file(char *sndfile, aac_dec_opt *opt) { int track; unsigned long samplerate; unsigned char channels; void *sample_buffer; MP4FileHandle infile; MP4SampleId sampleId, numSamples; audio_file *aufile; faacDecHandle hDecoder; faacDecFrameInfo frameInfo; unsigned char *buffer; int buffer_size; int first_time = 1; hDecoder = faacDecOpen(); infile = MP4Read(opt->filename, 0); if (!infile) { /* unable to open file */ error_handler("Error opening file: %s\n", opt->filename); return 1; } if ((track = GetAACTrack(infile)) < 0) { error_handler("Unable to find correct AAC sound track in the MP4 file.\n"); MP4Close(infile); return 1; } buffer = NULL; buffer_size = 0; MP4GetTrackESConfiguration(infile, track, &buffer, &buffer_size); if(faacDecInit2(hDecoder, buffer, buffer_size, &samplerate, &channels) < 0) { /* If some error initializing occured, skip the file */ error_handler("Error initializing decoder library.\n"); faacDecClose(hDecoder); MP4Close(infile); return 1; } if (buffer) free(buffer); numSamples = MP4GetTrackNumberOfSamples(infile, track); for (sampleId = 1; sampleId <= numSamples; sampleId++) { int rc; /* get access unit from MP4 file */ buffer = NULL; buffer_size = 0; rc = MP4ReadSample(infile, track, sampleId, &buffer, &buffer_size, NULL, NULL, NULL, NULL); if (rc == 0) { error_handler("Reading from MP4 file failed.\n"); faacDecClose(hDecoder); MP4Close(infile); return 1; } sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer, buffer_size); if (buffer) free(buffer); opt->progress_update((long)numSamples, sampleId); /* open the sound file now that the number of channels are known */ if (first_time && !frameInfo.error) { if(opt->decode_mode == 0) { if (Set_WIN_Params (INVALID_FILEDESC, samplerate, SAMPLE_SIZE, frameInfo.channels) < 0) { error_handler("\nCan't access %s\n", "WAVE OUT"); faacDecClose(hDecoder); MP4Close(infile); return (0); } } else { aufile = open_audio_file(sndfile, samplerate, frameInfo.channels, opt->output_format, opt->file_type, aacChannelConfig2wavexChannelMask(&frameInfo)); if (aufile == NULL) { faacDecClose(hDecoder); MP4Close(infile); return 0; } } first_time = 0; } if ((frameInfo.error == 0) && (frameInfo.samples > 0)) { if(opt->decode_mode == 0) WIN_Play_Samples((short*)sample_buffer, frameInfo.channels*frameInfo.samples); else write_audio_file(aufile, sample_buffer, frameInfo.samples, 0); } if (frameInfo.error > 0) { error_handler("Error: %s\n", faacDecGetErrorMessage(frameInfo.error)); break; } if(stop_decoding) break; } faacDecClose(hDecoder); MP4Close(infile); if(opt->decode_mode == 0) WIN_Audio_close(); else { if (!first_time) close_audio_file(aufile); } return frameInfo.error; }
bool MP4Metadata::ReadMetadata(CFErrorRef *error) { // Start from scratch CFDictionaryRemoveAllValues(mMetadata); CFDictionaryRemoveAllValues(mChangedMetadata); UInt8 buf [PATH_MAX]; if(!CFURLGetFileSystemRepresentation(mURL, FALSE, buf, PATH_MAX)) return false; // Open the file for reading MP4FileHandle file = MP4Read(reinterpret_cast<const char *>(buf)); if(MP4_INVALID_FILE_HANDLE == file) { if(error) { CFMutableDictionaryRef errorDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 32, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFStringRef displayName = CreateDisplayNameForURL(mURL); CFStringRef errorString = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFCopyLocalizedString(CFSTR("The file “%@” is not a valid MPEG-4 file."), ""), displayName); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedDescriptionKey, errorString); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedFailureReasonKey, CFCopyLocalizedString(CFSTR("Not an MPEG-4 file"), "")); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedRecoverySuggestionKey, CFCopyLocalizedString(CFSTR("The file's extension may not match the file's type."), "")); CFRelease(errorString), errorString = NULL; CFRelease(displayName), displayName = NULL; *error = CFErrorCreate(kCFAllocatorDefault, AudioMetadataErrorDomain, AudioMetadataFileFormatNotRecognizedError, errorDictionary); CFRelease(errorDictionary), errorDictionary = NULL; } return false; } // Read the properties if(0 < MP4GetNumberOfTracks(file)) { // Should be type 'soun', media data name'mp4a' MP4TrackId trackID = MP4FindTrackId(file, 0); // Verify this is an MPEG-4 audio file if(MP4_INVALID_TRACK_ID == trackID || strncmp("soun", MP4GetTrackType(file, trackID), 4)) { MP4Close(file), file = NULL; if(error) { CFMutableDictionaryRef errorDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 32, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFStringRef displayName = CreateDisplayNameForURL(mURL); CFStringRef errorString = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFCopyLocalizedString(CFSTR("The file “%@” is not a valid MPEG-4 file."), ""), displayName); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedDescriptionKey, errorString); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedFailureReasonKey, CFCopyLocalizedString(CFSTR("Not an MPEG-4 file"), "")); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedRecoverySuggestionKey, CFCopyLocalizedString(CFSTR("The file's extension may not match the file's type."), "")); CFRelease(errorString), errorString = NULL; CFRelease(displayName), displayName = NULL; *error = CFErrorCreate(kCFAllocatorDefault, AudioMetadataErrorDomain, AudioMetadataFileFormatNotSupportedError, errorDictionary); CFRelease(errorDictionary), errorDictionary = NULL; } return false; } MP4Duration mp4Duration = MP4GetTrackDuration(file, trackID); uint32_t mp4TimeScale = MP4GetTrackTimeScale(file, trackID); CFNumberRef totalFrames = CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, &mp4Duration); CFDictionarySetValue(mMetadata, kPropertiesTotalFramesKey, totalFrames); CFRelease(totalFrames), totalFrames = NULL; CFNumberRef sampleRate = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &mp4TimeScale); CFDictionarySetValue(mMetadata, kPropertiesSampleRateKey, sampleRate); CFRelease(sampleRate), sampleRate = NULL; double length = static_cast<double>(mp4Duration / mp4TimeScale); CFNumberRef duration = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &length); CFDictionarySetValue(mMetadata, kPropertiesDurationKey, duration); CFRelease(duration), duration = NULL; // "mdia.minf.stbl.stsd.*[0].channels" int channels = MP4GetTrackAudioChannels(file, trackID); CFNumberRef channelsPerFrame = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &channels); CFDictionaryAddValue(mMetadata, kPropertiesChannelsPerFrameKey, channelsPerFrame); CFRelease(channelsPerFrame), channelsPerFrame = NULL; // ALAC files if(MP4HaveTrackAtom(file, trackID, "mdia.minf.stbl.stsd.alac")) { CFDictionarySetValue(mMetadata, kPropertiesFormatNameKey, CFSTR("Apple Lossless")); uint64_t sampleSize; uint8_t *decoderConfig; uint32_t decoderConfigSize; if(MP4GetTrackBytesProperty(file, trackID, "mdia.minf.stbl.stsd.alac.alac.decoderConfig", &decoderConfig, &decoderConfigSize) && 28 <= decoderConfigSize) { // The ALAC magic cookie seems to have the following layout (28 bytes, BE): // Byte 10: Sample size // Bytes 25-28: Sample rate CFNumberRef bitsPerChannel = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, decoderConfig + 9); CFDictionaryAddValue(mMetadata, kPropertiesBitsPerChannelKey, bitsPerChannel); CFRelease(bitsPerChannel), bitsPerChannel = NULL; double losslessBitrate = static_cast<double>(mp4TimeScale * channels * sampleSize) / 1000; CFNumberRef bitrate = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &losslessBitrate); CFDictionarySetValue(mMetadata, kPropertiesBitrateKey, bitrate); CFRelease(bitrate), bitrate = NULL; free(decoderConfig), decoderConfig = NULL; } else if(MP4GetTrackIntegerProperty(file, trackID, "mdia.minf.stbl.stsd.alac.sampleSize", &sampleSize)) { CFNumberRef bitsPerChannel = CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, &sampleSize); CFDictionaryAddValue(mMetadata, kPropertiesBitsPerChannelKey, bitsPerChannel); CFRelease(bitsPerChannel), bitsPerChannel = NULL; double losslessBitrate = static_cast<double>(mp4TimeScale * channels * sampleSize) / 1000; CFNumberRef bitrate = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &losslessBitrate); CFDictionarySetValue(mMetadata, kPropertiesBitrateKey, bitrate); CFRelease(bitrate), bitrate = NULL; } } // AAC files if(MP4HaveTrackAtom(file, trackID, "mdia.minf.stbl.stsd.mp4a")) { CFDictionarySetValue(mMetadata, kPropertiesFormatNameKey, CFSTR("AAC")); // "mdia.minf.stbl.stsd.*.esds.decConfigDescr.avgBitrate" uint32_t trackBitrate = MP4GetTrackBitRate(file, trackID) / 1000; CFNumberRef bitrate = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &trackBitrate); CFDictionaryAddValue(mMetadata, kPropertiesBitrateKey, bitrate); CFRelease(bitrate), bitrate = NULL; } } // No valid tracks in file else { MP4Close(file), file = NULL; if(error) { CFMutableDictionaryRef errorDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 32, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFStringRef displayName = CreateDisplayNameForURL(mURL); CFStringRef errorString = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFCopyLocalizedString(CFSTR("The file “%@” is not a valid MPEG-4 file."), ""), displayName); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedDescriptionKey, errorString); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedFailureReasonKey, CFCopyLocalizedString(CFSTR("Not an MPEG-4 file"), "")); CFDictionarySetValue(errorDictionary, kCFErrorLocalizedRecoverySuggestionKey, CFCopyLocalizedString(CFSTR("The file's extension may not match the file's type."), "")); CFRelease(errorString), errorString = NULL; CFRelease(displayName), displayName = NULL; *error = CFErrorCreate(kCFAllocatorDefault, AudioMetadataErrorDomain, AudioMetadataFileFormatNotSupportedError, errorDictionary); CFRelease(errorDictionary), errorDictionary = NULL; } return false; } // Read the tags const MP4Tags *tags = MP4TagsAlloc(); if(NULL == tags) { MP4Close(file), file = NULL; if(error) *error = CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainPOSIX, ENOMEM, NULL); return false; } MP4TagsFetch(tags, file); // Album title if(tags->album) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->album, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataAlbumTitleKey, str); CFRelease(str), str = NULL; } // Artist if(tags->artist) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->artist, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataArtistKey, str); CFRelease(str), str = NULL; } // Album Artist if(tags->albumArtist) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->albumArtist, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataAlbumArtistKey, str); CFRelease(str), str = NULL; } // Genre if(tags->genre) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->genre, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataGenreKey, str); CFRelease(str), str = NULL; } // Release date if(tags->releaseDate) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->releaseDate, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataReleaseDateKey, str); CFRelease(str), str = NULL; } // Composer if(tags->composer) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->composer, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataComposerKey, str); CFRelease(str), str = NULL; } // Comment if(tags->comments) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->comments, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataCommentKey, str); CFRelease(str), str = NULL; } // Track title if(tags->name) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->name, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataTitleKey, str); CFRelease(str), str = NULL; } // Track number if(tags->track) { if(tags->track->index) { CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &tags->track->index); CFDictionarySetValue(mMetadata, kMetadataTrackNumberKey, num); CFRelease(num), num = NULL; } if(tags->track->total) { CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &tags->track->total); CFDictionarySetValue(mMetadata, kMetadataTrackTotalKey, num); CFRelease(num), num = NULL; } } // Disc number if(tags->disk) { if(tags->disk->index) { CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &tags->disk->index); CFDictionarySetValue(mMetadata, kMetadataDiscNumberKey, num); CFRelease(num), num = NULL; } if(tags->disk->total) { CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &tags->disk->total); CFDictionarySetValue(mMetadata, kMetadataDiscTotalKey, num); CFRelease(num), num = NULL; } } // Compilation if(tags->compilation) CFDictionarySetValue(mMetadata, kMetadataCompilationKey, *(tags->compilation) ? kCFBooleanTrue : kCFBooleanFalse); // BPM if(tags->tempo) { } // Lyrics if(tags->lyrics) { CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, tags->lyrics, kCFStringEncodingUTF8); CFDictionarySetValue(mMetadata, kMetadataLyricsKey, str); CFRelease(str), str = NULL; } // Album art if(tags->artworkCount) { for(uint32_t i = 0; i < tags->artworkCount; ++i) { CFDataRef data = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(tags->artwork[i].data), tags->artwork[i].size); CFDictionarySetValue(mMetadata, kAlbumArtFrontCoverKey, data); CFRelease(data), data = NULL; } } // ReplayGain // Reference loudness MP4ItmfItemList *items = MP4ItmfGetItemsByMeaning(file, "com.apple.iTunes", "replaygain_reference_loudness"); if(NULL != items) { float referenceLoudnessValue; if(1 <= items->size && 1 <= items->elements[0].dataList.size && sscanf(reinterpret_cast<const char *>(items->elements[0].dataList.elements[0].value), "%f", &referenceLoudnessValue)) { CFNumberRef referenceLoudness = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &referenceLoudnessValue); CFDictionaryAddValue(mMetadata, kReplayGainReferenceLoudnessKey, referenceLoudness); CFRelease(referenceLoudness), referenceLoudness = NULL; } MP4ItmfItemListFree(items), items = NULL; } // Track gain items = MP4ItmfGetItemsByMeaning(file, "com.apple.iTunes", "replaygain_track_gain"); if(NULL != items) { float trackGainValue; if(1 <= items->size && 1 <= items->elements[0].dataList.size && sscanf(reinterpret_cast<const char *>(items->elements[0].dataList.elements[0].value), "%f", &trackGainValue)) { CFNumberRef trackGain = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &trackGainValue); CFDictionaryAddValue(mMetadata, kReplayGainTrackGainKey, trackGain); CFRelease(trackGain), trackGain = NULL; } MP4ItmfItemListFree(items), items = NULL; } // Track peak items = MP4ItmfGetItemsByMeaning(file, "com.apple.iTunes", "replaygain_track_peak"); if(NULL != items) { float trackPeakValue; if(1 <= items->size && 1 <= items->elements[0].dataList.size && sscanf(reinterpret_cast<const char *>(items->elements[0].dataList.elements[0].value), "%f", &trackPeakValue)) { CFNumberRef trackPeak = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &trackPeakValue); CFDictionaryAddValue(mMetadata, kReplayGainTrackPeakKey, trackPeak); CFRelease(trackPeak), trackPeak = NULL; } MP4ItmfItemListFree(items), items = NULL; } // Album gain items = MP4ItmfGetItemsByMeaning(file, "com.apple.iTunes", "replaygain_album_gain"); if(NULL != items) { float albumGainValue; if(1 <= items->size && 1 <= items->elements[0].dataList.size && sscanf(reinterpret_cast<const char *>(items->elements[0].dataList.elements[0].value), "%f", &albumGainValue)) { CFNumberRef albumGain = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &albumGainValue); CFDictionaryAddValue(mMetadata, kReplayGainAlbumGainKey, albumGain); CFRelease(albumGain), albumGain = NULL; } MP4ItmfItemListFree(items), items = NULL; } // Album peak items = MP4ItmfGetItemsByMeaning(file, "com.apple.iTunes", "replaygain_album_peak"); if(NULL != items) { float albumPeakValue; if(1 <= items->size && 1 <= items->elements[0].dataList.size && sscanf(reinterpret_cast<const char *>(items->elements[0].dataList.elements[0].value), "%f", &albumPeakValue)) { CFNumberRef albumPeak = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &albumPeakValue); CFDictionaryAddValue(mMetadata, kReplayGainAlbumPeakKey, albumPeak); CFRelease(albumPeak), albumPeak = NULL; } MP4ItmfItemListFree(items), items = NULL; } // Clean up MP4TagsFree(tags), tags = NULL; MP4Close(file), file = NULL; return true; }
int AacPcm::getInfos(MediaInfo *infos) { if(!infos) return 1; if(hDecoder) { SHOW_INFO() return 0; } IsAAC=strcmpi(infos->getFilename()+lstrlen(infos->getFilename())-4,".aac")==0; if(!IsAAC) // MP4 file --------------------------------------------------------------------- { MP4Duration length; unsigned __int32 buffer_size; mp4AudioSpecificConfig mp4ASC; if(!(mp4File=MP4Read(infos->getFilename(), 0))) ERROR_getInfos("Error opening file"); if((track=GetAACTrack(mp4File))<0) ERROR_getInfos(0); //"Unable to find correct AAC sound track"); if(!(hDecoder=faacDecOpen())) ERROR_getInfos("Error initializing decoder library"); MP4GetTrackESConfiguration(mp4File, track, (unsigned __int8 **)&buffer, &buffer_size); if(!buffer) ERROR_getInfos("MP4GetTrackESConfiguration"); AudioSpecificConfig(buffer, buffer_size, &mp4ASC); Channels=mp4ASC.channelsConfiguration; if(faacDecInit2(hDecoder, buffer, buffer_size, &Samplerate, &Channels) < 0) ERROR_getInfos("Error initializing decoder library"); FREE_ARRAY(buffer); length=MP4GetTrackDuration(mp4File, track); len_ms=(DWORD)MP4ConvertFromTrackDuration(mp4File, track, length, MP4_MSECS_TIME_SCALE); file_info.bitrate=MP4GetTrackBitRate(mp4File, track); file_info.version=MP4GetTrackAudioType(mp4File, track)==MP4_MPEG4_AUDIO_TYPE ? 4 : 2; numSamples=MP4GetTrackNumberOfSamples(mp4File, track); sampleId=1; } else // AAC file ------------------------------------------------------------------------------ { DWORD read, tmp; BYTE Channels4Raw=0; if(!(aacFile=fopen(infos->getFilename(),"rb"))) ERROR_getInfos("Error opening file"); // use bufferized stream setvbuf(aacFile,NULL,_IOFBF,32767); // get size of file fseek(aacFile, 0, SEEK_END); src_size=ftell(aacFile); fseek(aacFile, 0, SEEK_SET); if(!(buffer=(BYTE *)malloc(FAAD_STREAMSIZE))) ERROR_getInfos("Memory allocation error: buffer") tmp=src_size<FAAD_STREAMSIZE ? src_size : FAAD_STREAMSIZE; read=fread(buffer, 1, tmp, aacFile); if(read==tmp) { bytes_read=read; bytes_into_buffer=read; } else ERROR_getInfos("Read failed!") if(tagsize=id3v2_tag(buffer)) { if(tagsize>(long)src_size) ERROR_getInfos("Corrupt stream!"); if(tagsize<bytes_into_buffer) { bytes_into_buffer-=tagsize; memcpy(buffer,buffer+tagsize,bytes_into_buffer); } else { bytes_read=tagsize; bytes_into_buffer=0; if(tagsize>bytes_into_buffer) fseek(aacFile, tagsize, SEEK_SET); } if(src_size<bytes_read+FAAD_STREAMSIZE-bytes_into_buffer) tmp=src_size-bytes_read; else tmp=FAAD_STREAMSIZE-bytes_into_buffer; read=fread(buffer+bytes_into_buffer, 1, tmp, aacFile); if(read==tmp) { bytes_read+=read; bytes_into_buffer+=read; } else ERROR_getInfos("Read failed!"); } if(get_AAC_format((char *)infos->getFilename(), &file_info, &seek_table, &seek_table_length, 0)) ERROR_getInfos("get_AAC_format"); IsSeekable=file_info.headertype==ADTS && seek_table && seek_table_length>0; BlockSeeking=!IsSeekable; if(!(hDecoder=faacDecOpen())) ERROR_getInfos("Can't open library"); if(file_info.headertype==RAW) { faacDecConfiguration config; config.defSampleRate=atoi(cfg_samplerate); switch(cfg_profile[1]) { case 'a': config.defObjectType=MAIN; break; case 'o': config.defObjectType=LOW; break; case 'S': config.defObjectType=SSR; break; case 'T': config.defObjectType=LTP; break; } switch(cfg_bps[0]) { case '1': config.outputFormat=FAAD_FMT_16BIT; break; case '2': config.outputFormat=FAAD_FMT_24BIT; break; case '3': config.outputFormat=FAAD_FMT_32BIT; break; case 'F': config.outputFormat=FAAD_FMT_24BIT; break; } faacDecSetConfiguration(hDecoder, &config); if(!FindBitrate) { AacPcm *NewInst; if(!(NewInst=new AacPcm())) ERROR_getInfos("Memory allocation error: NewInst"); NewInst->FindBitrate=TRUE; if(NewInst->getInfos(infos)) ERROR_getInfos(0); Channels4Raw=NewInst->frameInfo.channels; file_info.bitrate=NewInst->file_info.bitrate*Channels4Raw; delete NewInst; } else { DWORD Samples, BytesConsumed; if((bytes_consumed=faacDecInit(hDecoder,buffer,bytes_into_buffer,&Samplerate,&Channels))<0) ERROR_getInfos("Can't init library"); bytes_into_buffer-=bytes_consumed; if(!processData(infos,0,0)) ERROR_getInfos(0); Samples=frameInfo.samples/sizeof(short); BytesConsumed=frameInfo.bytesconsumed; processData(infos,0,0); if(BytesConsumed<frameInfo.bytesconsumed) BytesConsumed=frameInfo.bytesconsumed; file_info.bitrate=(BytesConsumed*8*Samplerate)/Samples; if(!file_info.bitrate) file_info.bitrate=1000; // try to continue decoding return 0; } } if((bytes_consumed=faacDecInit(hDecoder, buffer, bytes_into_buffer, &Samplerate, &Channels))<0) ERROR_getInfos("faacDecInit failed!") bytes_into_buffer-=bytes_consumed; if(Channels4Raw) Channels=Channels4Raw; len_ms=(DWORD)((1000*((float)src_size*8))/file_info.bitrate); } SHOW_INFO(); return 0; }
/** Action for exporting chapters from the <b>job.file</b> * * * @param job the job to process * @return mp4v2::util::SUCCESS if successful, mp4v2::util::FAILURE otherwise */ bool ChapterUtility::actionExport( JobContext& job ) { job.fileHandle = MP4Read( job.file.c_str() ); if( job.fileHandle == MP4_INVALID_FILE_HANDLE ) { return herrf( "unable to open for read: %s\n", job.file.c_str() ); } // get the list of chapters MP4Chapter_t* chapters = 0; uint32_t chapterCount = 0; MP4ChapterType chtp = MP4GetChapters( job.fileHandle, &chapters, &chapterCount, _ChapterType ); if (0 == chapterCount) { return herrf( "File \"%s\" does not contain chapters of type %s\n", job.file.c_str(), getChapterTypeName( chtp ).c_str() ); } // build the filename string outName = job.file; if( _ChapterFile.empty() ) { FileSystem::pathnameStripExtension( outName ); outName.append( ".chapters.txt" ); } else { outName = _ChapterFile; } ostringstream oss; oss << "Exporting " << chapterCount << " " << getChapterTypeName( chtp ); oss << " chapters from file " << '"' << job.file << '"' << " into chapter file " << '"' << outName << '"' << endl; verbose1f( "%s", oss.str().c_str() ); if( dryrunAbort() ) { // free up the memory MP4Free(chapters); return SUCCESS; } // open the file File out( outName, File::MODE_CREATE ); if( openFileForWriting( out ) ) { // free up the memory MP4Free(chapters); return FAILURE; } // write the chapters #if defined( _WIN32 ) static const char* LINEND = "\r\n"; #else static const char* LINEND = "\n"; #endif File::Size nout; bool failure = SUCCESS; int width = 2; if( CHPT_FMT_COMMON == _ChapterFormat && (chapterCount / 100) >= 1 ) { width = 3; } Timecode duration( 0, CHAPTERTIMESCALE ); duration.setFormat( Timecode::DECIMAL ); for( uint32_t i = 0; i < chapterCount; ++i ) { // print the infos ostringstream oss; switch( _ChapterFormat ) { case CHPT_FMT_COMMON: oss << "CHAPTER" << setw( width ) << setfill( '0' ) << i+1 << '=' << duration.svalue << LINEND << "CHAPTER" << setw( width ) << setfill( '0' ) << i+1 << "NAME=" << chapters[i].title << LINEND; break; case CHPT_FMT_NATIVE: default: oss << duration.svalue << ' ' << chapters[i].title << LINEND; } string str = oss.str(); if( out.write( str.c_str(), str.size(), nout ) ) { failure = herrf( "write to %s failed: %s\n", outName.c_str(), sys::getLastErrorStr() ); break; } // add the duration of this chapter to the sum (the start time of the next chapter) duration += Timecode(chapters[i].duration, CHAPTERTIMESCALE); } out.close(); if( failure ) { verbose1f( "removing file %s\n", outName.c_str() ); ::remove( outName.c_str() ); } // free up the memory MP4Free(chapters); return SUCCESS; }
static void *mp4Decode(void *args) { MP4FileHandle mp4file; pthread_mutex_lock(&mutex); seekPosition = -1; bPlaying = TRUE; if(!(mp4file = MP4Read(args, 0))){ mp4cfg.file_type = FILE_AAC; MP4Close(mp4file); }else{ mp4cfg.file_type = FILE_MP4; } if(mp4cfg.file_type == FILE_MP4){ // We are reading a MP4 file gint mp4track; if((mp4track = getAACTrack(mp4file)) < 0){ //TODO: check here for others Audio format..... g_print("Unsupported Audio track type\n"); g_free(args); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); }else{ faacDecHandle decoder; unsigned char *buffer = NULL; guint bufferSize = 0; gulong samplerate; guchar channels; guint avgBitrate; MP4Duration duration; gulong msDuration; MP4SampleId numSamples; MP4SampleId sampleID = 1; decoder = faacDecOpen(); MP4GetTrackESConfiguration(mp4file, mp4track, &buffer, &bufferSize); if(!buffer){ g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } if(faacDecInit2(decoder, buffer, bufferSize, &samplerate, &channels)<0){ g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } g_free(buffer); if(channels == 0){ g_print("Number of Channels not supported\n"); g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } duration = MP4GetTrackDuration(mp4file, mp4track); msDuration = MP4ConvertFromTrackDuration(mp4file, mp4track, duration, MP4_MSECS_TIME_SCALE); numSamples = MP4GetTrackNumberOfSamples(mp4file, mp4track); mp4_ip.output->open_audio(FMT_S16_NE, samplerate, channels); mp4_ip.output->flush(0); mp4_ip.set_info(args, msDuration, -1, samplerate/1000, channels); g_print("MP4 - %d channels @ %d Hz\n", channels, samplerate); while(bPlaying){ void* sampleBuffer; faacDecFrameInfo frameInfo; gint rc; if(seekPosition!=-1){ duration = MP4ConvertToTrackDuration(mp4file, mp4track, seekPosition*1000, MP4_MSECS_TIME_SCALE); sampleID = MP4GetSampleIdFromTime(mp4file, mp4track, duration, 0); mp4_ip.output->flush(seekPosition*1000); seekPosition = -1; } buffer=NULL; bufferSize=0; if(sampleID > numSamples){ mp4_ip.output->close_audio(); g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } rc = MP4ReadSample(mp4file, mp4track, sampleID++, &buffer, &bufferSize, NULL, NULL, NULL, NULL); //g_print("%d/%d\n", sampleID-1, numSamples); if((rc==0) || (buffer== NULL)){ g_print("MP4: read error\n"); sampleBuffer = NULL; sampleID=0; mp4_ip.output->buffer_free(); mp4_ip.output->close_audio(); g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); }else{ sampleBuffer = faacDecDecode(decoder, &frameInfo, buffer, bufferSize); if(frameInfo.error > 0){ g_print("MP4: %s\n", faacDecGetErrorMessage(frameInfo.error)); mp4_ip.output->close_audio(); g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } if(buffer){ g_free(buffer); buffer=NULL; bufferSize=0; } while(bPlaying && mp4_ip.output->buffer_free()<frameInfo.samples<<1) xmms_usleep(30000); } mp4_ip.add_vis_pcm(mp4_ip.output->written_time(), FMT_S16_NE, channels, frameInfo.samples<<1, sampleBuffer); mp4_ip.output->write_audio(sampleBuffer, frameInfo.samples<<1); } while(bPlaying && mp4_ip.output->buffer_free()){ xmms_usleep(10000); } mp4_ip.output->close_audio(); g_free(args); faacDecClose(decoder); MP4Close(mp4file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } } else{ // WE ARE READING AN AAC FILE FILE *file = NULL; faacDecHandle decoder = 0; guchar *buffer = 0; gulong bufferconsumed = 0; gulong samplerate = 0; guchar channels; gulong buffervalid = 0; TitleInput* input; gchar *temp = g_strdup(args); gchar *ext = strrchr(temp, '.'); gchar *xmmstitle = NULL; faacDecConfigurationPtr config; if((file = fopen(args, "rb")) == 0){ g_print("AAC: can't find file %s\n", args); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } if((decoder = faacDecOpen()) == NULL){ g_print("AAC: Open Decoder Error\n"); fclose(file); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } config = faacDecGetCurrentConfiguration(decoder); config->useOldADTSFormat = 0; faacDecSetConfiguration(decoder, config); if((buffer = g_malloc(BUFFER_SIZE)) == NULL){ g_print("AAC: error g_malloc\n"); fclose(file); bPlaying = FALSE; faacDecClose(decoder); pthread_mutex_unlock(&mutex); pthread_exit(NULL); } if((buffervalid = fread(buffer, 1, BUFFER_SIZE, file))==0){ g_print("AAC: Error reading file\n"); g_free(buffer); fclose(file); bPlaying = FALSE; faacDecClose(decoder); pthread_mutex_unlock(&mutex); pthread_exit(NULL); } XMMS_NEW_TITLEINPUT(input); input->file_name = g_basename(temp); input->file_ext = ext ? ext+1 : NULL; input->file_path = temp; if(!strncmp(buffer, "ID3", 3)){ gint size = 0; fseek(file, 0, SEEK_SET); size = (buffer[6]<<21) | (buffer[7]<<14) | (buffer[8]<<7) | buffer[9]; size+=10; fread(buffer, 1, size, file); buffervalid = fread(buffer, 1, BUFFER_SIZE, file); } xmmstitle = xmms_get_titlestring(xmms_get_gentitle_format(), input); if(xmmstitle == NULL) xmmstitle = g_strdup(input->file_name); if(temp) g_free(temp); if(input->performer) g_free(input->performer); if(input->album_name) g_free(input->album_name); if(input->track_name) g_free(input->track_name); if(input->genre) g_free(input->genre); g_free(input); bufferconsumed = faacDecInit(decoder, buffer, buffervalid, &samplerate, &channels); if(mp4_ip.output->open_audio(FMT_S16_NE,samplerate,channels) == FALSE){ g_print("AAC: Output Error\n"); g_free(buffer); buffer=0; faacDecClose(decoder); fclose(file); mp4_ip.output->close_audio(); /* if(positionTable){ g_free(positionTable); positionTable=0; } */ g_free(xmmstitle); bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } //if(bSeek){ //mp4_ip.set_info(xmmstitle, lenght*1000, -1, samplerate, channels); //}else{ mp4_ip.set_info(xmmstitle, -1, -1, samplerate, channels); //} mp4_ip.output->flush(0); while(bPlaying && buffervalid > 0){ faacDecFrameInfo finfo; unsigned long samplesdecoded; char* sample_buffer = NULL; /* if(bSeek && seekPosition!=-1){ fseek(file, positionTable[seekPosition], SEEK_SET); bufferconsumed=0; buffervalid = fread(buffer, 1, BUFFER_SIZE, file); aac_ip.output->flush(seekPosition*1000); seekPosition=-1; } */ if(bufferconsumed > 0){ memmove(buffer, &buffer[bufferconsumed], buffervalid-bufferconsumed); buffervalid -= bufferconsumed; buffervalid += fread(&buffer[buffervalid], 1, BUFFER_SIZE-buffervalid, file); bufferconsumed = 0; } sample_buffer = faacDecDecode(decoder, &finfo, buffer, buffervalid); if(finfo.error){ config = faacDecGetCurrentConfiguration(decoder); if(config->useOldADTSFormat != 1){ faacDecClose(decoder); decoder = faacDecOpen(); config = faacDecGetCurrentConfiguration(decoder); config->useOldADTSFormat = 1; faacDecSetConfiguration(decoder, config); finfo.bytesconsumed=0; finfo.samples = 0; faacDecInit(decoder, buffer, buffervalid, &samplerate, &channels); }else{ g_print("FAAD2 Warning %s\n", faacDecGetErrorMessage(finfo.error)); buffervalid = 0; } } bufferconsumed += finfo.bytesconsumed; samplesdecoded = finfo.samples; if((samplesdecoded<=0) && !sample_buffer){ g_print("AAC: error sample decoding\n"); continue; } while(bPlaying && mp4_ip.output->buffer_free() < (samplesdecoded<<1)){ xmms_usleep(10000); } mp4_ip.add_vis_pcm(mp4_ip.output->written_time(), FMT_S16_LE, channels, samplesdecoded<<1, sample_buffer); mp4_ip.output->write_audio(sample_buffer, samplesdecoded<<1); } while(bPlaying && mp4_ip.output->buffer_playing()){ xmms_usleep(10000); } mp4_ip.output->buffer_free(); mp4_ip.output->close_audio(); bPlaying = FALSE; g_free(buffer); faacDecClose(decoder); g_free(xmmstitle); fclose(file); seekPosition = -1; /* if(positionTable){ g_free(positionTable); positionTable=0; } */ bPlaying = FALSE; pthread_mutex_unlock(&mutex); pthread_exit(NULL); } }
DWORD CTag_Mp4::Load(LPCTSTR szFileName) { DWORD dwWin32errorCode = ERROR_SUCCESS; Release(); char *pFileName = TstrToDataAlloc(szFileName, -1, NULL, DTC_CODE_UTF8); if (pFileName == NULL) { return -1; } char *info = MP4FileInfo(pFileName); if(!info) { free(pFileName); Release(); return -1; } m_strTrackInfo = info; free(info); m_strTrackInfo.Replace(_T("\n"),_T("\r\n")); // Audio/Video _StripAudioInfo(m_strTrackInfo,m_strAudioInfo,m_strVideoInfo); MP4FileHandle mp4file = MP4Read(pFileName); free(pFileName); if(mp4file == MP4_INVALID_FILE_HANDLE) { Release(); return -1; } m_bEnable = TRUE; char *value; uint16_t numvalue, numvalue2; uint8_t int8value; #ifdef USE_OLD_TAG_API if(MP4GetMetadataName(mp4file,&value) && (value != NULL)) { m_strMetadata_Name = _CnvMetadata(value); } if(MP4GetMetadataArtist(mp4file,&value) && (value != NULL)) { m_strMetadata_Artist = _CnvMetadata(value); } if(MP4GetMetadataAlbum(mp4file,&value) && (value != NULL)) { m_strMetadata_Album = _CnvMetadata(value); } if(MP4GetMetadataGrouping(mp4file,&value) && (value != NULL)) { m_strMetadata_Group = _CnvMetadata(value); } if(MP4GetMetadataWriter(mp4file,&value) && (value != NULL)) { m_strMetadata_Composer = _CnvMetadata(value); } if(MP4GetMetadataGenre(mp4file,&value) && (value != NULL)) { m_strMetadata_Genre = _CnvMetadata(value); } if(MP4GetMetadataTrack(mp4file,&numvalue,&numvalue2)) { m_iMetadata_Track1 = numvalue; m_iMetadata_Track2 = numvalue2; } if(MP4GetMetadataDisk(mp4file,&numvalue,&numvalue2)) { m_iMetadata_Disc1 = numvalue; m_iMetadata_Disc2 = numvalue2; } if(MP4GetMetadataTempo(mp4file,&numvalue)) { m_iMetadata_Tempo = numvalue; } if(MP4GetMetadataYear(mp4file,&value) && (value != NULL)) { m_strMetadata_Year = _CnvMetadata(value); } if(MP4GetMetadataCompilation(mp4file,&int8value)) { m_iMetadata_Compilation = int8value; } if(MP4GetMetadataComment(mp4file,&value) && (value != NULL)) { m_strMetadata_Comment = _CnvMetadata(value); } if(MP4GetMetadataTool(mp4file,&value) && (value != NULL)) { m_strMetadata_Tool = _CnvMetadata(value); } #else const MP4Tags* tags = MP4TagsAlloc(); if(tags) { MP4TagsFetch(tags, mp4file); if(tags->name) { m_strMetadata_Name = _CnvMetadata(tags->name); } if(tags->artist) { m_strMetadata_Artist = _CnvMetadata(tags->artist); } if(tags->album) { m_strMetadata_Album = _CnvMetadata(tags->album); } if(tags->grouping) { m_strMetadata_Group = _CnvMetadata(tags->grouping); } if(tags->composer) { m_strMetadata_Composer = _CnvMetadata(tags->composer); } if(tags->genre) { m_strMetadata_Genre = _CnvMetadata(tags->genre); } if(tags->track) { m_iMetadata_Track1 = tags->track->index; m_iMetadata_Track2 = tags->track->total; } if(tags->disk) { m_iMetadata_Disc1 = tags->disk->index; m_iMetadata_Disc2 = tags->disk->total; } if(tags->tempo) { m_iMetadata_Tempo = *tags->tempo; } if(tags->releaseDate) { m_strMetadata_Year = _CnvMetadata(tags->releaseDate); } if(tags->compilation) { m_iMetadata_Compilation = *tags->compilation; } if(tags->comments) { m_strMetadata_Comment = _CnvMetadata(tags->comments); } if(tags->encodingTool) { m_strMetadata_Tool = _CnvMetadata(tags->encodingTool); } MP4TagsFree(tags); } #endif MP4Close(mp4file); return dwWin32errorCode; }
bool MP4::File::save() { MP4Close(mp4file); MP4FileHandle handle = MP4Modify(name()); if(handle == MP4_INVALID_FILE_HANDLE) { mp4file = MP4Read(name()); return false; } #ifdef MP4V2_HAS_WRITE_BUG /* according to gtkpod we have to delete all meta data before modifying it, save the stuff we would not touch */ // need to fetch/rewrite this only if we aren't going to anyway uint8_t compilation = 0; bool has_compilation = mp4tag->compilation() == MP4::Tag::Undefined ? MP4GetMetadataCompilation(handle, &compilation) : false; char *tool = NULL; MP4GetMetadataTool(handle, &tool); MP4MetadataDelete(handle); #endif #define setmeta(val, tag) \ if(mp4tag->val().isNull()) { \ /*MP4DeleteMetadata##tag(handle);*/ \ MP4SetMetadata##tag(handle, ""); \ } else { \ MP4SetMetadata##tag(handle, mp4tag->val().toCString(true)); \ } setmeta(title, Name); setmeta(artist, Artist); setmeta(album, Album); setmeta(comment, Comment); setmeta(genre, Genre); char buf[100] = ""; if(mp4tag->year()) snprintf(buf, sizeof(buf), "%u", mp4tag->year()); MP4SetMetadataYear(handle, buf); u_int16_t t1, t2; MP4GetMetadataTrack(handle, &t1, &t2); MP4SetMetadataTrack(handle, mp4tag->track(), t2); if(mp4tag->bpm() != 0) MP4SetMetadataTempo(handle, mp4tag->bpm()); if(mp4tag->compilation() != MP4::Tag::Undefined) { MP4SetMetadataCompilation(handle, mp4tag->compilation()); } MP4SetMetadataCoverArt(handle, mp4tag->cover().size() ? const_cast<u_int8_t *>( reinterpret_cast<const u_int8_t *>( mp4tag->cover().data() ) ) : 0, mp4tag->cover().size()); #ifdef MP4V2_HAS_WRITE_BUG // set the saved data again if(has_compilation) MP4SetMetadataCompilation(handle, compilation); if(tool) { MP4SetMetadataTool(handle, tool); free(tool); } #endif MP4Close(handle); mp4file = MP4Read(name()); if(mp4file == MP4_INVALID_FILE_HANDLE) { fprintf(stderr, "reopen failed\n"); return false; } return true; }
static GtkWidget* CreateMp4TrackMenu( GtkWidget* menu, char type, const char* source, u_int32_t* pIndex, u_int32_t* pNumber, u_int32_t** ppValues) { *pIndex = 0; u_int32_t newTrackNumber = 1; MP4FileHandle mp4File = MP4Read(source); char* trackType = NULL; if (mp4File) { if (type == 'V') { trackType = MP4_VIDEO_TRACK_TYPE; } else { trackType = MP4_AUDIO_TRACK_TYPE; } newTrackNumber = MP4GetNumberOfTracks(mp4File, trackType); } u_int32_t* newTrackValues = (u_int32_t*)malloc(sizeof(u_int32_t) * newTrackNumber); char** newTrackNames = (char**)malloc(sizeof(char*) * newTrackNumber); if (!mp4File) { newTrackValues[0] = 0; newTrackNames[0] = strdup(""); } else { for (u_int8_t i = 0; i < newTrackNumber; i++) { MP4TrackId trackId = MP4FindTrackId(mp4File, i, trackType); char* trackName = "Unknown"; char buf[64]; if (trackType == MP4_VIDEO_TRACK_TYPE) { u_int8_t videoType = MP4GetTrackEsdsObjectTypeId(mp4File, trackId); switch (videoType) { case MP4_MPEG1_VIDEO_TYPE: trackName = "MPEG1"; break; case MP4_MPEG2_SIMPLE_VIDEO_TYPE: case MP4_MPEG2_MAIN_VIDEO_TYPE: case MP4_MPEG2_SNR_VIDEO_TYPE: case MP4_MPEG2_SPATIAL_VIDEO_TYPE: case MP4_MPEG2_HIGH_VIDEO_TYPE: case MP4_MPEG2_442_VIDEO_TYPE: trackName = "MPEG2"; break; case MP4_MPEG4_VIDEO_TYPE: trackName = "MPEG4"; break; case MP4_YUV12_VIDEO_TYPE: trackName = "YUV12"; break; case MP4_H263_VIDEO_TYPE: trackName = "H263"; break; case MP4_H261_VIDEO_TYPE: trackName = "H261"; break; } snprintf(buf, sizeof(buf), "%u - %s %u x %u %.2f fps %u kbps", trackId, trackName, MP4GetTrackVideoWidth(mp4File, trackId), MP4GetTrackVideoHeight(mp4File, trackId), MP4GetTrackVideoFrameRate(mp4File, trackId), (MP4GetTrackBitRate(mp4File, trackId) + 500) / 1000); } else { // audio u_int8_t audioType = MP4GetTrackEsdsObjectTypeId(mp4File, trackId); switch (audioType) { case MP4_MPEG1_AUDIO_TYPE: case MP4_MPEG2_AUDIO_TYPE: trackName = "MPEG (MP3)"; break; case MP4_MPEG2_AAC_MAIN_AUDIO_TYPE: case MP4_MPEG2_AAC_LC_AUDIO_TYPE: case MP4_MPEG2_AAC_SSR_AUDIO_TYPE: case MP4_MPEG4_AUDIO_TYPE: trackName = "AAC"; break; case MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE: trackName = "PCM16 LITTLE ENDIAN"; break; case MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE: trackName = "PCM16 BIG ENDIAN"; case MP4_AC3_AUDIO_TYPE: trackName = "AC3"; break; case MP4_VORBIS_AUDIO_TYPE: trackName = "Ogg Vorbis"; break; case MP4_ALAW_AUDIO_TYPE: trackName = "G711 aLaw"; break; case MP4_ULAW_AUDIO_TYPE: trackName = "G711 uLaw"; break; } snprintf(buf, sizeof(buf), "%u - %s %u kbps", trackId, trackName, (MP4GetTrackBitRate(mp4File, trackId) + 500) / 1000); } newTrackValues[i] = trackId; newTrackNames[i] = strdup(buf); } MP4Close(mp4File); } // (re)create the menu menu = CreateOptionMenu( menu, newTrackNames, newTrackNumber, *pIndex, GTK_SIGNAL_FUNC(on_track_menu_activate)); // free up old names for (u_int8_t i = 0; i < *pNumber; i++) { free(trackNames[i]); } free(trackNames); free(*ppValues); *pNumber = newTrackNumber; trackNames = newTrackNames; *ppValues = newTrackValues; return menu; }
int main(int argc, char** argv) { const char* usageString = "[options] mp4file where:\n" "\t--track(-t)= <track-id> - display track id\n" "\t--dump-offset(-d) - dump offset within sample\n" "\t--rendering-offset(-r) - dump rendering offset\n" "\t--verbose=<level> - mp4 file verbosity\n" "\t--version(-V) - display version\n"; MP4TrackId trackId = MP4_INVALID_TRACK_ID; u_int32_t verbosity = MP4_DETAILS_ERROR; bool dump_offset = false; bool dump_rend = false; /* begin processing command line */ ProgName = argv[0]; while (true) { int c = -1; int option_index = 0; static struct option long_options[] = { { "track", 1, 0, 't' }, { "verbose", 2, 0, 'v' }, { "version", 0, 0, 'V' }, { "dump-offset", 0, 0, 'd'}, { "rendering-offset", 0, 0, 'r'}, { "help", 0, 0, '?'}, { NULL, 0, 0, 0 } }; c = getopt_long_only(argc, argv, "t:v::V?", long_options, &option_index); if (c == -1) break; switch (c) { case 'd': dump_offset = true; break; case 'r': dump_rend = true; break; case 't': if (sscanf(optarg, "%u", &trackId) != 1) { fprintf(stderr, "%s: bad track-id specified: %s\n", ProgName, optarg); exit(1); } break; case 'v': verbosity |= MP4_DETAILS_READ; if (optarg) { u_int32_t level; if (sscanf(optarg, "%u", &level) == 1) { if (level >= 2) { verbosity |= MP4_DETAILS_TABLE; } if (level >= 3) { verbosity |= MP4_DETAILS_SAMPLE; } if (level >= 4) { verbosity = MP4_DETAILS_ALL; } } } break; case '?': fprintf(stderr, "usage: %s %s", ProgName, usageString); exit(0); case 'V': fprintf(stderr, "%s - %s version %s\n", ProgName, MPEG4IP_PACKAGE, MPEG4IP_VERSION); exit(0); default: fprintf(stderr, "%s: unknown option specified, ignoring: %c\n", ProgName, c); } } /* check that we have at least one non-option argument */ if ((argc - optind) < 1) { fprintf(stderr, "usage: %s %s", ProgName, usageString); exit(1); } if (verbosity) { fprintf(stderr, "%s version %s\n", ProgName, MPEG4IP_VERSION); } /* point to the specified file names */ Mp4PathName = argv[optind++]; char* lastSlash = strrchr(Mp4PathName, '/'); if (lastSlash) { Mp4FileName = lastSlash + 1; } else { Mp4FileName = Mp4PathName; } /* warn about extraneous non-option arguments */ if (optind < argc) { fprintf(stderr, "%s: unknown options specified, ignoring: ", ProgName); while (optind < argc) { fprintf(stderr, "%s ", argv[optind++]); } fprintf(stderr, "\n"); } /* end processing of command line */ MP4FileHandle mp4File = MP4Read(Mp4PathName, verbosity); if (!mp4File) { exit(1); } if (trackId == MP4_INVALID_TRACK_ID) { u_int32_t numTracks = MP4GetNumberOfTracks(mp4File, MP4_VIDEO_TRACK_TYPE); printf("tracks %d\n", numTracks); for (u_int32_t ix = 0; ix < numTracks; ix++) { trackId = MP4FindTrackId(mp4File, ix, MP4_VIDEO_TRACK_TYPE); DumpTrack(mp4File, trackId, dump_offset, dump_rend); } } else { DumpTrack(mp4File, trackId, dump_offset, dump_rend); } MP4Close(mp4File); return(0); }
int main (int argc, char *argv[]) { int len = 0; char *allargs = NULL, *step; argc--; argv++; while (argc > 0 && strcasestr(*argv, ".mp4") != NULL) { MP4FileHandle mp4File; mp4File = MP4Read(*argv, MP4_DETAILS_ERROR); if (MP4_IS_VALID_FILE_HANDLE(mp4File)) { MP4TrackId tid; uint32_t ix = 0; do { uint32_t verb = MP4GetVerbosity(mp4File); MP4SetVerbosity(mp4File, verb & ~(MP4_DETAILS_ERROR)); tid = MP4FindTrackId(mp4File, ix, MP4_VIDEO_TRACK_TYPE); MP4SetVerbosity(mp4File, verb); if (MP4_IS_VALID_TRACK_ID(tid)) { uint8_t type = MP4GetTrackEsdsObjectTypeId(mp4File, tid); if (type == MP4_MPEG4_VIDEO_TYPE) { uint8_t *foo; uint32_t bufsize; MP4GetTrackESConfiguration(mp4File, tid, &foo, &bufsize); if (foo != NULL && bufsize != 0) { printf("%s\n", *argv); decode(foo, bufsize); free(foo); } else { fprintf(stderr, "%s - track %d - can't find esds\n", *argv, tid); } } else { fprintf(stderr, "%s - track %d is not MPEG4 - type %u\n", *argv, tid, type); } } ix++; } while (MP4_IS_VALID_TRACK_ID(tid)); } else { fprintf(stderr, "%s is not a valid mp4 file\n", *argv); } argc--; argv++; } if (argc > 0) { len = 1; while (argc > 0) { len += strlen(*argv); if (allargs == NULL) { allargs = (char *)malloc(len); allargs[0] = '\0'; } else allargs = (char *)realloc(allargs, len); strcat(allargs, *argv); argv++; argc--; } if ((len - 1) & 0x1) { fprintf(stderr, "odd length VOL\n"); exit(1); } len /= 2; uint8_t *vol = (uint8_t *)malloc(len), *write; write = vol; step = allargs; int ix; for (ix = 0; ix < len; ix++) { *write = 0; *write = tohex(*step) << 4; step++; *write |= tohex(*step); step++; write++; } printf("decoding vol \"%s\"\n", allargs); decode(vol, len); } return(0); }
void Context::open(const char * file) { fh = MP4Read(file, 0); if (fh == MP4_INVALID_FILE_HANDLE) throw Exception(file, "Open failed"); getTracks(file); }
Result SoundSourceM4A::tryOpen(const AudioSourceConfig& audioSrcCfg) { DEBUG_ASSERT(MP4_INVALID_FILE_HANDLE == m_hFile); // open MP4 file, check for >= ver 1.9.1 // From mp4v2/file.h: // * On Windows, this should be a UTF-8 encoded string. // * On other platforms, it should be an 8-bit encoding that is // * appropriate for the platform, locale, file system, etc. // * (prefer to use UTF-8 when possible). #if MP4V2_PROJECT_version_hex <= 0x00010901 m_hFile = MP4Read(getLocalFileName().toUtf8().constData(), 0); #else m_hFile = MP4Read(getLocalFileName().toUtf8().constData()); #endif if (MP4_INVALID_FILE_HANDLE == m_hFile) { qWarning() << "Failed to open file for reading:" << getUrlString(); return ERR; } m_trackId = findFirstAudioTrackId(m_hFile); if (MP4_INVALID_TRACK_ID == m_trackId) { qWarning() << "No AAC track found:" << getUrlString(); return ERR; } // Read fixed sample duration. If the sample duration is not // fixed (that is, if the number of frames per sample block varies // through the file), the call returns MP4_INVALID_DURATION. We // can't currently handle these. m_framesPerSampleBlock = MP4GetTrackFixedSampleDuration(m_hFile, m_trackId); if (MP4_INVALID_DURATION == m_framesPerSampleBlock) { qWarning() << "Unable to decode tracks with non-fixed sample durations: " << getUrlString(); return ERR; } const MP4SampleId numberOfSamples = MP4GetTrackNumberOfSamples(m_hFile, m_trackId); if (0 >= numberOfSamples) { qWarning() << "Failed to read number of samples from file:" << getUrlString(); return ERR; } m_maxSampleBlockId = kSampleBlockIdMin + (numberOfSamples - 1); // Determine the maximum input size (in bytes) of a // sample block for the selected track. const u_int32_t maxSampleBlockInputSize = MP4GetTrackMaxSampleSize(m_hFile, m_trackId); m_inputBuffer.resize(maxSampleBlockInputSize, 0); DEBUG_ASSERT(nullptr == m_hDecoder); // not already opened m_hDecoder = NeAACDecOpen(); if (!m_hDecoder) { qWarning() << "Failed to open the AAC decoder!"; return ERR; } NeAACDecConfigurationPtr pDecoderConfig = NeAACDecGetCurrentConfiguration( m_hDecoder); pDecoderConfig->outputFormat = FAAD_FMT_FLOAT; if ((kChannelCountMono == audioSrcCfg.channelCountHint) || (kChannelCountStereo == audioSrcCfg.channelCountHint)) { pDecoderConfig->downMatrix = 1; } else { pDecoderConfig->downMatrix = 0; } pDecoderConfig->defObjectType = LC; if (!NeAACDecSetConfiguration(m_hDecoder, pDecoderConfig)) { qWarning() << "Failed to configure AAC decoder!"; return ERR; } u_int8_t* configBuffer = nullptr; u_int32_t configBufferSize = 0; if (!MP4GetTrackESConfiguration(m_hFile, m_trackId, &configBuffer, &configBufferSize)) { /* failed to get mpeg-4 audio config... this is ok. * NeAACDecInit2() will simply use default values instead. */ qWarning() << "Failed to read the MP4 audio configuration." << "Continuing with default values."; } SAMPLERATE_TYPE sampleRate; unsigned char channelCount; if (0 > NeAACDecInit2(m_hDecoder, configBuffer, configBufferSize, &sampleRate, &channelCount)) { free(configBuffer); qWarning() << "Failed to initialize the AAC decoder!"; return ERR; } else { free(configBuffer); } // Calculate how many sample blocks we need to decode in advance // of a random seek in order to get the recommended number of // prefetch frames m_numberOfPrefetchSampleBlocks = (kNumberOfPrefetchFrames + (m_framesPerSampleBlock - 1)) / m_framesPerSampleBlock; setChannelCount(channelCount); setFrameRate(sampleRate); setFrameCount(((m_maxSampleBlockId - kSampleBlockIdMin) + 1) * m_framesPerSampleBlock); // Resize temporary buffer for decoded sample data const SINT sampleBufferCapacity = frames2samples(m_framesPerSampleBlock); m_sampleBuffer.resetCapacity(sampleBufferCapacity); // Invalidate current position to enforce the following // seek operation m_curFrameIndex = getMaxFrameIndex(); // (Re-)Start decoding at the beginning of the file seekSampleFrame(getMinFrameIndex()); return OK; }
int main(int argc, char** argv) { const char* usageString = "[-l] [-t <track-id>] [-s <sample-id>] [-v [<level>]] <file-name>\n"; bool doList = false; bool doSamples = false; MP4TrackId trackId = MP4_INVALID_TRACK_ID; MP4SampleId sampleId = MP4_INVALID_SAMPLE_ID; char* dstFileName = NULL; u_int32_t verbosity = MP4_DETAILS_ERROR; fprintf(stderr, "You don't want to use this utility - use mp4creator --extract instead\n"); fprintf(stderr, "If you really want to use it, remove this warning and the exit call\n"); fprintf(stderr, "from the source file\n"); exit(-1); /* begin processing command line */ ProgName = argv[0]; while (true) { int c = -1; int option_index = 0; static struct option long_options[] = { { "list", 0, 0, 'l' }, { "track", 1, 0, 't' }, { "sample", 2, 0, 's' }, { "verbose", 2, 0, 'v' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; c = getopt_long_only(argc, argv, "lt:s::v::V", long_options, &option_index); if (c == -1) break; switch (c) { case 'l': doList = true; break; case 's': doSamples = true; if (optarg) { if (sscanf(optarg, "%u", &sampleId) != 1) { fprintf(stderr, "%s: bad sample-id specified: %s\n", ProgName, optarg); } } break; case 't': if (sscanf(optarg, "%u", &trackId) != 1) { fprintf(stderr, "%s: bad track-id specified: %s\n", ProgName, optarg); exit(1); } break; case 'v': verbosity |= MP4_DETAILS_READ; if (optarg) { u_int32_t level; if (sscanf(optarg, "%u", &level) == 1) { if (level >= 2) { verbosity |= MP4_DETAILS_TABLE; } if (level >= 3) { verbosity |= MP4_DETAILS_SAMPLE; } if (level >= 4) { verbosity = MP4_DETAILS_ALL; } } } break; case '?': fprintf(stderr, "usage: %s %s", ProgName, usageString); exit(0); case 'V': fprintf(stderr, "%s - %s version %s\n", ProgName, MPEG4IP_PACKAGE, MPEG4IP_VERSION); exit(0); default: fprintf(stderr, "%s: unknown option specified, ignoring: %c\n", ProgName, c); } } /* check that we have at least one non-option argument */ if ((argc - optind) < 1) { fprintf(stderr, "usage: %s %s", ProgName, usageString); exit(1); } if (verbosity) { fprintf(stderr, "%s version %s\n", ProgName, MPEG4IP_VERSION); } /* point to the specified file names */ Mp4PathName = argv[optind++]; /* get dest file name for a single track */ if (trackId && (argc - optind) > 0) { dstFileName = argv[optind++]; } char* lastSlash = strrchr(Mp4PathName, '/'); if (lastSlash) { Mp4FileName = lastSlash + 1; } else { Mp4FileName = Mp4PathName; } /* warn about extraneous non-option arguments */ if (optind < argc) { fprintf(stderr, "%s: unknown options specified, ignoring: ", ProgName); while (optind < argc) { fprintf(stderr, "%s ", argv[optind++]); } fprintf(stderr, "\n"); } /* end processing of command line */ MP4FileHandle mp4File = MP4Read(Mp4PathName, verbosity); if (!mp4File) { exit(1); } if (doList) { MP4Info(mp4File); exit(0); } if (trackId == 0) { u_int32_t numTracks = MP4GetNumberOfTracks(mp4File); for (u_int32_t i = 0; i < numTracks; i++) { trackId = MP4FindTrackId(mp4File, i); ExtractTrack(mp4File, trackId, doSamples, sampleId); } } else { ExtractTrack(mp4File, trackId, doSamples, sampleId, dstFileName); } MP4Close(mp4File); return(0); }
Result SoundSourceM4A::tryOpen(const AudioSourceConfig& audioSrcCfg) { DEBUG_ASSERT(MP4_INVALID_FILE_HANDLE == m_hFile); /* open MP4 file, check for >= ver 1.9.1 */ #if MP4V2_PROJECT_version_hex <= 0x00010901 m_hFile = MP4Read(getLocalFileNameBytes().constData(), 0); #else m_hFile = MP4Read(getLocalFileNameBytes().constData()); #endif if (MP4_INVALID_FILE_HANDLE == m_hFile) { qWarning() << "Failed to open file for reading:" << getUrlString(); return ERR; } m_trackId = findFirstAudioTrackId(m_hFile); if (MP4_INVALID_TRACK_ID == m_trackId) { qWarning() << "No AAC track found:" << getUrlString(); return ERR; } const MP4SampleId numberOfSamples = MP4GetTrackNumberOfSamples(m_hFile, m_trackId); if (0 >= numberOfSamples) { qWarning() << "Failed to read number of samples from file:" << getUrlString(); return ERR; } m_maxSampleBlockId = kSampleBlockIdMin + (numberOfSamples - 1); // Determine the maximum input size (in bytes) of a // sample block for the selected track. const u_int32_t maxSampleBlockInputSize = MP4GetTrackMaxSampleSize(m_hFile, m_trackId); m_inputBuffer.resize(maxSampleBlockInputSize, 0); DEBUG_ASSERT(NULL == m_hDecoder); // not already opened m_hDecoder = NeAACDecOpen(); if (!m_hDecoder) { qWarning() << "Failed to open the AAC decoder!"; return ERR; } NeAACDecConfigurationPtr pDecoderConfig = NeAACDecGetCurrentConfiguration( m_hDecoder); pDecoderConfig->outputFormat = FAAD_FMT_FLOAT; if ((kChannelCountMono == audioSrcCfg.channelCountHint) || (kChannelCountStereo == audioSrcCfg.channelCountHint)) { pDecoderConfig->downMatrix = 1; } else { pDecoderConfig->downMatrix = 0; } pDecoderConfig->defObjectType = LC; if (!NeAACDecSetConfiguration(m_hDecoder, pDecoderConfig)) { qWarning() << "Failed to configure AAC decoder!"; return ERR; } u_int8_t* configBuffer = NULL; u_int32_t configBufferSize = 0; if (!MP4GetTrackESConfiguration(m_hFile, m_trackId, &configBuffer, &configBufferSize)) { /* failed to get mpeg-4 audio config... this is ok. * NeAACDecInit2() will simply use default values instead. */ qWarning() << "Failed to read the MP4 audio configuration." << "Continuing with default values."; } SAMPLERATE_TYPE sampleRate; unsigned char channelCount; if (0 > NeAACDecInit2(m_hDecoder, configBuffer, configBufferSize, &sampleRate, &channelCount)) { free(configBuffer); qWarning() << "Failed to initialize the AAC decoder!"; return ERR; } else { free(configBuffer); } setChannelCount(channelCount); setFrameRate(sampleRate); setFrameCount(getFrameCountForSampleBlockId(m_maxSampleBlockId)); // Resize temporary buffer for decoded sample data const SINT sampleBufferCapacity = frames2samples(kFramesPerSampleBlock); m_sampleBuffer.resetCapacity(sampleBufferCapacity); // Invalidate current position to enforce the following // seek operation m_curFrameIndex = getMaxFrameIndex(); // (Re-)Start decoding at the beginning of the file seekSampleFrame(getMinFrameIndex()); return OK; }
bool LoadFileAAC(FILE_INFO *pFile) { MP4FileHandle h = MP4Read(GetFullPath(pFile), 0); if (h == MP4_INVALID_FILE_HANDLE) { return false; } char* value; char* buff; u_int16_t no, total; if (MP4GetMetadataName(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetTrackNameSI(pFile, buff); free(buff); } } if (MP4GetMetadataArtist(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetArtistNameSI(pFile, buff); free(buff); } } if (MP4GetMetadataWriter(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetComposerSI(pFile, buff); free(buff); } } if (MP4GetMetadataComment(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetCommentSI(pFile, buff); free(buff); } } if (MP4GetMetadataTool(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetSoftwareSI(pFile, buff); free(buff); } } if (MP4GetMetadataYear(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetYearSI(pFile, buff); free(buff); } } if (MP4GetMetadataAlbum(h, &value) == true) { if (UTF8toSJIS(value, &buff) == true) { SetAlbumNameSI(pFile, buff); free(buff); } } if (MP4GetMetadataAlbumArtist(h, &value) == true) { /* 取得できるようにmp4v2.dllを変更 */ if (UTF8toSJIS(value, &buff) == true) { SetAlbumArtistSI(pFile, buff); free(buff); } } if (MP4GetMetadataTrack(h, &no, &total) == true) { char trackNo[10]; if (total > 0) { sprintf(trackNo, "%d/%d", no, total); } else { sprintf(trackNo, "%d", no); } SetTrackNumberSI(pFile, trackNo); } if (MP4GetMetadataDisk(h, &no, &total) == true) { char diskNo[10]; if (total > 0) { sprintf(diskNo, "%d/%d", no, total); } else { sprintf(diskNo, "%d", no); } SetDiskNumberSI(pFile, diskNo); } if (MP4GetMetadataGenre(h, &value) == true) { /* 取得できるようにmp4v2.dllを変更 */ if (UTF8toSJIS(value, &buff) == true) { SetGenreSI(pFile, buff); free(buff); } } if (MP4GetMetadataGrouping(h, &value) == true) { /* 取得できるようにmp4v2.dllに追加 */ if (UTF8toSJIS(value, &buff) == true) { SetKeywordSI(pFile, buff); free(buff); } } CString strOther = ""; { u_int16_t tempo; if (MP4GetMetadataTempo(h, &tempo) == true) { if (tempo > 0) { char buff[10]; sprintf(buff, " %dBPM", tempo); strOther += buff; } } } { u_int8_t cpl; if (MP4GetMetadataCompilation(h, &cpl) == true) { if (cpl == 1) { strOther += " コンピレーションの一部"; } } } //MP4TrackId trackId = MP4FindTrackId(pFile, 0, MP4_AUDIO_TRACK_TYPE); //SetAudioFormat(pFile, MP4Info(h)); // mp4info.cpp PrintAudioInfo() MP4TrackId trackId = MP4FindTrackId(h, 0); static const char* mpeg4AudioNames[] = { "MPEG-4 AAC main", "MPEG-4 AAC LC", "MPEG-4 AAC SSR", "MPEG-4 AAC LTP", NULL, "MPEG-4 AAC Scalable", "MPEG-4 TwinVQ", "MPEG-4 CELP", "MPEG-4 HVXC", NULL, NULL, "MPEG-4 TTSI", "MPEG-4 Main Synthetic", "MPEG-4 Wavetable Syn", "MPEG-4 General MIDI", "MPEG-4 Algo Syn and Audio FX", "MPEG-4 ER AAC LC", NULL, "MPEG-4 ER AAC LTP", "MPEG-4 ER AAC Scalable", "MPEG-4 ER TwinVQ", "MPEG-4 ER BSAC", "MPEG-4 ER ACC LD", "MPEG-4 ER CELP", "MPEG-4 ER HVXC", "MPEG-4 ER HILN", "MPEG-4 ER Parametric", }; static u_int8_t mpegAudioTypes[] = { MP4_MPEG2_AAC_MAIN_AUDIO_TYPE, // 0x66 MP4_MPEG2_AAC_LC_AUDIO_TYPE, // 0x67 MP4_MPEG2_AAC_SSR_AUDIO_TYPE, // 0x68 MP4_MPEG2_AUDIO_TYPE, // 0x69 MP4_MPEG1_AUDIO_TYPE, // 0x6B MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE, MP4_VORBIS_AUDIO_TYPE, MP4_ALAW_AUDIO_TYPE, MP4_ULAW_AUDIO_TYPE, MP4_G723_AUDIO_TYPE, MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE, }; static const char* mpegAudioNames[] = { "MPEG-2 AAC Main", "MPEG-2 AAC LC", "MPEG-2 AAC SSR", "MPEG-2 Audio (13818-3)", "MPEG-1 Audio (11172-3)", "PCM16 (little endian)", "Vorbis", "G.711 aLaw", "G.711 uLaw", "G.723.1", "PCM16 (big endian)", }; static u_int8_t numMpegAudioTypes = sizeof(mpegAudioTypes) / sizeof(u_int8_t); u_int8_t type = MP4GetTrackEsdsObjectTypeId(h, trackId); const char* typeName = "Unknown"; if (type == MP4_MPEG4_AUDIO_TYPE) { u_int8_t* pAacConfig = NULL; u_int32_t aacConfigLength; MP4GetTrackESConfiguration(h, trackId, &pAacConfig, &aacConfigLength); if (pAacConfig != NULL && aacConfigLength >= 2) { type = (pAacConfig[0] >> 3) & 0x1f; if (type == 0 || type == 5 || type == 10 || type == 11 || type == 18 || type >= 28) { typeName = "MPEG-4"; } else { typeName = mpeg4AudioNames[type - 1]; } MP4Free(pAacConfig); } else {
bool Read_Tag(LPCSTR filename, void* tagHandle) { // TODO: // read metadata from tag and set each field to tagHandle // only TAGFIELD_* are supported (see QCDModTagEditor.h) // example of how to set value to tagHandle // use SetFieldA for ASCII or MultiBytes strings. // use SetFieldW for UNICODE strings // // ModInitTag.SetFieldW(tagHandle, TAGFIELD_COMPOSER, szwValue); // return true for successfull read, false for failure MP4FileHandle file = MP4_INVALID_FILE_HANDLE; char *pVal, dummy1[1024]; short dummy, dummy2; u_int32_t valueSize = 0; #ifdef DEBUG_OUTPUT in_mp4_DebugOutput("mp4_tag_read"); #endif file = MP4Read(filename, 0); if (file == MP4_INVALID_FILE_HANDLE) return false; /* get Metadata */ pVal = NULL; MP4GetMetadataName(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_TITLE, pVal); pVal = NULL; MP4GetMetadataArtist(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_ARTIST, pVal); pVal = NULL; MP4GetMetadataWriter(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_COMPOSER, pVal); pVal = NULL; MP4GetMetadataComment(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_COMMENT, pVal); pVal = NULL; MP4GetMetadataAlbum(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_ALBUM, pVal); pVal = NULL; MP4GetMetadataGenre(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_GENRE, pVal); //dummy = 0; //MP4GetMetadataTempo(file, &dummy); //if (dummy) //{ // wsprintf(dummy1, "%d", dummy); // SetDlgItemText(hwndDlg,IDC_METATEMPO, dummy1); //} dummy = 0; dummy2 = 0; MP4GetMetadataTrack(file, (unsigned __int16*)&dummy, (unsigned __int16*)&dummy2); if (dummy) { wsprintf(dummy1, "%d", dummy); ModInitTag.SetFieldA(tagHandle, TAGFIELD_TRACK, dummy1); } //if (dumm2) //{ // wsprintf(dummy1, "%d", dummy2); // SetDlgItemText(hwndDlg,IDC_METATRACK2, dummy1); //} //dummy = 0; dummy2 = 0; //MP4GetMetadataDisk(file, &dummy, &dummy2); //if (dummy) //{ // wsprintf(dummy1, "%d", dummy); // SetDlgItemText(hwndDlg,IDC_METADISK1, dummy1); //} //if (dummy) //{ // wsprintf(dummy1, "%d", dummy2); // SetDlgItemText(hwndDlg,IDC_METADISK2, dummy1); //} pVal = NULL; if (MP4GetMetadataYear(file, &pVal)) uSetDlgItemText(tagHandle, TAGFIELD_YEAR, pVal); //dummy3 = 0; //MP4GetMetadataCompilation(file, &dummy3); //if (dummy3) // SendMessage(GetDlgItem(hwndDlg, IDC_METACOMPILATION), BM_SETCHECK, BST_CHECKED, 0); pVal = NULL; MP4GetMetadataTool(file, &pVal); uSetDlgItemText(tagHandle, TAGFIELD_ENCODER, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "CONDUCTOR", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_CONDUCTOR, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "ORCHESTRA", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_ORCHESTRA, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "YEARCOMPOSED", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_YEARCOMPOSED, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "ORIGARTIST", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_ORIGARTIST, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "LABEL", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_LABEL, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "COPYRIGHT", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_COPYRIGHT, pVal); pVal = NULL; MP4GetMetadataFreeForm(file, "CDDBTAGID", (unsigned __int8**)&pVal, &valueSize); uSetDlgItemText(tagHandle, TAGFIELD_CDDBTAGID, pVal); /* ! Metadata */ MP4Close(file); return true; }
void main(int argc, char** argv) { if (argc < 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); exit(1); } //u_int32_t verbosity = MP4_DETAILS_ALL; char* fileName = argv[1]; // open the mp4 file, and read meta-info MP4FileHandle mp4File = MP4Read(fileName ); uint8_t profileLevel = MP4GetVideoProfileLevel(mp4File); // get a handle on the first video track MP4TrackId trackId = MP4FindTrackId(mp4File, 0, "video"); // gather the crucial track information uint32_t timeScale = MP4GetTrackTimeScale(mp4File, trackId); // note all times and durations // are in units of the track time scale MP4Duration trackDuration = MP4GetTrackDuration(mp4File, trackId); MP4SampleId numSamples = MP4GetTrackNumberOfSamples(mp4File, trackId); uint32_t maxSampleSize = MP4GetTrackMaxSampleSize(mp4File, trackId); uint8_t* pConfig; uint32_t configSize = 0; MP4GetTrackESConfiguration(mp4File, trackId, &pConfig, &configSize); // initialize decoder with Elementary Stream (ES) configuration // done with our copy of ES configuration free(pConfig); // now consecutively read and display the track samples uint8_t* pSample = (uint8_t*)malloc(maxSampleSize); uint32_t sampleSize; MP4Timestamp sampleTime; MP4Duration sampleDuration; MP4Duration sampleRenderingOffset; bool isSyncSample; for (MP4SampleId sampleId = 1; sampleId <= numSamples; sampleId++) { // give ReadSample our own buffer, and let it know how big it is sampleSize = maxSampleSize; // read next sample from video track MP4ReadSample(mp4File, trackId, sampleId, &pSample, &sampleSize, &sampleTime, &sampleDuration, &sampleRenderingOffset, &isSyncSample); // convert timestamp and duration from track time to milliseconds uint64_t myTime = MP4ConvertFromTrackTimestamp(mp4File, trackId, sampleTime, MP4_MSECS_TIME_SCALE); uint64_t myDuration = MP4ConvertFromTrackDuration(mp4File, trackId, sampleDuration, MP4_MSECS_TIME_SCALE); // decode frame and display it } // close mp4 file MP4Close(mp4File); // Note to seek to time 'when' in the track // use MP4GetSampleIdFromTime(MP4FileHandle hFile, // MP4Timestamp when, bool wantSyncSample) // 'wantSyncSample' determines if a sync sample is desired or not // e.g. // MP4Timestamp when = // MP4ConvertToTrackTimestamp(mp4File, trackId, 30, MP4_SECS_TIME_SCALE); // MP4SampleId newSampleId = MP4GetSampleIdFromTime(mp4File, when, true); // MP4ReadSample(mp4File, trackId, newSampleId, ...); // // Note that start time for sample may be later than 'when' exit(0); }
bool Write_Tag(LPCSTR filename, void* tagHandle) { // TODO: // read metadata from tagHandle and set each field to supported tag // only TAGFIELD_* are supported (see QCDModTagEditor.h) // example of how to get value from tagHandle // use SetFieldA for ASCII or MultiBytes strings. // use SetFieldW for UNICODE strings // // szwValue = ModInitTag.GetFieldW(tagHandle, TAGFIELD_ORCHESTRA); // write tag to file MP4FileHandle file = MP4_INVALID_FILE_HANDLE; char dummy1[1024]; char temp[1024]; short dummy, dummy2; #ifdef DEBUG_OUTPUT in_mp4_DebugOutput("mp4_tag_write"); #endif /* save Metadata changes */ tag_delete(&tags); file = MP4Read(filename, 0); if (file != MP4_INVALID_FILE_HANDLE) { ReadMP4Tag(file, &tags); MP4Close(file); file = MP4Modify(filename, 0, 0); if (file != MP4_INVALID_FILE_HANDLE) { MP4MetadataDelete(file); MP4Close(file); } } file = MP4Modify(filename, 0, 0); if (file == MP4_INVALID_FILE_HANDLE) { tag_delete(&tags); //EndDialog(hwndDlg, wParam); return false; } uGetDlgItemText(tagHandle, TAGFIELD_TITLE, dummy1, 1024); tag_set_field(&tags, "title", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_COMPOSER, dummy1, 1024); tag_set_field(&tags, "writer", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_ARTIST, dummy1, 1024); tag_set_field(&tags, "artist", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_ALBUM, dummy1, 1024); tag_set_field(&tags, "album", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_COMMENT, dummy1, 1024); tag_set_field(&tags, "comment", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_GENRE, dummy1, 1024); tag_set_field(&tags, "genre", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_YEAR, dummy1, 1024); tag_set_field(&tags, "year", dummy1); dummy = 0; MP4GetMetadataTrack(file, (unsigned __int16*)&dummy, (unsigned __int16*)&dummy2); memcpy(dummy1, ModInitTag.GetFieldA(tagHandle, TAGFIELD_TRACK), sizeof(dummy1)); dummy = atoi(dummy1); wsprintf(temp, "%d/%d", dummy, dummy2); tag_set_field(&tags, "track", temp); //GetDlgItemText(hwndDlg, IDC_METADISK1, dummy1, 1024); //dummy = atoi(dummy1); //GetDlgItemText(hwndDlg, IDC_METADISK2, dummy1, 1024); //dummy2 = atoi(dummy1); //wsprintf(temp, "%d/%d", dummy, dummy2); //tag_set_field(&tags, "disc", temp); //GetDlgItemText(hwndDlg, IDC_METATEMPO, dummy1, 1024); //tag_set_field(&tags, "tempo", dummy1); //dummy3 = SendMessage(GetDlgItem(hwndDlg, IDC_METACOMPILATION), BM_GETCHECK, 0, 0); //tag_set_field(&tags, "compilation", (dummy3 ? "1" : "0")); uGetDlgItemText(tagHandle, TAGFIELD_ENCODER, dummy1, 1024); tag_set_field(&tags, "tool", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_CONDUCTOR, dummy1, 1024); tag_set_field(&tags, "CONDUCTOR", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_ORCHESTRA, dummy1, 1024); tag_set_field(&tags, "ORCHESTRA", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_YEARCOMPOSED, dummy1, 1024); tag_set_field(&tags, "YEARCOMPOSED", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_ORIGARTIST, dummy1, 1024); tag_set_field(&tags, "ORIGARTIST", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_LABEL, dummy1, 1024); tag_set_field(&tags, "LABEL", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_COPYRIGHT, dummy1, 1024); tag_set_field(&tags, "COPYRIGHT", dummy1); uGetDlgItemText(tagHandle, TAGFIELD_CDDBTAGID, dummy1, 1024); tag_set_field(&tags, "CDDBTAGID", dummy1); WriteMP4Tag(file, &tags); MP4Close(file); MP4Optimize(filename, NULL, 0); /* ! */ return true; }
int MP4Streamer::Open(const char *filename) { //LOg Log(">MP4 opening [%s]\n",filename); //Lock pthread_mutex_lock(&mutex); //If already opened if (opened) { //Unlock pthread_mutex_unlock(&mutex); //Return error return Error("Already opened\n"); } // Open mp4 file mp4 = MP4Read(filename); // If not valid if (mp4 == MP4_INVALID_FILE_HANDLE) { //Unlock pthread_mutex_unlock(&mutex); //Return error return Error("Invalid file handle for %s\n",filename); } //No tracks audio = NULL; video = NULL; text = NULL; //Iterate thougth tracks DWORD i = 0; // Get the first hint track MP4TrackId hintId = MP4_INVALID_TRACK_ID; // Iterate hint tracks do { // Get the next hint track hintId = MP4FindTrackId(mp4, i++, MP4_HINT_TRACK_TYPE, 0); Log("-Found hint track [hintId:%d]\n", hintId); // Get asociated track MP4TrackId trackId = MP4GetHintTrackReferenceTrackId(mp4, hintId); // Check it's good if (trackId != MP4_INVALID_TRACK_ID) { // Get track type const char *type = MP4GetTrackType(mp4, trackId); // Get rtp track char *name; BYTE payload; MP4GetHintTrackRtpPayload(mp4, hintId, &name, &payload, NULL, NULL); Log("-Streaming media [trackId:%d,type:\"%s\",name:\"%s\",payload:%d]\n", trackId, type, name, payload); // Check track type if ((strcmp(type, MP4_AUDIO_TRACK_TYPE) == 0) && !audio) { // Depending on the name if (strcmp("PCMU", name) == 0) //Create new audio track audio = new MP4RtpTrack(MediaFrame::Audio,AudioCodec::PCMU,payload); else if (strcmp("PCMA", name) == 0) //Create new audio track audio = new MP4RtpTrack(MediaFrame::Audio,AudioCodec::PCMA,payload); else //Skip continue; // Get time scale audio->timeScale = MP4GetTrackTimeScale(mp4, hintId); //Store the other values audio->mp4 = mp4; audio->hint = hintId; audio->track = trackId; audio->sampleId = 1; audio->packetIndex = 0; } else if ((strcmp(type, MP4_VIDEO_TRACK_TYPE) == 0) && !video) { // Depending on the name if (strcmp("H263", name) == 0) //Create new video track video = new MP4RtpTrack(MediaFrame::Video,VideoCodec::H263_1996,payload); else if (strcmp("H263-1998", name) == 0) //Create new video track video = new MP4RtpTrack(MediaFrame::Video,VideoCodec::H263_1998,payload); else if (strcmp("H263-2000", name) == 0) //Create new video track video = new MP4RtpTrack(MediaFrame::Video,VideoCodec::H263_1998,payload); else if (strcmp("H264", name) == 0) //Create new video track video = new MP4RtpTrack(MediaFrame::Video,VideoCodec::H264,payload); else continue; // Get time scale video->timeScale = MP4GetTrackTimeScale(mp4, hintId); // it's video video->mp4 = mp4; video->hint = hintId; video->track = trackId; video->sampleId = 1; video->packetIndex = 0; } } } while (hintId != MP4_INVALID_TRACK_ID); // Get the first text MP4TrackId textId = MP4FindTrackId(mp4, 0, MP4_TEXT_TRACK_TYPE, 0); Log("-Found text track [%d]\n",textId); // Iterate hint tracks if (textId != MP4_INVALID_TRACK_ID) { //We have it text = new MP4TextTrack(); //Set values text->mp4 = mp4; text->track = textId; text->sampleId = 1; // Get time scale text->timeScale = MP4GetTrackTimeScale(mp4, textId); } //We are opened opened = true; //Unlock pthread_mutex_unlock(&mutex); return 1; }