Skip to content

Commit

Permalink
Merge pull request #8 from vansante/context
Browse files Browse the repository at this point in the history
Add new function to use a context with ffprobe instead of a plain timeout
  • Loading branch information
vansante authored Apr 17, 2019
2 parents b4944c5 + 85e3882 commit d5721ef
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ffprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ffprobe

import (
"bytes"
"context"
"encoding/json"
"errors"
"os/exec"
Expand All @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit d5721ef

Please sign in to comment.