Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 8a5c071

Browse files
committed
Merge branch 'master' of github.com:/floostack/transcoder
2 parents 24e6bb5 + 9367de1 commit 8a5c071

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

ffmpeg/ffmpeg.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ffmpeg
33
import (
44
"bufio"
55
"bytes"
6+
"context"
67
"encoding/json"
78
"errors"
89
"fmt"
@@ -29,6 +30,7 @@ type Transcoder struct {
2930
outputPipeReader io.ReadCloser
3031
inputPipeWriter io.WriteCloser
3132
outputPipeWriter io.WriteCloser
33+
commandContext context.Context
3234
}
3335

3436
// New ...
@@ -90,7 +92,15 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
9092
}
9193

9294
// Initialize command
93-
cmd := exec.Command(t.config.FfmpegBinPath, args...)
95+
// If a context object was supplied to this Transcoder before
96+
// starting, use this context when creating the command to allow
97+
// the command to be killed when the context expires
98+
var cmd *exec.Cmd
99+
if t.commandContext == nil {
100+
cmd = exec.Command(t.config.FfmpegBinPath, args...)
101+
} else {
102+
cmd = exec.CommandContext(t.commandContext, t.config.FfmpegBinPath, args...)
103+
}
94104
cmd.Env = append(t.config.Env, os.Environ()...)
95105
cmd.Dir = t.config.Dir
96106

@@ -177,6 +187,14 @@ func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.T
177187
return t
178188
}
179189

190+
// WithContext is to be used on a Transcoder *before Starting* to
191+
// pass in a context.Context object that can be used to kill
192+
// a running transcoder process. Usage of this method is optional
193+
func (t *Transcoder) WithContext(ctx context.Context) transcoder.Transcoder {
194+
t.commandContext = ctx
195+
return t
196+
}
197+
180198
// validate ...
181199
func (t *Transcoder) validate() error {
182200
if t.config.FfmpegBinPath == "" {
@@ -222,7 +240,12 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
222240

223241
args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"}
224242

225-
cmd := exec.Command(t.config.FfprobeBinPath, args...)
243+
var cmd *exec.Cmd
244+
if t.commandContext == nil {
245+
cmd = exec.Command(t.config.FfprobeBinPath, args...)
246+
} else {
247+
cmd = exec.CommandContext(t.commandContext, t.config.FfprobeBinPath, args...)
248+
}
226249
cmd.Stdout = &outb
227250
cmd.Stderr = &errb
228251
cmd.Env = append(t.config.Env, os.Environ()...)

transcoder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transcoder
22

33
import (
4+
"context"
45
"io"
56
)
67

@@ -13,5 +14,6 @@ type Transcoder interface {
1314
OutputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder
1415
WithOptions(opts Options) Transcoder
1516
WithAdditionalOptions(opts Options) Transcoder
17+
WithContext(ctx *context.Context) Transcoder
1618
GetMetadata() (Metadata, error)
1719
}

0 commit comments

Comments
 (0)