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

Commit 9367de1

Browse files
authored
Merge pull request #29 from hbomb79/master
feat(context): added WithContext method for Transcoder
2 parents 3106f44 + 4b93d4d commit 9367de1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

ffmpeg/ffmpeg.go

Lines changed: 20 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"
@@ -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 ...
164182
func (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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy