Skip to content

Commit

Permalink
feat: trim beginning of demuxed VODs
Browse files Browse the repository at this point in the history
  • Loading branch information
birme committed Aug 7, 2023
1 parent a0689bf commit 3ffa3dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,19 @@ class HLSTruncateVod {
let accDuration = 0;
let prevAccDuration = 0;
let pos = 0;
let startPos = 0;

if (this.startOffset) {
let accStartOffset = 0;
m3u.items.PlaylistItem.map((item => {
if (accStartOffset <= this.startOffset) {
accStartOffset += item.get('duration');
startPos++;
}
}));
}

m3u.items.PlaylistItem.map((item => {
m3u.items.PlaylistItem.slice(startPos).map((item => {
if (accDuration <= this.durationAudio) {
prevAccDuration = accDuration;
accDuration += item.get('duration');
Expand All @@ -224,7 +235,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(0, pos);
this.audioSegments[audioGroupId][audioLang].items.PlaylistItem = m3u.items.PlaylistItem.slice(startPos, startPos + pos);
resolve();
});
parser.on('error', (err) => {
Expand Down
21 changes: 21 additions & 0 deletions spec/hls_truncate_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ describe("HLSTruncateVod,", () => {
done();
})
});

it("can trim the beginning if start offset is requested", done => {
const mockVod = new HLSTruncateVod('http://mock.com/mock.m3u8', 30, { offset: 30 });

mockVod.load(mockMasterManifest, mockMediaManifest, mockAudioManifest)
.then(() => {
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");
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");
done();
})
});
});
describe("for Demuxed TS HLS Vods which have different segments durations,", () => {
let mockMasterManifest;
Expand Down

0 comments on commit 3ffa3dc

Please sign in to comment.