flv-header.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * mux.js
  3. *
  4. * Copyright (c) Brightcove
  5. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6. */
  7. 'use strict';
  8. var FlvTag = require('./flv-tag.js'); // For information on the FLV format, see
  9. // http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf.
  10. // Technically, this function returns the header and a metadata FLV tag
  11. // if duration is greater than zero
  12. // duration in seconds
  13. // @return {object} the bytes of the FLV header as a Uint8Array
  14. var getFlvHeader = function getFlvHeader(duration, audio, video) {
  15. // :ByteArray {
  16. var headBytes = new Uint8Array(3 + 1 + 1 + 4),
  17. head = new DataView(headBytes.buffer),
  18. metadata,
  19. result,
  20. metadataLength; // default arguments
  21. duration = duration || 0;
  22. audio = audio === undefined ? true : audio;
  23. video = video === undefined ? true : video; // signature
  24. head.setUint8(0, 0x46); // 'F'
  25. head.setUint8(1, 0x4c); // 'L'
  26. head.setUint8(2, 0x56); // 'V'
  27. // version
  28. head.setUint8(3, 0x01); // flags
  29. head.setUint8(4, (audio ? 0x04 : 0x00) | (video ? 0x01 : 0x00)); // data offset, should be 9 for FLV v1
  30. head.setUint32(5, headBytes.byteLength); // init the first FLV tag
  31. if (duration <= 0) {
  32. // no duration available so just write the first field of the first
  33. // FLV tag
  34. result = new Uint8Array(headBytes.byteLength + 4);
  35. result.set(headBytes);
  36. result.set([0, 0, 0, 0], headBytes.byteLength);
  37. return result;
  38. } // write out the duration metadata tag
  39. metadata = new FlvTag(FlvTag.METADATA_TAG);
  40. metadata.pts = metadata.dts = 0;
  41. metadata.writeMetaDataDouble('duration', duration);
  42. metadataLength = metadata.finalize().length;
  43. result = new Uint8Array(headBytes.byteLength + metadataLength);
  44. result.set(headBytes);
  45. result.set(head.byteLength, metadataLength);
  46. return result;
  47. };
  48. module.exports = getFlvHeader;