mpv-player / mpv-examples

📚
224 stars 77 forks source link

mpv API zooming/panning commands usage #35

Open Greenday001 opened 4 years ago

Greenday001 commented 4 years ago

Hi I am very new to the mpv API interfaces which I want to embed into my application. I am now developing a simple program trying to perform sort of digital zooming commands like "video-zoom" and "video-pan-x" at run time. Based on the mpv-example/simple and input.conf/rst, I have wrote the following program running on Ubuntu 16.04:

// Build with: gcc -o simple simple.c pkg-config --libs --cflags mpv

include

include

include

include <mpv/client.h>

static inline void check_error(int status) { if (status < 0) { printf("mpv API error: %s\n", mpv_error_string(status)); exit(1); } }

int main(int argc, char argv[]) { if (argc != 2) { printf("pass a single media file as argument\n"); return 1; } mpv_handle ctx = mpv_create(); if (!ctx) { printf("failed creating context\n"); return 1; } check_error(mpv_set_option_string(ctx, "input-default-bindings", "yes")); mpv_set_option_string(ctx, "input-vo-keyboard", "yes"); check_error(mpv_initialize(ctx)); const char *cmd[] = {"loadfile", argv[1], NULL}; check_error(mpv_command(ctx, cmd));

int cnt = 0;
double zv;
double pv;
const char *cmd1[] = {"add", "video-zoom", "0.002", NULL};
const char *cmd2[] = {"add", "video-pan-x", "0.0005", NULL};
const char *cmd3[] = {"add", "video-pan-y", "0.0005", NULL};

// Play and do video-zoom at each frame
while (1) {
    mpv_event *event = mpv_wait_event(ctx, 0.01);
    if (cnt<500)  cnt++;  //proceed 500 steps
    zv=cnt/500.0;   //zoom 1/500 per step
    pv=cnt/2000.0;   //pan 1/2000 per step

    //----------do zooming and panning through setting and adding--------------//
    // check_error(mpv_set_property(ctx,"video-zoom",MPV_FORMAT_DOUBLE,&zv));  //this works as expected
    // check_error(mpv_set_property(ctx,"video-pan-x",MPV_FORMAT_DOUBLE,&pv)); //this works as expected
    // check_error(mpv_set_property(ctx,"video-pan-y",MPV_FORMAT_DOUBLE,&pv)); //this works as expected
    // check_error(mpv_set_property_string(ctx,"video-zoom","1"));  //this works as expected

    if (cnt<500){
        // check_error(mpv_set_option_string(ctx, "add video-zoom 0.002", NULL));   //mpv API error: option not found
        // check_error(mpv_set_option_string(ctx, "add video-pan-x 0.0005", NULL));   //mpv API error: option not found
        // check_error(mpv_set_option_string(ctx, "add video-pan-y 0.0005", NULL));   //mpv API error: option not found
        // check_error(mpv_command(ctx, cmd1));    //this works as expected
        // check_error(mpv_command(ctx, cmd2));    //this works as expected
        // check_error(mpv_command(ctx, cmd3));    //this works as expected
    }

    printf("%d event: %s\n", cnt, mpv_event_name(event->event_id));
    if (event->event_id == MPV_EVENT_SHUTDOWN)
        break;
}

mpv_terminate_destroy(ctx);
return 0;

}

1) as you can see, I have made most of APIs working except "mpv_set_option_string" reports error, so what's the correct syntax for it? Also could someone double check the others in terms of syntax and efficiency? 2) I tried this program to stream a web cam successful, however the zooming and panning are not working very smoothly, the view is kind of shaking randomly. I have not tried hardware acceleration yet. Any one can help optimize or provide suggestion? Thanks!