codec-utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @file codec-utils.js
  3. */
  4. /**
  5. * Check if a codec string refers to an audio codec.
  6. *
  7. * @param {String} codec codec string to check
  8. * @return {Boolean} if this is an audio codec
  9. * @private
  10. */
  11. 'use strict';
  12. Object.defineProperty(exports, '__esModule', {
  13. value: true
  14. });
  15. var isAudioCodec = function isAudioCodec(codec) {
  16. return (/mp4a\.\d+.\d+/i.test(codec)
  17. );
  18. };
  19. /**
  20. * Check if a codec string refers to a video codec.
  21. *
  22. * @param {String} codec codec string to check
  23. * @return {Boolean} if this is a video codec
  24. * @private
  25. */
  26. var isVideoCodec = function isVideoCodec(codec) {
  27. return (/avc1\.[\da-f]+/i.test(codec)
  28. );
  29. };
  30. /**
  31. * Parse a content type header into a type and parameters
  32. * object
  33. *
  34. * @param {String} type the content type header
  35. * @return {Object} the parsed content-type
  36. * @private
  37. */
  38. var parseContentType = function parseContentType(type) {
  39. var object = { type: '', parameters: {} };
  40. var parameters = type.trim().split(';');
  41. // first parameter should always be content-type
  42. object.type = parameters.shift().trim();
  43. parameters.forEach(function (parameter) {
  44. var pair = parameter.trim().split('=');
  45. if (pair.length > 1) {
  46. var _name = pair[0].replace(/"/g, '').trim();
  47. var value = pair[1].replace(/"/g, '').trim();
  48. object.parameters[_name] = value;
  49. }
  50. });
  51. return object;
  52. };
  53. /**
  54. * Replace the old apple-style `avc1.<dd>.<dd>` codec string with the standard
  55. * `avc1.<hhhhhh>`
  56. *
  57. * @param {Array} codecs an array of codec strings to fix
  58. * @return {Array} the translated codec array
  59. * @private
  60. */
  61. var translateLegacyCodecs = function translateLegacyCodecs(codecs) {
  62. return codecs.map(function (codec) {
  63. return codec.replace(/avc1\.(\d+)\.(\d+)/i, function (orig, profile, avcLevel) {
  64. var profileHex = ('00' + Number(profile).toString(16)).slice(-2);
  65. var avcLevelHex = ('00' + Number(avcLevel).toString(16)).slice(-2);
  66. return 'avc1.' + profileHex + '00' + avcLevelHex;
  67. });
  68. });
  69. };
  70. exports['default'] = {
  71. isAudioCodec: isAudioCodec,
  72. parseContentType: parseContentType,
  73. isVideoCodec: isVideoCodec,
  74. translateLegacyCodecs: translateLegacyCodecs
  75. };
  76. module.exports = exports['default'];