Created
September 22, 2022 11:04
-
-
Save fstanis/19112c109f9c386a27c7712e87c8e02e to your computer and use it in GitHub Desktop.
libmpv/simple/simple.c rewritten in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include <stdlib.h> | |
#include <mpv/client.h> | |
#cgo pkg-config: mpv | |
const char* INPUT_DEFAULT_BINDING = "input-default-bindings"; | |
const char* INPUT_VO_KEYBOARD = "input-vo-keyboard"; | |
const char* YES = "yes"; | |
const char* OSC = "osc"; | |
const char* LOADFILE = "loadfile"; | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"unsafe" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
log.Fatalln("pass a single media file as argument") | |
} | |
ctx := C.mpv_create() | |
if ctx == nil { | |
log.Fatalln("failed creating context") | |
} | |
// Enable default key bindings, so the user can actually interact with | |
// the player (and e.g. close the window). | |
checkError(C.mpv_set_option_string(ctx, C.INPUT_DEFAULT_BINDING, C.YES)) | |
C.mpv_set_option_string(ctx, C.INPUT_VO_KEYBOARD, C.YES) | |
var val C.int = 1 | |
checkError(C.mpv_set_option(ctx, C.OSC, C.MPV_FORMAT_FLAG, unsafe.Pointer(&val))) | |
// Done setting up options. | |
checkError(C.mpv_initialize(ctx)) | |
// Play this file. | |
filename := C.CString(os.Args[1]) | |
cmd := [3]*C.char{C.LOADFILE, filename, nil} | |
checkError(C.mpv_command(ctx, &cmd[0])) | |
C.free(unsafe.Pointer(filename)) | |
// Let it play, and wait until the user quits. | |
for { | |
event := C.mpv_wait_event(ctx, 10000) | |
fmt.Printf("event: %s\n", C.GoString(C.mpv_event_name(event.event_id))) | |
if event.event_id == C.MPV_EVENT_SHUTDOWN { | |
break | |
} | |
} | |
C.mpv_terminate_destroy(ctx) | |
} | |
func checkError(status C.int) { | |
if status < 0 { | |
log.Fatalf("mpv API error: %s\n", C.GoString(C.mpv_error_string(status))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment