-
Notifications
You must be signed in to change notification settings - Fork 172
/
probe.go
48 lines (43 loc) · 1.23 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ffmpeg_go
import (
"bytes"
"context"
"fmt"
"os/exec"
"time"
)
// Probe Run ffprobe on the specified file and return a JSON representation of the output.
func Probe(fileName string, kwargs ...KwArgs) (string, error) {
return ProbeWithTimeout(fileName, 0, MergeKwArgs(kwargs))
}
func ProbeWithTimeout(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
args := KwArgs{
"show_format": "",
"show_streams": "",
"of": "json",
}
return ProbeWithTimeoutExec(fileName, timeOut, MergeKwArgs([]KwArgs{args, kwargs}))
}
func ProbeWithTimeoutExec(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
args := ConvertKwargsToCmdLineArgs(kwargs)
args = append(args, fileName)
ctx := context.Background()
if timeOut > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(context.Background(), timeOut)
defer cancel()
}
cmd := exec.CommandContext(ctx, "ffprobe", args...)
buf := bytes.NewBuffer(nil)
stdErrBuf := bytes.NewBuffer(nil)
cmd.Stdout = buf
cmd.Stderr = stdErrBuf
for _, option := range GlobalCommandOptions {
option(cmd)
}
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("[%s] %w", string(stdErrBuf.Bytes()), err)
}
return string(buf.Bytes()), nil
}