forked from vansante/go-ffprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffprobe.go
158 lines (128 loc) · 4.17 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ffprobe
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os/exec"
"strconv"
)
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",
}, 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",
}, 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)
}
if stdErr.Len() > 0 {
return nil, fmt.Errorf("ffprobe error: %s", stdErr.String())
}
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
}
func ProbeKeyframes(ctx context.Context, reader io.Reader, streams string, extraFFProbeOptions ...string) (data []float64, err error) {
if streams == "" {
streams = "v"
}
args := append([]string{
"-select_streams", streams,
"-print_format", "csv",
"-show_entries", "frame=pict_type,pts_time",
"-skip_frame", "nokey",
"-loglevel", "fatal",
}, extraFFProbeOptions...)
// Add the file argument
args = append(args, "-")
cmd := exec.CommandContext(ctx, binPath, args...)
cmd.Stdin = reader
cmd.SysProcAttr = procAttributes()
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)
}
if stdErr.Len() > 0 {
return nil, fmt.Errorf("ffprobe error: %s", stdErr.String())
}
data = []float64{}
r := csv.NewReader(&outputBuf)
r.FieldsPerRecord = -1
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return data, fmt.Errorf("error parsing csv: %w %s", err, record)
}
val, err := strconv.ParseFloat(record[1], 64)
if err != nil {
return data, fmt.Errorf("error parsing float64: %w %s", err, record)
}
data = append(data, val)
}
return data, nil
}