Skip to content

Commit 916ec43

Browse files
committed
feat: Add RunWithContext and BuildWithContext to support context.
1 parent a9c6eb9 commit 916ec43

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ err := fluentffmpeg.NewCommand("").
2828
Run()
2929
```
3030

31+
You could use `context` to set the timeout:
32+
33+
```go
34+
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5)
35+
defer cancel()
36+
err := fluentffmpeg.NewCommand("").
37+
InputPath("/path/to/video.avi").
38+
OutputFormat("mp4").
39+
OutputPath("/path/to/video.mp4").
40+
RunWithContext(ctx)
41+
```
42+
3143
If you want to view the errors/logs returned from FFmpeg, provide an io.Writer to receive the data.
3244
```go
3345
buf := &bytes.Buffer{}
@@ -37,7 +49,7 @@ err := fluentffmpeg.NewCommand("").
3749
OutputPath("./video.mp4").
3850
Overwrite(true).
3951
OutputLogs(buf). // provide a io.Writer
40-
Run()
52+
Run()
4153

4254
out, _ := ioutil.ReadAll(buf) // read logs
4355
fmt.Println(string(out))
@@ -48,11 +60,11 @@ You can also get the command in the form of an [exec.Cmd](https://golang.org/pkg
4860
```go
4961
done := make(chan error, 1)
5062
cmd := fluentffmpeg.NewCommand("").
51-
InputPath("./video.avi").
52-
OutputFormat("mp4").
53-
OutputPath("./video.mp4").
54-
Overwrite(true).
55-
Build()
63+
InputPath("./video.avi").
64+
OutputFormat("mp4").
65+
OutputPath("./video.mp4").
66+
Overwrite(true).
67+
Build()
5668
cmd.Start()
5769

5870
go func() {

command.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fluentffmpeg
22

33
import (
4+
"context"
45
"io"
56
"os/exec"
67
"reflect"
@@ -35,9 +36,19 @@ func (c *Command) Run() error {
3536
return c.Build().Run()
3637
}
3738

39+
// RunWithContext is like Run but includes a context which is used to kill the process
40+
func (c *Command) RunWithContext(ctx context.Context) error {
41+
return c.BuildWithContext(ctx).Run()
42+
}
43+
3844
// Build returns an exec.Cmd struct ready to run the FFmpeg command with its arguments
3945
func (c *Command) Build() *exec.Cmd {
40-
cmd := exec.Command(c.FFmpegPath, c.GetArgs()...)
46+
return c.BuildWithContext(context.Background())
47+
}
48+
49+
// BuildWithContext is like Build but includes a context which is used to kill the process
50+
func (c *Command) BuildWithContext(ctx context.Context) *exec.Cmd {
51+
cmd := exec.CommandContext(ctx, c.FFmpegPath, c.GetArgs()...)
4152

4253
if c.input != nil {
4354
cmd.Stdin = c.input

0 commit comments

Comments
 (0)