@@ -3,6 +3,7 @@ package ffmpeg
3
3
import (
4
4
"bufio"
5
5
"bytes"
6
+ "context"
6
7
"encoding/json"
7
8
"errors"
8
9
"fmt"
@@ -28,6 +29,7 @@ type Transcoder struct {
28
29
outputPipeReader * io.ReadCloser
29
30
inputPipeWriter * io.WriteCloser
30
31
outputPipeWriter * io.WriteCloser
32
+ commandContext * context.Context
31
33
}
32
34
33
35
// New ...
@@ -82,7 +84,15 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
82
84
}
83
85
84
86
// Initialize command
85
- cmd := exec .Command (t .config .FfmpegBinPath , args ... )
87
+ // If a context object was supplied to this Transcoder before
88
+ // starting, use this context when creating the command to allow
89
+ // the command to be killed when the context expires
90
+ var cmd * exec.Cmd
91
+ if t .commandContext == nil {
92
+ cmd = exec .Command (t .config .FfmpegBinPath , args ... )
93
+ } else {
94
+ cmd = exec .CommandContext (* t .commandContext , t .config .FfmpegBinPath , args ... )
95
+ }
86
96
87
97
// If progresss enabled, get stderr pipe and start progress process
88
98
if t .config .ProgressEnabled && ! t .config .Verbose {
@@ -160,6 +170,14 @@ func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.T
160
170
return t
161
171
}
162
172
173
+ // WithContext is to be used on a Transcoder *before Starting* to
174
+ // pass in a context.Context object that can be used to kill
175
+ // a running transcoder process. Usage of this method is optional
176
+ func (t * Transcoder ) WithContext (ctx * context.Context ) transcoder.Transcoder {
177
+ t .commandContext = ctx
178
+ return t
179
+ }
180
+
163
181
// validate ...
164
182
func (t * Transcoder ) validate () error {
165
183
if t .config .FfmpegBinPath == "" {
@@ -192,7 +210,7 @@ func (t *Transcoder) validate() error {
192
210
}
193
211
194
212
// GetMetadata Returns metadata for the specified input file
195
- func (t * Transcoder ) GetMetadata () ( transcoder.Metadata , error ) {
213
+ func (t * Transcoder ) GetMetadata () (transcoder.Metadata , error ) {
196
214
197
215
if t .config .FfprobeBinPath != "" {
198
216
var outb , errb bytes.Buffer
0 commit comments