From 4b8c115c57c78d5686ef7fbfe7d1d72f51da3363 Mon Sep 17 00:00:00 2001 From: Gustav Grusell Date: Fri, 30 Aug 2024 17:30:46 +0200 Subject: [PATCH] refactor: make code a little bit nicer Signed-off-by: Gustav Grusell --- src/packager.test.ts | 37 ++++++++++++++++++++++++++++++++----- src/packager.ts | 34 +++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/packager.test.ts b/src/packager.test.ts index 1c64aa6..a32cd59 100644 --- a/src/packager.test.ts +++ b/src/packager.test.ts @@ -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', @@ -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', @@ -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', @@ -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', diff --git a/src/packager.ts b/src/packager.ts index a02b9c0..6c77927 100644 --- a/src/packager.ts +++ b/src/packager.ts @@ -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(',')); } }); @@ -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( @@ -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'); }