This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmediainfo.go
116 lines (100 loc) · 2.69 KB
/
mediainfo.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
package mediainfo
// #cgo CFLAGS: -DUNICODE
// #cgo LDFLAGS: -lz -lzen -lpthread -lstdc++ -lmediainfo -ldl
// #include "go_mediainfo.h"
import "C"
import (
"fmt"
"runtime"
"strconv"
"unsafe"
)
// MediaInfo - represents MediaInfo class, all interaction with libmediainfo through it
type MediaInfo struct {
handle unsafe.Pointer
}
func init() {
C.setlocale(C.LC_CTYPE, C.CString(""))
C.MediaInfoDLL_Load()
if C.MediaInfoDLL_IsLoaded() == 0 {
panic("Cannot load mediainfo")
}
}
// NewMediaInfo - constructs new MediaInfo
func NewMediaInfo() *MediaInfo {
result := &MediaInfo{handle: C.GoMediaInfo_New()}
runtime.SetFinalizer(result, func(f *MediaInfo) {
f.Close()
C.GoMediaInfo_Delete(f.handle)
})
return result
}
// OpenFile - opens file
func (mi *MediaInfo) OpenFile(path string) error {
p := C.CString(path)
s := C.GoMediaInfo_OpenFile(mi.handle, p)
if s == 0 {
return fmt.Errorf("MediaInfo can't open file: %s", path)
}
C.free(unsafe.Pointer(p))
return nil
}
// OpenMemory - opens memory buffer
func (mi *MediaInfo) OpenMemory(bytes []byte) error {
if len(bytes) == 0 {
return fmt.Errorf("Buffer is empty")
}
s := C.GoMediaInfo_OpenMemory(mi.handle, (*C.char)(unsafe.Pointer(&bytes[0])), C.size_t(len(bytes)))
if s == 0 {
return fmt.Errorf("MediaInfo can't open memory buffer")
}
return nil
}
// Close - closes file
func (mi *MediaInfo) Close() {
C.GoMediaInfo_Close(mi.handle)
}
// Get - allow to read info from file
func (mi *MediaInfo) Get(param string) (result string) {
p := C.CString(param)
r := C.GoMediaInfoGet(mi.handle, p)
result = C.GoString(r)
C.free(unsafe.Pointer(p))
C.free(unsafe.Pointer(r))
return
}
// Inform returns string with summary file information, like mediainfo util
func (mi *MediaInfo) Inform() (result string) {
r := C.GoMediaInfoInform(mi.handle)
result = C.GoString(r)
C.free(unsafe.Pointer(r))
return
}
// Option configure or get information about MediaInfoLib
func (mi *MediaInfo) Option(option string, value string) (result string) {
o := C.CString(option)
v := C.CString(value)
r := C.GoMediaInfoOption(mi.handle, o, v)
C.free(unsafe.Pointer(o))
C.free(unsafe.Pointer(v))
result = C.GoString(r)
C.free(unsafe.Pointer(r))
return
}
// AvailableParameters returns string with all available Get params and it's descriptions
func (mi *MediaInfo) AvailableParameters() string {
return mi.Option("Info_Parameters", "")
}
// Duration returns file duration
func (mi *MediaInfo) Duration() int {
duration, _ := strconv.Atoi(mi.Get("Duration"))
return duration
}
// Codec returns file codec
func (mi *MediaInfo) Codec() string {
return mi.Get("Codec")
}
// Format returns file codec
func (mi *MediaInfo) Format() string {
return mi.Get("Format")
}