📄 mp4.cpp
字号:
extern "C" u_int8_t MP4GetTrackEsdsObjectTypeId( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackEsdsObjectTypeId(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return MP4_INVALID_AUDIO_TYPE;}extern "C" MP4Duration MP4GetTrackFixedSampleDuration( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackFixedSampleDuration(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return MP4_INVALID_DURATION;}extern "C" u_int32_t MP4GetTrackBitRate( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { MP4File *pFile = (MP4File *)hFile; try { return pFile->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*.esds.decConfigDescr.avgBitrate"); } catch (MP4Error* e) { //PRINT_ERROR(e); we don't really need to print this. delete e; } // if we're here, we can't get the bitrate from above - // lets calculate it try { MP4Duration trackDur; trackDur = MP4GetTrackDuration(hFile, trackId); uint64_t msDuration = pFile->ConvertFromTrackDuration(trackId, trackDur, MP4_MSECS_TIME_SCALE); if (msDuration == 0) return 0; MP4Track *pTrack = pFile->GetTrack(trackId); uint64_t bytes = pTrack->GetTotalOfSampleSizes(); bytes *= TO_U64(8 * 1000); bytes /= msDuration; return (uint32_t)bytes; } catch (MP4Error* e) { PRINT_ERROR(e); // print this one. delete e; } } return 0;}extern "C" bool MP4GetTrackESConfiguration( MP4FileHandle hFile, MP4TrackId trackId, u_int8_t** ppConfig, u_int32_t* pConfigSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->GetTrackESConfiguration( trackId, ppConfig, pConfigSize); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } *ppConfig = NULL; *pConfigSize = 0; return false;}extern "C" bool MP4GetTrackVideoMetadata( MP4FileHandle hFile, MP4TrackId trackId, u_int8_t** ppConfig, u_int32_t* pConfigSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->GetTrackVideoMetadata( trackId, ppConfig, pConfigSize); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } *ppConfig = NULL; *pConfigSize = 0; return false;}extern "C" bool MP4SetTrackESConfiguration( MP4FileHandle hFile, MP4TrackId trackId, const u_int8_t* pConfig, u_int32_t configSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->SetTrackESConfiguration( trackId, pConfig, configSize); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4GetTrackH264ProfileLevel (MP4FileHandle hFile, MP4TrackId trackId, uint8_t *pProfile, uint8_t *pLevel){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { *pProfile = ((MP4File *)hFile)->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*[0].avcC.AVCProfileIndication"); *pLevel = ((MP4File *)hFile)->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*[0].avcC.AVCLevelIndication"); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" void MP4GetTrackH264SeqPictHeaders (MP4FileHandle hFile, MP4TrackId trackId, uint8_t ***pSeqHeader, uint32_t **pSeqHeaderSize, uint8_t ***pPictHeader, uint32_t **pPictHeaderSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->GetTrackH264SeqPictHeaders(trackId, pSeqHeader, pSeqHeaderSize, pPictHeader, pPictHeaderSize); return; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return;}extern "C" bool MP4GetTrackH264LengthSize (MP4FileHandle hFile, MP4TrackId trackId, uint32_t *pLength){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { *pLength = 1 + ((MP4File*) hFile)->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*[0].avcC.lengthSizeMinusOne"); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;} extern "C" MP4SampleId MP4GetTrackNumberOfSamples( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackNumberOfSamples(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return 0;}extern "C" u_int16_t MP4GetTrackVideoWidth( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*.width"); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return 0;}extern "C" u_int16_t MP4GetTrackVideoHeight( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackIntegerProperty(trackId, "mdia.minf.stbl.stsd.*.height"); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return 0;}extern "C" double MP4GetTrackVideoFrameRate( MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackVideoFrameRate(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return 0.0;}extern "C" int MP4GetTrackAudioChannels (MP4FileHandle hFile, MP4TrackId trackId){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->GetTrackAudioChannels(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return -1;}// returns true if the track is a media track encrypted according to ismacrypextern "C" bool MP4IsIsmaCrypMediaTrack( MP4FileHandle hFile, MP4TrackId trackId){ bool retval = false; uint32_t verb = MP4GetVerbosity(hFile); MP4SetVerbosity(hFile, verb & ~(MP4_DETAILS_ERROR)); if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { retval = ((MP4File*)hFile)->IsIsmaCrypMediaTrack(trackId); } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } MP4SetVerbosity(hFile, verb); return retval;}/* generic track properties */extern "C" bool MP4HaveTrackAtom (MP4FileHandle hFile, MP4TrackId trackId, const char *atomName){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { return ((MP4File*)hFile)->FindTrackAtom(trackId, atomName) != NULL; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;} extern "C" bool MP4GetTrackIntegerProperty ( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, u_int64_t *retvalue){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { *retvalue = ((MP4File*)hFile)->GetTrackIntegerProperty(trackId, propName); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4GetTrackFloatProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, float *retvalue){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { *retvalue = ((MP4File*)hFile)->GetTrackFloatProperty(trackId, propName); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4GetTrackStringProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, const char **retvalue){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { *retvalue = ((MP4File*)hFile)->GetTrackStringProperty(trackId, propName); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4GetTrackBytesProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, u_int8_t** ppValue, u_int32_t* pValueSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->GetTrackBytesProperty( trackId, propName, ppValue, pValueSize); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } *ppValue = NULL; *pValueSize = 0; return false;}extern "C" bool MP4SetTrackIntegerProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, int64_t value){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->SetTrackIntegerProperty(trackId, propName, value); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4SetTrackFloatProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, float value){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->SetTrackFloatProperty(trackId, propName, value); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4SetTrackStringProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, const char* value){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->SetTrackStringProperty(trackId, propName, value); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4SetTrackBytesProperty( MP4FileHandle hFile, MP4TrackId trackId, const char* propName, const u_int8_t* pValue, u_int32_t valueSize){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->SetTrackBytesProperty( trackId, propName, pValue, valueSize); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}/* sample operations */extern "C" bool MP4ReadSample( /* input parameters */ MP4FileHandle hFile, MP4TrackId trackId, MP4SampleId sampleId, /* output parameters */ u_int8_t** ppBytes, u_int32_t* pNumBytes, MP4Timestamp* pStartTime, MP4Duration* pDuration, MP4Duration* pRenderingOffset, bool* pIsSyncSample){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->ReadSample( trackId, sampleId, ppBytes, pNumBytes, pStartTime, pDuration, pRenderingOffset, pIsSyncSample); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } *pNumBytes = 0; return false;}extern "C" bool MP4ReadSampleFromTime( /* input parameters */ MP4FileHandle hFile, MP4TrackId trackId, MP4Timestamp when, /* output parameters */ u_int8_t** ppBytes, u_int32_t* pNumBytes, MP4Timestamp* pStartTime, MP4Duration* pDuration, MP4Duration* pRenderingOffset, bool* pIsSyncSample){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { MP4SampleId sampleId = ((MP4File*)hFile)->GetSampleIdFromTime( trackId, when, false); ((MP4File*)hFile)->ReadSample( trackId, sampleId, ppBytes, pNumBytes, pStartTime, pDuration, pRenderingOffset, pIsSyncSample); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } *pNumBytes = 0; return false;}extern "C" bool MP4WriteSample( MP4FileHandle hFile, MP4TrackId trackId, const u_int8_t* pBytes, u_int32_t numBytes, MP4Duration duration, MP4Duration renderingOffset, bool isSyncSample){ if (MP4_IS_VALID_FILE_HANDLE(hFile)) { try { ((MP4File*)hFile)->WriteSample( trackId, pBytes, numBytes, duration, renderingOffset, isSyncSample); return true; } catch (MP4Error* e) { PRINT_ERROR(e); delete e; } } return false;}extern "C" bool MP4CopySample( MP4FileHandle srcFile, MP4TrackId srcTrackId, MP4SampleId srcSampleId, MP4FileHandle dstFile, MP4TrackId dstTrackId, MP4Duration dstSampleDuration){ bool rc; u_int8_t* pBytes = NULL; u_int32_t numBytes = 0; MP4Duration sampleDuration; MP4Duration renderingOffset; bool isSyncSample; // Note: we leave it up to the caller to ensure that the // source and destination tracks are compatible. // i.e. copying audio samples into a video track // is unlikely to do anything useful rc = MP4ReadSample( srcFile, srcTrackId, srcSampleId, &pBytes, &numBytes, NULL, &sampleDuration, &renderingOffset, &isSyncSample); if (!rc) { return false; } if (dstFile == MP4_INVALID_FILE_HANDLE) { dstFile = srcFile; } if (dstTrackId == MP4_INVALID_TRACK_ID) { dstTrackId = srcTrackId; } if (dstSampleDuration != MP4_INVALID_DURATION) { sampleDuration = dstSampleDuration; } rc = MP4WriteSample( dstFile, dstTrackId, pBytes, numBytes, sampleDuration, renderingOffset, isSyncSample); free(pBytes);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -