mirror of
https://github.com/zoriya/go-mediainfo.git
synced 2025-12-06 06:06:09 +00:00
init
This commit is contained in:
47
README.md
Normal file
47
README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# go-mediainfo
|
||||
|
||||
Go bindings for [MediaInfo](https://github.com/MediaArea/MediaInfoLib)
|
||||
|
||||
## Dependencies
|
||||
|
||||
### ubuntu 18.04
|
||||
|
||||
```bash
|
||||
sudo apt-get install libmediainfo0v5 libmediainfo-dev
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Open file
|
||||
|
||||
```go
|
||||
file, err := mediainfo.Open("/file/path/filename.mp4")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
```
|
||||
|
||||
### Bitrate
|
||||
|
||||
```go
|
||||
videoBitrate := file.Parameter(mediainfo.StreamVideo, 0, "BitRate")
|
||||
audioBitrateTrack1 := file.Parameter(mediainfo.StreamAudio, 0, "BitRate")
|
||||
audioBitrateTrack2 := file.Parameter(mediainfo.StreamAudio, 1, "BitRate")
|
||||
```
|
||||
|
||||
### Stream count
|
||||
|
||||
```go
|
||||
audioTracks := file.Parameter(mediainfo.StreamAudio, 0, "StreamCount")
|
||||
```
|
||||
|
||||
### Available parameters
|
||||
|
||||
```go
|
||||
parameters := file.Option("info_parameters", "")
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [aleksandr@zelenin.me](mailto:aleksandr@zelenin.me)
|
||||
103
mediainfo.go
Normal file
103
mediainfo.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package mediainfo
|
||||
|
||||
// #include <mediainfo.h>
|
||||
// #cgo CFLAGS: -DUNICODE
|
||||
// #cgo LDFLAGS: -ldl -lmediainfo
|
||||
import "C"
|
||||
import (
|
||||
"unsafe"
|
||||
"errors"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type StreamKind int
|
||||
|
||||
const (
|
||||
StreamGeneral StreamKind = iota
|
||||
StreamVideo
|
||||
StreamAudio
|
||||
StreamText
|
||||
StreamOther
|
||||
StreamImage
|
||||
StreamMenu
|
||||
streamMax
|
||||
)
|
||||
|
||||
type InfoKind int
|
||||
|
||||
const (
|
||||
// Unique name of parameter
|
||||
InfoName InfoKind = iota
|
||||
// Value of parameter
|
||||
InfoText
|
||||
// Unique name of measure unit of parameter
|
||||
InfoMeasure
|
||||
// See infooptions_t
|
||||
InfoOptions
|
||||
// Translated name of parameter
|
||||
InfoNameText
|
||||
// Translated name of measure unit
|
||||
InfoMeasureText
|
||||
// More information about the parameter
|
||||
InfoInfo
|
||||
// Information : how data is found
|
||||
InfoHowTo
|
||||
infoMax
|
||||
)
|
||||
|
||||
func init() {
|
||||
C.MediaInfoDLL_Load()
|
||||
}
|
||||
|
||||
type File struct {
|
||||
handle unsafe.Pointer
|
||||
}
|
||||
|
||||
func (file *File) Close() {
|
||||
C.CloseFile(file.handle)
|
||||
}
|
||||
|
||||
func (file *File) Inform() string {
|
||||
return C.GoString(C.Inform(file.handle, 0))
|
||||
}
|
||||
|
||||
func (file *File) Get(streamKind StreamKind, streamNumber int, parameter string, infoKind InfoKind) string {
|
||||
cParameter := C.CString(parameter)
|
||||
defer C.free(unsafe.Pointer(cParameter))
|
||||
|
||||
return C.GoString(C.Get(file.handle, C.MediaInfo_stream_C(streamKind), C.size_t(streamNumber), cParameter, C.MediaInfo_info_C(infoKind), C.MediaInfo_info_C(0)))
|
||||
}
|
||||
|
||||
func (file *File) Parameter(streamKind StreamKind, streamNumber int, parameter string) string {
|
||||
return file.Get(streamKind, streamNumber, parameter, InfoText)
|
||||
}
|
||||
|
||||
func (file *File) Option(parameter string, value string) string {
|
||||
cParameter := C.CString(parameter)
|
||||
defer C.free(unsafe.Pointer(cParameter))
|
||||
|
||||
cValue := C.CString(value)
|
||||
defer C.free(unsafe.Pointer(cValue))
|
||||
|
||||
return C.GoString(C.Option(file.handle, cParameter, cValue))
|
||||
}
|
||||
|
||||
func Open(filePath string) (*File, error) {
|
||||
cFilePath := C.CString(filePath)
|
||||
defer C.free(unsafe.Pointer(cFilePath))
|
||||
|
||||
handle := C.OpenFile(C.CString(filePath))
|
||||
if handle == nil {
|
||||
return nil, errors.New("Cannot open file.")
|
||||
}
|
||||
|
||||
file := &File{
|
||||
handle: handle,
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(file, func(file *File) {
|
||||
file.Close()
|
||||
})
|
||||
|
||||
return file, nil
|
||||
}
|
||||
58
mediainfo.h
Normal file
58
mediainfo.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include <MediaInfoDLL/MediaInfoDLL.h>
|
||||
|
||||
const wchar_t *toWchar(const char *c)
|
||||
{
|
||||
const size_t cSize = strlen(c)+1;
|
||||
wchar_t* wc = malloc(cSize * sizeof(wchar_t));
|
||||
mbstowcs(wc, c, cSize);
|
||||
|
||||
return wc;
|
||||
}
|
||||
|
||||
const char *toChar(const wchar_t *c)
|
||||
{
|
||||
const size_t cSize = wcslen(c)+1;
|
||||
char* wc = malloc(cSize * sizeof(char));
|
||||
wcstombs(wc, c, cSize);
|
||||
|
||||
return wc;
|
||||
}
|
||||
|
||||
void *OpenFile(char *filePath)
|
||||
{
|
||||
void* handle = MediaInfo_New();
|
||||
if (!handle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t res = MediaInfo_Open(handle, toWchar(filePath));
|
||||
if (!res) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void CloseFile(void *handle)
|
||||
{
|
||||
MediaInfo_Close(handle);
|
||||
MediaInfo_Delete(handle);
|
||||
}
|
||||
|
||||
const char* Get(void *handle, MediaInfo_stream_C streamKind, size_t StreamNumber, char* Parameter, MediaInfo_info_C KindOfInfo, MediaInfo_info_C KindOfSearch)
|
||||
{
|
||||
return toChar(MediaInfo_Get(handle, streamKind, StreamNumber, toWchar(Parameter), KindOfInfo, KindOfSearch));
|
||||
}
|
||||
|
||||
const char* Option(void *handle, char* Parameter, char* Value)
|
||||
{
|
||||
return toChar(MediaInfo_Option(handle, toWchar(Parameter), toWchar(Value)));
|
||||
}
|
||||
|
||||
const char* Inform(void *handle, size_t Reserved)
|
||||
{
|
||||
return toChar(MediaInfo_Inform(handle, Reserved));
|
||||
}
|
||||
Reference in New Issue
Block a user