BxDolTranscoderAudio.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * This class transcodes audio files.
  11. *
  12. * To generate audio which plays in all moders browsers,
  13. * you need to generate .mp3 audio.
  14. * Audio is converting upon first access, so it is probably better to force audio conversion by calling @see BxDolTranscoderAudio::getFileUrl just after video uploading.
  15. * Audio for conversion is queued and when cron is run video conversion is performed.
  16. *
  17. * Transcoder object and other params are the same as in @see BxDolTranscoderImage, but it is highly recommended to disable 'atime_pruning' and 'atime_tracking',
  18. * or set it to fairly big value, since audio transcoding is not performed on the fly and takes some time.
  19. *
  20. * Available filters:
  21. * - Mp3 - this filter convert audio into .mp3 format, the parameters are the following:
  22. * - audio_bitrate - audio bitrate in kb (128k by default)
  23. * - ffmpeg_options - additional command line options for ffmepeg, as key => value array (empty by default)
  24. *
  25. *
  26. * Example of usage:
  27. * @code
  28. * // transcoder object which generate .mp3 file
  29. * $oTranscoderMp3 = BxDolTranscoder::getObjectInstance('bx_audio_mp3');
  30. *
  31. * // make sure to call it only once (for example: during module installation), before the first usage, no need to call it every time
  32. * $oTranscoderMp3->registerHandlers();
  33. *
  34. * // get URLs of transcoded videos and video thumbnail, 33 is ID of original video file stored in specified storage object
  35. * $sUrlMp3 = $oTranscoderMp3->getFileUrl(33);
  36. *
  37. * echo 'My voice: <audio controls><source src="' . $sUrlMp3 . '" type="audio/mpeg"></audio>';
  38. * @endcode
  39. *
  40. * Also @see transcoder_audio sample for complete example.
  41. */
  42. class BxDolTranscoderAudio extends BxDolTranscoderVideo implements iBxDolFactoryObject
  43. {
  44. protected function __construct($aObject, $oStorage)
  45. {
  46. parent::__construct($aObject, $oStorage);
  47. $this->_oDb = new BxDolTranscoderAudioQuery($aObject);
  48. $this->_sQueueTable = $this->_oDb->getQueueTable();
  49. }
  50. /**
  51. * check if transcoder suppors given file mime type
  52. */
  53. public function isMimeTypeSupported($sMimeType)
  54. {
  55. if (0 === strncmp($sMimeType, 'audio/', 6))
  56. return true;
  57. return false;
  58. }
  59. /**
  60. * Convert video to .mp4 format
  61. */
  62. protected function applyFilter_Mp3 ($sFile, $aParams)
  63. {
  64. return $this->_convertVideo($sFile, $sFile, '.mp3', $aParams, array (
  65. 'acodec' => 'libmp3lame',
  66. 'ar' => '44100',
  67. 'b:a' => isset($aParams['audio_bitrate']) ? $aParams['audio_bitrate'] . 'k' : '128k',
  68. ));
  69. }
  70. }
  71. /** @} */