Skip to content

Commit

Permalink
Update args builder to respect WithAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiramadhana authored Jan 12, 2024
1 parent 882f348 commit 09463dd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
14 changes: 11 additions & 3 deletions internal/mosaic/command/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ func Build(m mosaic.Mosaic, cfg *config.Config) []string {
"[posv1][v2] overlay=shortest=0:x=1260:y=40 [posv2];" +
"[image][posv2] overlay=shortest=0 [mosaico]"

return []string{
args := []string{
"-loglevel", "error",
"-i", cfg.StaticsPath + "/background.jpg",
"-i", m.Medias[0].URL,
"-i", m.Medias[1].URL,
"-filter_complex", filterComplex,
"-map", "[mosaico]",
"-map", "1:a",
}

if m.WithAudio {
args = append(args, "-map", "1:a")
}

args = append(args, []string{
"-c:v", "libx264",
"-x264opts", "keyint=30:min-keyint=30:scenecut=-1",
"-preset", "ultrafast",
Expand All @@ -40,5 +46,7 @@ func Build(m mosaic.Mosaic, cfg *config.Config) []string {
"-http_persistent", "1",
"-sc_threshold", "0",
fmt.Sprintf("http://localhost:8080/%s", playlistPath),
}
}...)

return args
}
44 changes: 42 additions & 2 deletions internal/mosaic/mosaic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ func TestBuildFFMPEGCommand(t *testing.T) {
}{
{
command: "ffmpeg",
name: "Multiple URLs",
name: "Multiple URLs with audio",
key: "mosaicvideo",
mosaic: mosaic.Mosaic{
Name: "mosaicvideo",
Medias: []mosaic.Media{
{URL: "http://example.com/mosaicvideo_1.m3u8"},
{URL: "http://example.com/mosaicvideo_2.m3u8"},
},
WithAudio: true,
},
cfg: &config.Config{
StaticsPath: "statics",
Expand Down Expand Up @@ -60,12 +61,51 @@ func TestBuildFFMPEGCommand(t *testing.T) {
"http://localhost:8080/hls/mosaicvideo/playlist.m3u8",
},
},
{
command: "ffmpeg",
name: "Multiple URLs without audio",
key: "mosaicvideo",
mosaic: mosaic.Mosaic{
Name: "mosaicvideo",
Medias: []mosaic.Media{
{URL: "http://example.com/mosaicvideo_1.m3u8"},
{URL: "http://example.com/mosaicvideo_2.m3u8"},
},
WithAudio: false,
},
cfg: &config.Config{
StaticsPath: "statics",
},
expectedCmd: "ffmpeg",
expectedArgs: []string{
"-loglevel", "error",
"-i", "statics/background.jpg",
"-i", "http://example.com/mosaicvideo_1.m3u8",
"-i", "http://example.com/mosaicvideo_2.m3u8",
"-filter_complex", `nullsrc=size=1920x1080 [background];[0:v] realtime, scale=1920x1080 [image];[1:v] setpts=PTS-STARTPTS, scale=1170x660 [v1];[2:v] setpts=PTS-STARTPTS, scale=568x320 [v2];[background][v1] overlay=shortest=0:x=84:y=40 [posv1];[posv1][v2] overlay=shortest=0:x=1260:y=40 [posv2];[image][posv2] overlay=shortest=0 [mosaico]`,
"-map", "[mosaico]",
"-c:v", "libx264",
"-x264opts", "keyint=30:min-keyint=30:scenecut=-1",
"-preset", "ultrafast",
"-threads", "0",
"-r", "24",
"-c:a", "copy",
"-f", "hls",
"-hls_playlist_type", "event",
"-hls_time", "5",
"-strftime", "1",
"-method", "PUT",
"-http_persistent", "1",
"-sc_threshold", "0",
"http://localhost:8080/hls/mosaicvideo/playlist.m3u8",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := command.Build(tt.mosaic, tt.cfg)
assert.ElementsMatch(t, tt.expectedArgs, args)
assert.Equal(t, tt.expectedArgs, args)
})
}
}
Expand Down

0 comments on commit 09463dd

Please sign in to comment.