go-podcast-proxy/mpvconvert.go
caffeinelucy b4a978043e code
2024-08-03 20:05:08 +02:00

62 lines
1.6 KiB
Go

package main
import (
"log"
"path/filepath"
"os"
"unsafe"
)
// #cgo LDFLAGS: -lmpv
// #include <stdlib.h>
// #include <stdio.h>
// #include <mpv/client.h>
// mpv_handle* mpvinit(){
// return mpv_create();
// }
// int mpvbegin(mpv_handle* mpv, char* src, char* dst, char* ext) {
// mpv_set_property_string(mpv, "o", dst);
// mpv_set_property_string(mpv, "of", ext);
// mpv_set_property_string(mpv, "oacopts", "b=96k");
// mpv_initialize(mpv);
// const char *args[3] = {"loadfile", src, NULL};
// return mpv_command(mpv, args);
// }
// mpv_event* mpvwait(mpv_handle* mpv){
// return mpv_wait_event(mpv, 0.25);
// }
// void mpvend(mpv_handle* mpv){
// mpv_destroy(mpv);
// }
import "C"
func (ctx* ServerContext) MPVConvert(source string, destination string, ext string) {
defer ctx.WG.Done()
dir := filepath.Dir(destination)
os.MkdirAll(dir, os.ModePerm)
mpv := C.mpvinit()
csrc := C.CString(source)
defer C.free(unsafe.Pointer(csrc))
cdst := C.CString(destination)
defer C.free(unsafe.Pointer(cdst))
cext := C.CString(ext)
oacopts := C.CString("oacopts")
defer C.free(unsafe.Pointer(oacopts))
r := C.mpvbegin(mpv, csrc, cdst, cext)
if r!=0 {
log.Println("mpv error (", source, ", ", destination, ", ", ext, "): ", r)
}
for C.mpvwait(mpv).event_id!=C.MPV_EVENT_END_FILE {
if !ctx.IsActive {
C.mpvend(mpv)
os.Remove(destination)
log.Println("mpv cancelled (", source, ", ", destination, ", ", ext, ")")
return
}
}
log.Println("mpv success (", source, ", ", destination, ", ", ext, ")")
C.mpvend(mpv)
}