Skip to content

Commit

Permalink
fix: need to take into account when audio and video segment may differ
Browse files Browse the repository at this point in the history
  • Loading branch information
birme committed Aug 8, 2023
1 parent 3ffa3dc commit f6d03cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HLSTruncateVod {
this.playlists = {};
this.duration = duration;
this.durationAudio = 0;
this.startVideoOffset = 0;
this.bandwiths = [];
this.audioSegments = {};
if (options && options.offset) {
Expand Down Expand Up @@ -155,11 +156,13 @@ class HLSTruncateVod {
if (this.startOffset) {
let accStartOffset = 0;
m3u.items.PlaylistItem.map((item => {
if (accStartOffset <= this.startOffset) {
if (accStartOffset + item.get('duration') <= this.startOffset) {
accStartOffset += item.get('duration');
startPos++;
}
}));

this.startVideoOffset = this.startVideoOffset === 0 ? accStartOffset : this.startVideoOffset;
}

m3u.items.PlaylistItem.slice(startPos).map((item => {
Expand Down Expand Up @@ -214,11 +217,14 @@ class HLSTruncateVod {
if (this.startOffset) {
let accStartOffset = 0;
m3u.items.PlaylistItem.map((item => {
if (accStartOffset <= this.startOffset) {
if (accStartOffset + item.get('duration') <= this.startOffset) {
accStartOffset += item.get('duration');
startPos++;
}
}));
if ((accStartOffset > this.startVideoOffset) && startPos > 1) {
startPos--;
}
}

m3u.items.PlaylistItem.slice(startPos).map((item => {
Expand All @@ -235,6 +241,7 @@ class HLSTruncateVod {
if (this._similarSegItemDuration() && (accDuration - this.durationAudio) >= (this.durationAudio - prevAccDuration) && pos > 1) {
pos--;
}

this.audioSegments[audioGroupId][audioLang].items.PlaylistItem = m3u.items.PlaylistItem.slice(startPos, startPos + pos);
resolve();
});
Expand Down
20 changes: 20 additions & 0 deletions spec/hls_truncate_cmaf_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,25 @@ describe("HLSTruncateVod", () => {
done();
})
});

it("cuts to the closest segment when requesting unaligned duration with equal time between them and has start offset", done => {
const mockVod = new HLSTruncateVod('http://mock.com/mock.m3u8', 7.5, { offset: 5 });

mockVod.load(mockMasterManifest, mockMediaManifest, mockAudioManifest)
.then(() => {
const bandwidths = mockVod.getBandwidths();
const videoManifest = mockVod.getMediaManifest(bandwidths[0]);
const audioManifest = mockVod.getAudioManifest("audio-aacl-256", "sv");
const linesVideo = videoManifest.split("\n");
const linesAudio = audioManifest.split("\n");
const durationVideo = calcDuration(videoManifest);
const durationAudio = calcDuration(audioManifest);
expect(durationVideo).toEqual(6);
expect(durationAudio).toEqual(7.68);
expect(linesVideo[9]).toEqual("test-video=2500000-2.m4s");
expect(linesAudio[9]).toEqual("test-audio=256000-2.m4s");
done();
})
});
});
});
12 changes: 6 additions & 6 deletions spec/hls_truncate_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ describe("HLSTruncateVod for muxed TS HLS Vods", () => {
const bandwidths = mockVod.getBandwidths();
const manifest = mockVod.getMediaManifest(bandwidths[0]);
const lines = manifest.split("\n");
expect(lines[8]).toEqual("segment5_0_av.ts");
expect(lines[10]).toEqual("segment6_0_av.ts");
expect(lines[8]).toEqual("segment4_0_av.ts");
expect(lines[10]).toEqual("segment5_0_av.ts");
const duration = calcDuration(manifest);
expect(duration).toEqual(6);
done();
Expand Down Expand Up @@ -203,15 +203,15 @@ describe("HLSTruncateVod,", () => {
const bandwidths = mockVod.getBandwidths();
const manifest = mockVod.getMediaManifest(bandwidths[0]);
const lines = manifest.split("\n");
expect(lines[8]).toEqual("level1/seg_37.ts");
expect(lines[11]).toEqual("level1/seg_38.ts");
expect(lines[8]).toEqual("level1/seg_36.ts");
expect(lines[11]).toEqual("level1/seg_37.ts");
const duration = calcDuration(manifest);
expect(duration).toEqual(30.03);

const audioManifest = mockVod.getAudioManifest("aac", "en");
const audioLines = audioManifest.split("\n");
expect(audioLines[8]).toEqual("audio/seg_en_37.ts");
expect(audioLines[11]).toEqual("audio/seg_en_38.ts");
expect(audioLines[8]).toEqual("audio/seg_en_36.ts");
expect(audioLines[11]).toEqual("audio/seg_en_37.ts");
done();
})
});
Expand Down

0 comments on commit f6d03cf

Please sign in to comment.