Skip to content

Commit

Permalink
Separate audio and video duration in mp4 getDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Walch committed Dec 4, 2020
1 parent 25f3deb commit c426997
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ export function getStartDTS (initData: InitData, fmp4: Uint8Array): number {
*/
export function getDuration (data: Uint8Array, initData: InitData) {
let rawDuration = 0;
let totalDuration = 0;
let videoDuration = 0;
let audioDuration = 0;
const trafs = findBox(data, ['moof', 'traf']);
for (let i = 0; i < trafs.length; i++) {
const traf = trafs[i];
Expand Down Expand Up @@ -389,24 +389,24 @@ export function getDuration (data: Uint8Array, initData: InitData) {
} else {
rawDuration = computeRawDurationFromSamples(truns[j]);
}
totalDuration += rawDuration / timescale;
if (track.type === ElementaryStreamTypes.VIDEO) {
videoDuration += rawDuration / timescale;
} else if (track.type === ElementaryStreamTypes.AUDIO) {
audioDuration += rawDuration / timescale;
}
}
}
if (totalDuration === 0) {
if (videoDuration === 0 && audioDuration === 0) {
// If duration samples are not available in the traf use sidx subsegment_duration
const sidx = parseSegmentIndex(data);
if (sidx?.references) {
return sidx.references.reduce((dur, ref) => dur + ref.info.duration || 0, 0);
}
}
if (videoDuration) {
// Only return duration of the video track. We don't want the combined duration of video and audio.
return videoDuration;
}
return totalDuration;
return audioDuration;
}

/*
Expand Down

0 comments on commit c426997

Please sign in to comment.