-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe.go
79 lines (66 loc) · 1.71 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
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
package probe
/*
#cgo pkg-config: libavformat libavcodec libavutil
#include "probe.h"
#include <stddef.h>
#include <stdlib.h>
#include <libavutil/common.h>
*/
import "C"
import (
"errors"
"unsafe"
)
// Option is a function that configures the probe.
type Option func(*Options)
// Options is a probe options.
type Options struct {
quiet int
}
// WithQuiet sets the quiet option.
func WithQuiet() Option {
return func(o *Options) {
o.quiet = 1
}
}
func applyOptions(opts []Option) *Options {
o := &Options{}
for _, opt := range opts {
opt(o)
}
return o
}
// Do probe multiple video streams.
func Do(inputs []string, opts ...Option) error {
o := applyOptions(opts)
inputsC := C.malloc(C.size_t(len(inputs)) * C.size_t(unsafe.Sizeof(uintptr(0))))
defer C.free(inputsC)
// convert the C array to a Go Array so we can index it
inputsCIndexable := (*[1<<30 - 1]*C.char)(inputsC)
for idx, input := range inputs {
inputsCIndexable[idx] = C.CString(input)
}
if err := C.probe(C.size_t(len(inputs)), (**C.char)(inputsC), C.int(o.quiet)); err != 0 {
if err == C.AVERROR_EOF {
return nil
}
buf := make([]byte, C.AV_ERROR_MAX_STRING_SIZE)
C.av_make_error_string((*C.char)(unsafe.Pointer(&buf[0])), C.AV_ERROR_MAX_STRING_SIZE, err)
return errors.New(string(buf))
}
return nil
}
// ContainsVideoOrAudio checks if the input contains video or audio.
func ContainsVideoOrAudio(input string) (bool, error) {
s := C.contains_video_or_audio(C.CString(input))
if s.err != 0 {
buf := make([]byte, C.AV_ERROR_MAX_STRING_SIZE)
C.av_make_error_string(
(*C.char)(unsafe.Pointer(&buf[0])),
C.AV_ERROR_MAX_STRING_SIZE,
s.err,
)
return false, errors.New(string(buf))
}
return s.contains_video_or_audio >= 1, nil
}