diff --git a/ffmpeg_linux_test.go b/ffmpeg_linux_test.go index 8492dba..f9fc62e 100644 --- a/ffmpeg_linux_test.go +++ b/ffmpeg_linux_test.go @@ -9,6 +9,19 @@ import ( func TestCompileWithOptions(t *testing.T) { out := Input("dummy.mp4").Output("dummy2.mp4") cmd := out.Compile(SeparateProcessGroup()) - assert.Equal(t, cmd.SysProcAttr.Pgid, 0) + assert.Equal(t, 0, cmd.SysProcAttr.Pgid) + assert.True(t, cmd.SysProcAttr.Setpgid) +} + +func TestGlobalCommandOptions(t *testing.T) { + out := Input("dummy.mp4").Output("dummy2.mp4") + GlobalCommandOptions = append(GlobalCommandOptions, func(cmd *exec.Cmd) { + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0} + }) + defer func() { + GlobalCommandOptions = GlobalCommandOptions[0 : len(GlobalCommandOptions)-1] + }() + cmd := out.Compile() + assert.Equal(t, 0, cmd.SysProcAttr.Pgid) assert.True(t, cmd.SysProcAttr.Setpgid) } diff --git a/ffmpeg_windows_test.go b/ffmpeg_windows_test.go index c7ac10e..68a677c 100644 --- a/ffmpeg_windows_test.go +++ b/ffmpeg_windows_test.go @@ -18,3 +18,18 @@ func TestCompileWithOptions(t *testing.T) { }) assert.Equal(t, true, cmd.SysProcAttr.HideWindow) } + +func TestGlobalCommandOptions(t *testing.T) { + out := Input("dummy.mp4").Output("dummy2.mp4") + GlobalCommandOptions = append(GlobalCommandOptions, func(cmd *exec.Cmd) { + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.HideWindow = true + }) + defer func() { + GlobalCommandOptions = GlobalCommandOptions[0 : len(GlobalCommandOptions)-1] + }() + cmd := out.Compile() + assert.Equal(t, true, cmd.SysProcAttr.HideWindow) +} diff --git a/probe.go b/probe.go index 7f1318a..93ea902 100644 --- a/probe.go +++ b/probe.go @@ -34,6 +34,9 @@ func ProbeWithTimeoutExec(fileName string, timeOut time.Duration, kwargs KwArgs) cmd := exec.CommandContext(ctx, "ffprobe", args...) buf := bytes.NewBuffer(nil) cmd.Stdout = buf + for _, option := range GlobalCommandOptions { + option(cmd) + } err := cmd.Run() if err != nil { return "", err diff --git a/run.go b/run.go index c4c5b2e..539f577 100644 --- a/run.go +++ b/run.go @@ -236,6 +236,10 @@ func (s *Stream) ErrorToStdOut() *Stream { return s.WithErrorOutput(os.Stdout) } +type CommandOption func(cmd *exec.Cmd) + +var GlobalCommandOptions = make([]CommandOption, 0) + type CompilationOption func(s *Stream, cmd *exec.Cmd) func (s *Stream) SetFfmpegPath(path string) *Stream { @@ -259,6 +263,9 @@ func (s *Stream) Compile(options ...CompilationOption) *exec.Cmd { for _, option := range options { option(s, cmd) } + for _, option := range GlobalCommandOptions { + option(cmd) + } log.Printf("compiled command: ffmpeg %s\n", strings.Join(args, " ")) return cmd }