mirror of
https://github.com/u2takey/ffmpeg-go.git
synced 2025-09-26 20:11:17 +08:00
Merge branch 'master' into master
This commit is contained in:
27
ffmpeg_linux_test.go
Normal file
27
ffmpeg_linux_test.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package ffmpeg_go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompileWithOptions(t *testing.T) {
|
||||||
|
out := Input("dummy.mp4").Output("dummy2.mp4")
|
||||||
|
cmd := out.Compile(SeparateProcessGroup())
|
||||||
|
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)
|
||||||
|
}
|
@@ -277,13 +277,6 @@ func TestCompile(t *testing.T) {
|
|||||||
assert.Equal(t, out.Compile().Args, []string{"ffmpeg", "-i", "dummy.mp4", "dummy2.mp4"})
|
assert.Equal(t, out.Compile().Args, []string{"ffmpeg", "-i", "dummy.mp4", "dummy2.mp4"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCompileWithOptions(t *testing.T) {
|
|
||||||
out := Input("dummy.mp4").Output("dummy2.mp4")
|
|
||||||
cmd := out.Compile(SeparateProcessGroup())
|
|
||||||
assert.Equal(t, cmd.SysProcAttr.Pgid, 0)
|
|
||||||
assert.True(t, cmd.SysProcAttr.Setpgid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPipe(t *testing.T) {
|
func TestPipe(t *testing.T) {
|
||||||
|
|
||||||
width, height := 32, 32
|
width, height := 32, 32
|
||||||
|
35
ffmpeg_windows_test.go
Normal file
35
ffmpeg_windows_test.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package ffmpeg_go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompileWithOptions(t *testing.T) {
|
||||||
|
out := Input("dummy.mp4").Output("dummy2.mp4")
|
||||||
|
cmd := out.Compile(func(s *Stream, cmd *exec.Cmd) {
|
||||||
|
if cmd.SysProcAttr == nil {
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
||||||
|
}
|
||||||
|
cmd.SysProcAttr.HideWindow = true
|
||||||
|
})
|
||||||
|
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)
|
||||||
|
}
|
3
probe.go
3
probe.go
@@ -34,6 +34,9 @@ func ProbeWithTimeoutExec(fileName string, timeOut time.Duration, kwargs KwArgs)
|
|||||||
cmd := exec.CommandContext(ctx, "ffprobe", args...)
|
cmd := exec.CommandContext(ctx, "ffprobe", args...)
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
cmd.Stdout = buf
|
cmd.Stdout = buf
|
||||||
|
for _, option := range GlobalCommandOptions {
|
||||||
|
option(cmd)
|
||||||
|
}
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
20
run.go
20
run.go
@@ -9,7 +9,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -237,16 +236,11 @@ func (s *Stream) ErrorToStdOut() *Stream {
|
|||||||
return s.WithErrorOutput(os.Stdout)
|
return s.WithErrorOutput(os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompilationOption func(s *Stream, cmd *exec.Cmd)
|
type CommandOption func(cmd *exec.Cmd)
|
||||||
|
|
||||||
// SeparateProcessGroup ensures that the command is run in a separate process
|
var GlobalCommandOptions = make([]CommandOption, 0)
|
||||||
// group. This is useful to enable handling of signals such as SIGINT without
|
|
||||||
// propagating them to the ffmpeg process.
|
type CompilationOption func(s *Stream, cmd *exec.Cmd)
|
||||||
func SeparateProcessGroup() CompilationOption {
|
|
||||||
return func(s *Stream, cmd *exec.Cmd) {
|
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) SetFfmpegPath(path string) *Stream {
|
func (s *Stream) SetFfmpegPath(path string) *Stream {
|
||||||
s.FfmpegPath = path
|
s.FfmpegPath = path
|
||||||
@@ -271,10 +265,10 @@ func (s *Stream) Compile(options ...CompilationOption) *exec.Cmd {
|
|||||||
if a, ok := s.Context.Value("Stderr").(io.Writer); ok {
|
if a, ok := s.Context.Value("Stderr").(io.Writer); ok {
|
||||||
cmd.Stderr = a
|
cmd.Stderr = a
|
||||||
}
|
}
|
||||||
for _, option := range options {
|
for _, option := range GlobalCommandOptions {
|
||||||
option(s, cmd)
|
option(cmd)
|
||||||
}
|
}
|
||||||
if LogCompiledCommand {
|
if LogCompiledCommand {
|
||||||
log.Printf("compiled command: ffmpeg %s\n", strings.Join(args, " "))
|
log.Printf("compiled command: ffmpeg %s\n", strings.Join(args, " "))
|
||||||
}
|
}
|
||||||
return cmd
|
return cmd
|
||||||
|
@@ -140,3 +140,12 @@ func (s *Stream) RunLinux() error {
|
|||||||
|
|
||||||
return cmd.Wait()
|
return cmd.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SeparateProcessGroup ensures that the command is run in a separate process
|
||||||
|
// group. This is useful to enable handling of signals such as SIGINT without
|
||||||
|
// propagating them to the ffmpeg process.
|
||||||
|
func SeparateProcessGroup() CompilationOption {
|
||||||
|
return func(s *Stream, cmd *exec.Cmd) {
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user