Skip to content

Commit

Permalink
refactor: make code a little bit nicer
Browse files Browse the repository at this point in the history
Signed-off-by: Gustav Grusell <gustav.grusell@eyevinn.se>
  • Loading branch information
grusell committed Aug 30, 2024
1 parent 8b23231 commit 4b8c115
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
37 changes: 32 additions & 5 deletions src/packager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('Test create shaka args', () => {
it('Should use first video file as audio source if noImplicitAudio not set', async () => {
const args = createShakaArgs(singleInputVideo, false);
expect(args).toEqual([
'in=test.mp4,stream=video,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s,playlist_name=video-1.m3u8',
'in=test.mp4,stream=audio,init_segment=audio/init.mp4,segment_template=audio/$Number$.m4s,playlist_name=audio.m3u8,hls_group_id=audio,hls_name=defaultaudio',
'in=test.mp4,stream=video,playlist_name=video-1.m3u8,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s',
'in=test.mp4,stream=audio,playlist_name=audio.m3u8,hls_group_id=audio,hls_name=defaultaudio,init_segment=audio/init.mp4,segment_template=audio/$Number$.m4s',
'--hls_master_playlist_output',
'index.m3u8',
'--generate_static_live_mpd',
Expand All @@ -60,7 +60,7 @@ describe('Test create shaka args', () => {
it('Should not use first video file as audio source if noImplicitAudio is true', async () => {
const args = createShakaArgs(singleInputVideo, true);
expect(args).toEqual([
'in=test.mp4,stream=video,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s,playlist_name=video-1.m3u8',
'in=test.mp4,stream=video,playlist_name=video-1.m3u8,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s',
'--hls_master_playlist_output',
'index.m3u8',
'--generate_static_live_mpd',
Expand All @@ -74,7 +74,7 @@ describe('Test create shaka args', () => {
segmentDuration: 3.84
});
expect(args).toEqual([
'in=test.mp4,stream=video,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s,playlist_name=video-1.m3u8',
'in=test.mp4,stream=video,playlist_name=video-1.m3u8,init_segment=video-1/init.mp4,segment_template=video-1/$Number$.m4s',
'--hls_master_playlist_output',
'index.m3u8',
'--generate_static_live_mpd',
Expand All @@ -91,7 +91,34 @@ describe('Test create shaka args', () => {
segmentSingleFileTemplate: 'Container-$KEY$.mp4'
});
expect(args).toEqual([
'in=test.mp4,stream=video,out=Container-1.mp4,playlist_name=video-1.m3u8',
'in=test.mp4,stream=video,playlist_name=video-1.m3u8,out=Container-1.mp4',
'--hls_master_playlist_output',
'index.m3u8',
'--generate_static_live_mpd',
'--mpd_output',
'manifest.mpd'
]);
});

it('Should set correct output path for stream if single file segment specified with audio', async () => {
const args = createShakaArgs(
[
...singleInputVideo,
{
type: 'audio',
filename: 'audio.mp4',
key: '2'
}
],
true,
{
segmentSingleFile: true,
segmentSingleFileTemplate: 'Container-$KEY$.mp4'
}
);
expect(args).toEqual([
'in=test.mp4,stream=video,playlist_name=video-1.m3u8,out=Container-1.mp4',
'in=audio.mp4,stream=audio,playlist_name=audio.m3u8,hls_group_id=audio,hls_name=defaultaudio,out=Container-2.mp4',
'--hls_master_playlist_output',
'index.m3u8',
'--generate_static_live_mpd',
Expand Down
34 changes: 23 additions & 11 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,27 @@ export function createShakaArgs(
if (input.type === 'video') {
const playlistName = `video-${input.key}`;
const playlist = `${playlistName}.m3u8`;
const streamOptions = [
`in=${input.filename}`,
'stream=video',
`playlist_name=${playlist}`
];
if (packageFormatOptions?.segmentSingleFile) {
const segmentName =
packageFormatOptions.segmentSingleFileTemplate?.replace(
'$KEY$',
input.key
) || `${playlistName}.mp4`;
const args = `in=${input.filename},stream=video,out=${segmentName},playlist_name=${playlist}`;
cmdInputs.push(args);
streamOptions.push(`out=${segmentName}`);
} else {
const initSegment = join(playlistName, 'init.mp4');
const segmentTemplate = join(playlistName, '$Number$.m4s');

const args = `in=${input.filename},stream=video,init_segment=${initSegment},segment_template=${segmentTemplate},playlist_name=${playlist}`;
cmdInputs.push(args);
streamOptions.push(
`init_segment=${initSegment}`,
`segment_template=${segmentTemplate}`
);
}
cmdInputs.push(streamOptions.join(','));
}
});

Expand All @@ -247,6 +253,13 @@ export function createShakaArgs(
const playlistName = `audio`;
const playlist = `${playlistName}.m3u8`;
const fileForAudio = inputForAudio.filename;
const streamOptions = [
`in=${fileForAudio}`,
'stream=audio',
`playlist_name=${playlist}`,
'hls_group_id=audio',
'hls_name=defaultaudio'
];
if (packageFormatOptions?.segmentSingleFile) {
// Ensure non-duplicate key, to ensure unique segment file name
const key = inputs.find(
Expand All @@ -257,16 +270,15 @@ export function createShakaArgs(
const segmentName =
packageFormatOptions.segmentSingleFileTemplate?.replace('$KEY$', key) ||
`${playlistName}.mp4`;

cmdInputs.push(
`in=${fileForAudio},stream=audio,out=${segmentName},playlist_name=${playlist},hls_group_id=audio,hls_name=defaultaudio`
);
streamOptions.push(`out=${segmentName}`);
} else {
const segmentTemplate = 'audio/' + '$Number$.m4s';
cmdInputs.push(
`in=${fileForAudio},stream=audio,init_segment=${playlistName}/init.mp4,segment_template=${segmentTemplate},playlist_name=${playlist},hls_group_id=audio,hls_name=defaultaudio`
streamOptions.push(
`init_segment=${playlistName}/init.mp4`,
`segment_template=${segmentTemplate}`
);
}
cmdInputs.push(streamOptions.join(','));
} else {
console.log('No audio input found');
}
Expand Down

0 comments on commit 4b8c115

Please sign in to comment.