Socket
Socket
Sign inDemoInstall

mux.js

Package Overview
Dependencies
Maintainers
14
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mux.js - npm Package Compare versions

Comparing version 5.3.1 to 5.4.0

153

lib/mp4/probe.js

@@ -13,59 +13,6 @@ /**

var toHexString = require('../utils/bin').toHexString;
var findBox, parseType, timescale, startTime, getVideoTrackIds, getTracks;
var mp4Inspector = require('../tools/mp4-inspector.js');
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks;
// Find the data for a box specified by its path
findBox = function(data, path) {
var results = [],
i, size, type, end, subresults;
if (!path.length) {
// short-circuit the search for empty paths
return null;
}
for (i = 0; i < data.byteLength;) {
size = toUnsigned(data[i] << 24 |
data[i + 1] << 16 |
data[i + 2] << 8 |
data[i + 3]);
type = parseType(data.subarray(i + 4, i + 8));
end = size > 1 ? i + size : data.byteLength;
if (type === path[0]) {
if (path.length === 1) {
// this is the end of the path and we've found the box we were
// looking for
results.push(data.subarray(i + 8, end));
} else {
// recursively search for the next box along the path
subresults = findBox(data.subarray(i + 8, end), path.slice(1));
if (subresults.length) {
results = results.concat(subresults);
}
}
}
i = end;
}
// we've finished searching all of data
return results;
};
/**
* Returns the string representation of an ASCII encoded four byte buffer.
* @param buffer {Uint8Array} a four-byte buffer to translate
* @return {string} the corresponding string
*/
parseType = function(buffer) {
var result = '';
result += String.fromCharCode(buffer[0]);
result += String.fromCharCode(buffer[1]);
result += String.fromCharCode(buffer[2]);
result += String.fromCharCode(buffer[3]);
return result;
};
/**
* Parses an MP4 initialization segment and extracts the timescale

@@ -91,3 +38,3 @@ * values for any declared tracks. Timescale values indicate the

result = {},
traks = findBox(init, ['moov', 'trak']);
traks = mp4Inspector.findBox(init, ['moov', 'trak']);

@@ -98,3 +45,3 @@ // mdhd timescale

tkhd = findBox(trak, ['tkhd'])[0];
tkhd = mp4Inspector.findBox(trak, ['tkhd'])[0];
if (!tkhd) {

@@ -110,3 +57,3 @@ return null;

mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
mdhd = mp4Inspector.findBox(trak, ['mdia', 'mdhd'])[0];
if (!mdhd) {

@@ -145,7 +92,7 @@ return null;

// we need info from two childrend of each track fragment box
trafs = findBox(fragment, ['moof', 'traf']);
trafs = mp4Inspector.findBox(fragment, ['moof', 'traf']);
// determine the start times for each track
baseTimes = [].concat.apply([], trafs.map(function(traf) {
return findBox(traf, ['tfhd']).map(function(tfhd) {
return mp4Inspector.findBox(traf, ['tfhd']).map(function(tfhd) {
var id, scale, baseTime;

@@ -162,3 +109,3 @@

// get the base media decode time from the tfdt
baseTime = findBox(traf, ['tfdt']).map(function(tfdt) {
baseTime = mp4Inspector.findBox(traf, ['tfdt']).map(function(tfdt) {
var version, result;

@@ -193,2 +140,52 @@

/**
* Determine the composition start, in seconds, for an MP4
* fragment.
*
* The composition start time of a fragment can be calculated using the base
* media decode time, composition time offset, and timescale, as follows:
*
* compositionStartTime = (baseMediaDecodeTime + compositionTimeOffset) / timescale
*
* All of the aforementioned information is contained within a media fragment's
* `traf` box, except for timescale info, which comes from the initialization
* segment, so a track id (also contained within a `traf`) is also necessary to
* associate it with a timescale
*
*
* @param timescales {object} - a hash of track ids to timescale values.
* @param fragment {Unit8Array} - the bytes of a media segment
* @return {number} the composition start time for the fragment, in seconds
**/
compositionStartTime = function(timescales, fragment) {
var trafBoxes = mp4Inspector.findBox(fragment, ['moof', 'traf']);
var baseMediaDecodeTime = 0;
var compositionTimeOffset = 0;
var trackId;
if (trafBoxes && trafBoxes.length) {
// The spec states that track run samples contained within a `traf` box are contiguous, but
// it does not explicitly state whether the `traf` boxes themselves are contiguous.
// We will assume that they are, so we only need the first to calculate start time.
var parsedTraf = mp4Inspector.parseTraf(trafBoxes[0]);
for (var i = 0; i < parsedTraf.boxes.length; i++) {
if (parsedTraf.boxes[i].type === 'tfhd') {
trackId = parsedTraf.boxes[i].trackId;
} else if (parsedTraf.boxes[i].type === 'tfdt') {
baseMediaDecodeTime = parsedTraf.boxes[i].baseMediaDecodeTime;
} else if (parsedTraf.boxes[i].type === 'trun' && parsedTraf.boxes[i].samples.length) {
compositionTimeOffset = parsedTraf.boxes[i].samples[0].compositionTimeOffset || 0;
}
}
}
// Get timescale for this specific track. Assume a 90kHz clock if no timescale was
// specified.
var timescale = timescales[trackId] || 90e3;
// return the composition start time, in seconds
return (baseMediaDecodeTime + compositionTimeOffset) / timescale;
};
/**
* Find the trackIds of the video tracks in this source.

@@ -205,11 +202,11 @@ * Found by parsing the Handler Reference and Track Header Boxes:

getVideoTrackIds = function(init) {
var traks = findBox(init, ['moov', 'trak']);
var traks = mp4Inspector.findBox(init, ['moov', 'trak']);
var videoTrackIds = [];
traks.forEach(function(trak) {
var hdlrs = findBox(trak, ['mdia', 'hdlr']);
var tkhds = findBox(trak, ['tkhd']);
var hdlrs = mp4Inspector.findBox(trak, ['mdia', 'hdlr']);
var tkhds = mp4Inspector.findBox(trak, ['tkhd']);
hdlrs.forEach(function(hdlr, index) {
var handlerType = parseType(hdlr.subarray(8, 12));
var handlerType = mp4Inspector.parseType(hdlr.subarray(8, 12));
var tkhd = tkhds[index];

@@ -238,3 +235,3 @@ var view;

getTracks = function(init) {
var traks = findBox(init, ['moov', 'trak']);
var traks = mp4Inspector.findBox(init, ['moov', 'trak']);
var tracks = [];

@@ -244,3 +241,3 @@

var track = {};
var tkhd = findBox(trak, ['tkhd'])[0];
var tkhd = mp4Inspector.findBox(trak, ['tkhd'])[0];
var view, version;

@@ -256,7 +253,7 @@

var hdlr = findBox(trak, ['mdia', 'hdlr'])[0];
var hdlr = mp4Inspector.findBox(trak, ['mdia', 'hdlr'])[0];
// type
if (hdlr) {
var type = parseType(hdlr.subarray(8, 12));
var type = mp4Inspector.parseType(hdlr.subarray(8, 12));

@@ -274,3 +271,3 @@ if (type === 'vide') {

// codec
var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
var stsd = mp4Inspector.findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];

@@ -280,5 +277,5 @@ if (stsd) {

// gives the codec type string
track.codec = parseType(sampleDescriptions.subarray(4, 8));
track.codec = mp4Inspector.parseType(sampleDescriptions.subarray(4, 8));
var codecBox = findBox(sampleDescriptions, [track.codec])[0];
var codecBox = mp4Inspector.findBox(sampleDescriptions, [track.codec])[0];
var codecConfig, codecConfigType;

@@ -292,3 +289,3 @@

codecConfig = codecBox.subarray(78);
codecConfigType = parseType(codecConfig.subarray(4, 8));
codecConfigType = mp4Inspector.parseType(codecConfig.subarray(4, 8));

@@ -313,3 +310,3 @@ if (codecConfigType === 'avcC' && codecConfig.length > 11) {

codecConfig = codecBox.subarray(28);
codecConfigType = parseType(codecConfig.subarray(4, 8));
codecConfigType = mp4Inspector.parseType(codecConfig.subarray(4, 8));

@@ -331,3 +328,3 @@ if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {

var mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
var mdhd = mp4Inspector.findBox(trak, ['mdia', 'mdhd'])[0];

@@ -350,8 +347,10 @@ if (mdhd && tkhd) {

module.exports = {
findBox: findBox,
parseType: parseType,
// export mp4 inspector's findBox and parseType for backwards compatibility
findBox: mp4Inspector.findBox,
parseType: mp4Inspector.parseType,
timescale: timescale,
startTime: startTime,
compositionStartTime: compositionStartTime,
videoTrackIds: getVideoTrackIds,
tracks: getTracks
};

@@ -15,4 +15,2 @@ /**

textifyMp4,
parseType = require('../mp4/probe').parseType,
toUnsigned = require('../utils/bin').toUnsigned,

@@ -33,2 +31,54 @@ parseMp4Date = function(seconds) {

},
/**
* Returns the string representation of an ASCII encoded four byte buffer.
* @param buffer {Uint8Array} a four-byte buffer to translate
* @return {string} the corresponding string
*/
parseType = function(buffer) {
var result = '';
result += String.fromCharCode(buffer[0]);
result += String.fromCharCode(buffer[1]);
result += String.fromCharCode(buffer[2]);
result += String.fromCharCode(buffer[3]);
return result;
},
// Find the data for a box specified by its path
findBox = function(data, path) {
var results = [],
i, size, type, end, subresults;
if (!path.length) {
// short-circuit the search for empty paths
return null;
}
for (i = 0; i < data.byteLength;) {
size = toUnsigned(data[i] << 24 |
data[i + 1] << 16 |
data[i + 2] << 8 |
data[i + 3]);
type = parseType(data.subarray(i + 4, i + 8));
end = size > 1 ? i + size : data.byteLength;
if (type === path[0]) {
if (path.length === 1) {
// this is the end of the path and we've found the box we were
// looking for
results.push(data.subarray(i + 8, end));
} else {
// recursively search for the next box along the path
subresults = findBox(data.subarray(i + 8, end), path.slice(1));
if (subresults.length) {
results = results.concat(subresults);
}
}
}
i = end;
}
// we've finished searching all of data
return results;
},
nalParse = function(avcStream) {

@@ -845,2 +895,4 @@ var

textify: textifyMp4,
parseType: parseType,
findBox: findBox,
parseTraf: parse.traf,

@@ -847,0 +899,0 @@ parseTfdt: parse.tfdt,

{
"name": "mux.js",
"version": "5.3.1",
"version": "5.4.0",
"description": "A collection of lightweight utilities for inspecting and manipulating video container formats.",

@@ -5,0 +5,0 @@ "repository": {

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc