Function: readTrunTotals()
function readTrunTotals(cursor): TrackRunTotals;Defined in: readers/readTrunTotals.ts:53
Reads the number of samples of a trun box and the total of its sample durations, with no object for each sample.
ISO/IEC 14496-12:2015 - 8.8.8
Use the function for the number of samples and the duration of a trun box. The function reads no field of one sample. readTrun creates one object for each sample. This function creates one object for the box.
The total uses only the values of the sample records. The function applies no default. For a box with no sample durations, multiply sampleCount by the default sample duration. That default comes from the tfhd box. If the tfhd box has no default, it comes from the trex box.
The function moves the cursor past the sample records. The total is a number. A total above 2^53 - 1 loses precision. Each duration is at most 2^32 - 1. So only a box with more than 2^21 samples can have a total above 2^53 - 1.
Parameters
cursor
The read cursor at the first byte after the box header
Returns
The number of samples and the total of the sample durations
Throws
RangeError if the sample records of sample_count pass the end of the box. The message is the same as in readTrun.
Throws
RangeError if a header field passes the end of the box.
Example
const file = load('captions_fragmented.mp4')
const trex = seekIsoBox(file, ['moov', 'mvex', 'trex'])
const trexDuration = trex ? readTrex(trex).defaultSampleDuration : 0
let sampleCount = 0
let duration = 0
for (const traf of seekIsoBoxes(file, ['moof', 'traf'])) {
const tfhd = seekIsoBox(traf, ['tfhd'])
const defaultDuration =
(tfhd ? readTfhd(tfhd).defaultSampleDuration : undefined) ?? trexDuration
for (const cursor of seekIsoBoxes(traf, ['trun'])) {
const totals = readTrunTotals(cursor)
sampleCount += totals.sampleCount
// A box with no sample durations takes the default for each sample.
duration += totals.totalSampleDuration ?? totals.sampleCount * defaultDuration
}
}