I have a question about accessing libmpv properties in Qt
-
Problem descriptionL:
"I've been working on a small project, creating a demo for a video player. I'm trying to access the frame rate of the currently playing video using mpv_observe_property(mpv, 0, "estimated-vf-fps", MPV_FORMAT_DOUBLE);. It works fine in the callback mpv_events and I can retrieve the frame rate. However, the issue arises when I also try to observe the video time information using mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE); during program initialization. This seems to cause the program to crash. It appears that I can only observe one property at a time when the mpv object is created. How can I solve this problem?"
Below is the relevant code snippet:
void MpvPlayer::create_mvpPlayer(QWidget *videoWin) { // create mpv instance mpv = mpv_create(); if (!mpv) throw std::runtime_error("can't create mpv instance"); int64_t wid = videoWin->winId(); mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid); mpv_observe_property(mpv, 0, "time-pos", MPV_FORMAT_DOUBLE); //mpv_observe_property(mpv, 0, "estimated-vf-fps", MPV_FORMAT_DOUBLE); connect(this, &MpvPlayer::mpv_events, this, &MpvPlayer::on_mpv_events, Qt::QueuedConnection); mpv_set_wakeup_callback(mpv, wakeup, this); if (mpv_initialize(mpv) < 0) throw std::runtime_error("mpv to initialize failed !"); } void MpvPlayer::on_mpv_events() { // All events are processed until the event queue is empty while (mpv) { mpv_event *event = mpv_wait_event(mpv, 0); if (event->event_id == MPV_EVENT_NONE) break; handle_mpv_event(event); } } void MpvPlayer::handle_mpv_event(mpv_event *event) { switch (event->event_id) { case MPV_EVENT_PROPERTY_CHANGE: { mpv_event_property *prop = (mpv_event_property *)event->data; if (strcmp(prop->name, "time-pos") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double *)prop->data; //qDebug() << "time: " << QString::number(time, 10, 2); //qDebug() << prop->name << prop->format << prop->data; // qDebug() << getProperty("time-pos"); //qDebug() <<getProperty("playback-time"); //qDebug()<<getProperty("duration"); double totalTime = getProperty("duration").toDouble(); double passTime = time; emit mpv_sendTimeInformat(totalTime,passTime); } else if (prop->format == MPV_FORMAT_NONE) { emit mpv_palyEnd(); qDebug() << "End of playback"; } } else if(strcmp(prop->name, "estimated-vf-fps") == 0) { double estimated_fps = *(double *)prop->data; emit fpsChanged(estimated_fps); } } break; // palyer Exit event triggering case MPV_EVENT_SHUTDOWN: { mpv_terminate_destroy(mpv); mpv = NULL; } break; default: ; } }
-
@CodeFarmer There's a library in the KDE landscape that you can have a look at :)
-