Skip to content

Instantly share code, notes, and snippets.

@Atreyagaurav
Created January 21, 2021 12:34
Show Gist options
  • Save Atreyagaurav/ff303a908610ea7acbc665a256e8e5e0 to your computer and use it in GitHub Desktop.
Save Atreyagaurav/ff303a908610ea7acbc665a256e8e5e0 to your computer and use it in GitHub Desktop.
// Build with: gcc -o plugin.so plugin.c `pkg-config --cflags mpv` -shared -fPIC
// Warning: do not link against libmpv.so! Read:
// https://mpv.io/manual/master/#linkage-to-libmpv
// The pkg-config call is for adding the proper client.h include path.
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <mpv/client.h>
#define INFO_DIR "/tmp/mpvinfo/"
#define LOG_FILE "/home/gaurav/.log/mpv.log"
#define MAX_CHAR 50
struct status {
char *fname;
double cur, dur, perc;
} st;
void write_time(FILE *fp, int time_sec){
if (time_sec < 60*60){
fprintf(fp, "%02d:%02d", time_sec/60, time_sec % 60);
}else{
fprintf(fp, "%d:%02d:%02d", time_sec/3600 ,(time_sec%3600)/60, time_sec % 60);
}
}
void shorten_string(char *str, int len){
int total;
total = strlen(str);
if(total < len){
return;
}
int i, pre, post;
pre = len * 3 / 4;
post = len - pre - 3;
for(i=0; i<3; i++){
*(str+pre+i)= '.';
}
strcpy(str+pre+3,str+total-post);
}
void info_filename(int number, char *dest){
strcpy(dest, INFO_DIR);
char count[5];
int i;
count[4] = '\0';
for(i=3;i>=0;i--){
count[i] = number % 10 + '0';
number /= 10;
}
int l = strlen(dest);
if (*(dest+l-1) == '/'){
strcpy(dest+l,count);
}else{
*(dest+l) = '/';
strcpy(dest+l+1, count);
}
}
void new_info_file(char *filename){
int i = 1;
while (1){
info_filename(i++, filename);
if (access(filename, F_OK) == 0){
continue;
}
return;
}
}
void read_status(mpv_handle * handle){
mpv_get_property(handle, "media-title", MPV_FORMAT_STRING, &(st.fname));
mpv_get_property(handle, "time-pos", MPV_FORMAT_DOUBLE, &(st.cur));
mpv_get_property(handle, "duration", MPV_FORMAT_DOUBLE, &(st.dur));
mpv_get_property(handle, "percent-pos", MPV_FORMAT_DOUBLE, &(st.perc));
}
void write_status(char *filename, mpv_handle *handle){
FILE *fp;
char fname[MAX_CHAR+1];
/* shorten_string(st.fname, MAX_CHAR); */
fp = fopen(filename, "w");
fprintf(fp,"%s\n TIME:",st.fname);
write_time(fp, (int)st.cur);
fprintf(fp," of ");
write_time(fp, (int)st.dur);
fprintf(fp,"(%.2f%%)\n", st.perc);
fclose(fp);
}
void clear_status(char *filename) { remove(filename); }
void write_log(const char *action){
FILE *fp;
time_t t1;
t1 = time(NULL);
fp = fopen(LOG_FILE, "a");
fprintf(fp, "%ld %s ",t1,action);
write_time(fp, (int)st.cur);
fprintf(fp, " ");
write_time(fp, (int)st.dur);
fprintf(fp," %s\n",st.fname);
fclose(fp);
}
int mpv_open_cplugin(mpv_handle *handle)
{
mkdir(INFO_DIR, 0700);
char filename[128];
int on_seek = 0;
new_info_file(filename);
printf("Status Update file: %s\n", filename);
mpv_observe_property(handle, 0, "playback-time", MPV_FORMAT_DOUBLE);
mpv_observe_property(handle, 0, "pause", MPV_FORMAT_FLAG);
mpv_event_property *prop;
int flag = 1, started = 0;
while (flag) {
mpv_event *event = mpv_wait_event(handle, -1);
switch (event->event_id){
case MPV_EVENT_SHUTDOWN:
flag = 0;
break;
case MPV_EVENT_FILE_LOADED:
started = 1;
read_status(handle);
write_log(mpv_event_name(event->event_id));
break;
case MPV_EVENT_END_FILE:
if (started==0) continue;
started = 0;
write_log(mpv_event_name(event->event_id));
break;
case MPV_EVENT_SEEK:
if (on_seek) continue;
on_seek = 1;
write_log(mpv_event_name(event->event_id));
break;
case MPV_EVENT_PLAYBACK_RESTART:
if (on_seek == 0) continue;
on_seek = 0;
read_status(handle);
write_log(mpv_event_name(event->event_id));
break;
case MPV_EVENT_PROPERTY_CHANGE:
prop = (mpv_event_property *)event->data;
if (strcmp(prop->name, "playback-time") == 0) {
read_status(handle);
write_status(filename, handle);
}else if (strcmp(prop->name, "pause") == 0) {
if (started) write_log(*(int*)prop->data ? "pause":"unpause");
}
break;
default:
break;
}
}
clear_status(filename);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment