@@ -3,6 +3,7 @@ package ffmpeg
33import (
44 "bufio"
55 "bytes"
6+ "context"
67 "encoding/json"
78 "errors"
89 "fmt"
@@ -28,6 +29,7 @@ type Transcoder struct {
2829 outputPipeReader * io.ReadCloser
2930 inputPipeWriter * io.WriteCloser
3031 outputPipeWriter * io.WriteCloser
32+ commandContext * context.Context
3133}
3234
3335// New ...
@@ -82,7 +84,15 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
8284 }
8385
8486 // 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+ }
8696
8797 // If progresss enabled, get stderr pipe and start progress process
8898 if t .config .ProgressEnabled && ! t .config .Verbose {
@@ -160,6 +170,14 @@ func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.T
160170 return t
161171}
162172
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+
163181// validate ...
164182func (t * Transcoder ) validate () error {
165183 if t .config .FfmpegBinPath == "" {
@@ -192,7 +210,7 @@ func (t *Transcoder) validate() error {
192210}
193211
194212// GetMetadata Returns metadata for the specified input file
195- func (t * Transcoder ) GetMetadata () ( transcoder.Metadata , error ) {
213+ func (t * Transcoder ) GetMetadata () (transcoder.Metadata , error ) {
196214
197215 if t .config .FfprobeBinPath != "" {
198216 var outb , errb bytes.Buffer
0 commit comments