u2takey / ffmpeg-go

golang binding for ffmpeg
Apache License 2.0
1.66k stars 167 forks source link

Move platform-specific code under build tags. Fix blinkind shell window on Windows #79

Closed bbars closed 1 year ago

bbars commented 1 year ago

Move platform-specific code under build tags; fix corresponding tests

The example SeparateProcessGroup() of brand new CompilationOptions is in the wrong place, because it uses platform-specific code:

cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}

I suggest to move SeparateProcessGroup() to _runlinux.go and move corresponding test to the new file with a build-flag.

Fix #80

Also there's a new mechanism to process every exec.Cmd before it runs.

Client code may differ by platform using build tags. I think that these mutations will be the same within an app. So global package-level setting is quite good and fast fix. Usage example:

//go:build windows

package pkg

import (
    ffmpeg "github.com/u2takey/ffmpeg-go"
    "os/exec"
    "syscall"
)

func init() {
    ffmpeg.GlobalCommandOptions = append(ffmpeg.GlobalCommandOptions, func(cmd *exec.Cmd) {
        if cmd.SysProcAttr == nil {
            cmd.SysProcAttr = &syscall.SysProcAttr{}
        }
        cmd.SysProcAttr.HideWindow = true
    }) 
}

or setpgid for linux:

//go:build linux

package pkg

import (
    ffmpeg "github.com/u2takey/ffmpeg-go"
    "os/exec"
    "syscall"
)

func init() {
    ffmpeg.GlobalCommandOptions = append(ffmpeg.GlobalCommandOptions, func(cmd *exec.Cmd) {
        if cmd.SysProcAttr == nil {
            cmd.SysProcAttr = &syscall.SysProcAttr{}
        }
        cmd.SysProcAttr.Setpgid = true
        cmd.SysProcAttr.Pgid = 0
    }) 
}