Skip to content

Commit

Permalink
Merge pull request #48 from agorman/v2
Browse files Browse the repository at this point in the history
Returning stderr in error when ffprobe fails
vansante authored Mar 1, 2024
2 parents 379d89b + afdf69b commit f3db26b
Showing 3 changed files with 45 additions and 5 deletions.
Empty file added assets/test.avi
Empty file.
4 changes: 0 additions & 4 deletions ffprobe.go
Original file line number Diff line number Diff line change
@@ -72,10 +72,6 @@ func runProbe(cmd *exec.Cmd) (data *ProbeData, err error) {
return nil, fmt.Errorf("error running %s [%s] %w", binPath, stdErr.String(), err)
}

if stdErr.Len() > 0 {
return nil, fmt.Errorf("ffprobe error: %s", stdErr.String())
}

data = &ProbeData{}
err = json.Unmarshal(outputBuf.Bytes(), data)
if err != nil {
46 changes: 45 additions & 1 deletion ffprobe_test.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,14 @@ import (
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
)

const (
testPath = "assets/test.mp4"
testPath = "assets/test.mp4"
testPathError = "assets/test.avi"
)

func Test_ProbeURL(t *testing.T) {
@@ -25,6 +27,20 @@ func Test_ProbeURL(t *testing.T) {
validateData(t, data)
}

func Test_ProbeURL_Error(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()

_, err := ProbeURL(ctx, testPathError, "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}

if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}

func Test_ProbeURL_HTTP(t *testing.T) {
const testPort = 20811

@@ -47,6 +63,15 @@ func Test_ProbeURL_HTTP(t *testing.T) {
}

validateData(t, data)

_, err = ProbeURL(ctx, fmt.Sprintf("http://127.0.0.1:%d/test.avi", testPort), "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}

if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}

func Test_ProbeReader(t *testing.T) {
@@ -66,6 +91,25 @@ func Test_ProbeReader(t *testing.T) {
validateData(t, data)
}

func Test_ProbeReader_Error(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()

fileReader, err := os.Open(testPathError)
if err != nil {
t.Errorf("Error opening test file: %v", err)
}

_, err = ProbeReader(ctx, fileReader, "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}

if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}

func validateData(t *testing.T, data *ProbeData) {
// test ProbeData.GetStream
stream := data.StreamType(StreamVideo)

0 comments on commit f3db26b

Please sign in to comment.