Skip to content

Instantly share code, notes, and snippets.

@mykola2312
Created April 25, 2024 23:17
Show Gist options
  • Save mykola2312/da4b420448f309060c009c1fc8a426cf to your computer and use it in GitHub Desktop.
Save mykola2312/da4b420448f309060c009c1fc8a426cf to your computer and use it in GitHub Desktop.
mpv ipc socket client
#include <sys/un.h>
#include <sys/unistd.h>
#include <sys/socket.h>
#include <stdio.h>
int main() {
int sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr = {0};
if (sockfd < 0) {
perror("socket");
return 1;
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, "/tmp/mptv-mpv.sock");
if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect");
return 1;
}
// \n newline must be present at the end of json command to terminate it,
// otherwise MPV would not execute it.
const char* cmd = "{\"command\":[\"set_property\",\"volume\",\"0\"]}\n";
ssize_t sent = send(sockfd, cmd, strlen(cmd), 0);
printf("sent: %l\n", sent);
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment