-
Notifications
You must be signed in to change notification settings - Fork 45
/
ffprobe.go
97 lines (79 loc) · 2.84 KB
/
ffprobe.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ffprobe
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os/exec"
)
var binPath = "ffprobe"
// SetFFProbeBinPath sets the global path to find and execute the ffprobe program
func SetFFProbeBinPath(newBinPath string) {
binPath = newBinPath
}
// ProbeURL is used to probe the given media file using ffprobe. The URL can be a local path, a HTTP URL or any other
// protocol supported by ffprobe, see here for a full list: https://ffmpeg.org/ffmpeg-protocols.html
// This function takes a context to allow killing the ffprobe process if it takes too long or in case of shutdown.
// Any additional ffprobe parameter can be supplied as well using extraFFProbeOptions.
func ProbeURL(ctx context.Context, fileURL string, extraFFProbeOptions ...string) (data *ProbeData, err error) {
args := append([]string{
"-loglevel", "fatal",
"-print_format", "json",
"-show_format",
"-show_streams",
"-show_chapters",
}, extraFFProbeOptions...)
// Add the file argument
args = append(args, fileURL)
cmd := exec.CommandContext(ctx, binPath, args...)
cmd.SysProcAttr = procAttributes()
return runProbe(cmd)
}
// ProbeReader is used to probe a media file using an io.Reader. The reader is piped to the stdin of the ffprobe command
// and the data is returned.
// This function takes a context to allow killing the ffprobe process if it takes too long or in case of shutdown.
// Any additional ffprobe parameter can be supplied as well using extraFFProbeOptions.
func ProbeReader(ctx context.Context, reader io.Reader, extraFFProbeOptions ...string) (data *ProbeData, err error) {
args := append([]string{
"-loglevel", "fatal",
"-print_format", "json",
"-show_format",
"-show_streams",
"-show_chapters",
}, extraFFProbeOptions...)
// Add the file from stdin argument
args = append(args, "-")
cmd := exec.CommandContext(ctx, binPath, args...)
cmd.Stdin = reader
cmd.SysProcAttr = procAttributes()
return runProbe(cmd)
}
// runProbe takes the fully configured ffprobe command and executes it, returning the ffprobe data if everything went fine.
func runProbe(cmd *exec.Cmd) (data *ProbeData, err error) {
var outputBuf bytes.Buffer
var stdErr bytes.Buffer
cmd.Stdout = &outputBuf
cmd.Stderr = &stdErr
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("error running %s [%s] %w", binPath, stdErr.String(), err)
}
data = &ProbeData{}
err = json.Unmarshal(outputBuf.Bytes(), data)
if err != nil {
return data, fmt.Errorf("error parsing ffprobe output: %w", err)
}
if data.Format == nil {
return data, fmt.Errorf("no format data found in ffprobe output")
}
// Populate the old Tags structs for backwards compatibility purposes:
if len(data.Format.TagList) > 0 {
data.Format.Tags = &FormatTags{}
data.Format.Tags.setFrom(data.Format.TagList)
}
for _, str := range data.Streams {
str.Tags.setFrom(str.TagList)
}
return data, nil
}