From 85e388203a003525e0ee5d6b1a2a121e67c236b4 Mon Sep 17 00:00:00 2001 From: Paul van Santen Date: Wed, 17 Apr 2019 10:50:32 +0200 Subject: [PATCH] Add new function to use a context with ffprobe instead of a plain timeout --- ffprobe.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ffprobe.go b/ffprobe.go index 43acb46..a48ddf2 100644 --- a/ffprobe.go +++ b/ffprobe.go @@ -2,6 +2,7 @@ package ffprobe import ( "bytes" + "context" "encoding/json" "errors" "os/exec" @@ -22,10 +23,20 @@ func SetFFProbeBinPath(newBinPath string) { binPath = newBinPath } -// GetProbeData is the main command used for probing the given media file using ffprobe. -// A timeout can be provided to kill the process if it takes too long to determine +// GetProbeData is used for probing the given media file using ffprobe with a set timeout. +// The timeout can be provided to kill the process if it takes too long to determine // the files information. +// Note: It is probably better to use Context with GetProbeDataContext() these days as it is more flexible. func GetProbeData(filePath string, timeout time.Duration) (data *ProbeData, err error) { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + return GetProbeDataContext(ctx, filePath) +} + +// GetProbeDataContext is the main command used for probing the given media file using ffprobe. +// It takes a context to allow killing the ffprobe process if it takes too long or in case of shutdown. +func GetProbeDataContext(ctx context.Context, filePath string) (data *ProbeData, err error) { cmd := exec.Command( binPath, "-v", "quiet", @@ -51,7 +62,7 @@ func GetProbeData(filePath string, timeout time.Duration) (data *ProbeData, err }() select { - case <-time.After(timeout): + case <-ctx.Done(): err = cmd.Process.Kill() if err == nil { return nil, ErrTimeout