MP4Writer.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "MP4Writer.h"
  2. #include <strsafe.h>
  3. MP4Writer::MP4Writer()
  4. {
  5. wchar_t tmppath[MAX_PATH-14] = {0};
  6. GetTempPathW(MAX_PATH-14,tmppath);
  7. GetTempFileNameW(tmppath, L"mp4", 0, tempfile);
  8. mp4File = MP4Create(tempfile);
  9. if(!mp4File)
  10. {
  11. return;
  12. }
  13. }
  14. MP4Writer::~MP4Writer()
  15. {
  16. /* in case it's lingering open */
  17. if (mp4File)
  18. MP4Close(mp4File);
  19. }
  20. void MP4Writer::CloseTo(const wchar_t *filename)
  21. {
  22. MP4Close(mp4File);
  23. mp4File=0;
  24. MP4MakeIsmaCompliant(tempfile, 0, true);
  25. DeleteFileW(filename);
  26. if (MoveFileW(tempfile,filename) == 0) // if the function fails
  27. {
  28. CopyFileW(tempfile,filename, FALSE);
  29. DeleteFileW(tempfile);
  30. }
  31. }
  32. void MP4Writer::WriteGaps(uint32_t pregap, uint32_t postgap, uint64_t totalSamples)
  33. {
  34. char data[128] = {0};
  35. StringCchPrintfA(data, 128, " %08X %08X %08X %016X %08X %08X %08X %08X %08X %08X %08X %08X", 0, pregap, postgap, totalSamples, 0, 0,0, 0,0, 0,0, 0);
  36. MP4SetMetadataFreeForm(mp4File, "iTunSMPB", (u_int8_t *)data, lstrlenA(data));
  37. }
  38. void MP4Writer::Write(const void *buf, size_t size, MP4Duration duration)
  39. {
  40. MP4WriteSample(mp4File, mp4Track, (const uint8_t *)buf, size, duration);
  41. }
  42. void MP4Writer::AddAudioTrack(const HANDLE_MPEG4ENC_ENCODER encoder, const MPEG4ENC_SETUP *setup)
  43. {
  44. MPEG4ENC_INFO info;
  45. MPEG4ENC_GetInfo(encoder, &info);
  46. MP4SetTimeScale(mp4File, info.nSamplingRate[0]);
  47. mp4Track = MP4AddAudioTrack(mp4File, info.nSamplingRate[0], info.nSamplesFrame[0], MP4_MPEG4_AUDIO_TYPE);
  48. MP4SetAudioProfileLevel(mp4File, info.nProfLev);
  49. MP4SetTrackESConfiguration(mp4File, mp4Track, info.ascBuf[0].ascBuffer, (info.ascBuf[0].nAscSizeBits+7)/8);
  50. }
  51. void MP4Writer::WriteTool(const char *tool)
  52. {
  53. MP4SetMetadataTool(mp4File, tool);
  54. }