Qt audio and video development 14-mpv read and control

I. Introduction

Use mpv to read the file information, and set the current playback progress, volume, mute, etc., which are the same as the functions encapsulated by vlc at the time, except that vlc is processed by calling the function interface, and mpv is processed by reading and setting attributes , Vlc supports timers or function methods in threads to read the state, and also supports event callbacks to get the corresponding state changes. Of course, mpv also supports it, and it is more convenient. How do you know the main workload or time spent? Attributes and their respective functional meanings are listed in the official website (http://mpv.io/manual/master/#options, http://mpv.io/manual/master/#list-of-input- commands, http://mpv.io/manual/master/#properties), but they are all in English, most programmers should be no difficulty, but the right mouse button can be translated into Chinese, haha, I believe many people I have done this. Many browsers support right-click menu translation by default. It is very convenient. When I consult many English documents, I use a lot of them, including Qt official documents and BUG report pages, but It is recommended to use English descriptions as much as possible when searching for problems, so that the search can be more precise.

Some commonly used attributes:

  1. Original video width height width height
  2. Width and height of the video after zooming dwidth dheight
  3. Save the video file stream-record is empty, it means stop recording
  4. Video aspect ratio video-aspect
  5. Pause playback pause yes means pause no means continue
  6. Video file duration
  7. Mute yes means mute no means non-mute
  8. Volume int value 0-100
  9. Get the playback progress time-pos
  10. Set playback progress seek
  11. Current audio track aid
  12. Track-list/count
  13. Screenshot-to-file

2. Features

  1. Multi-threaded real-time playing video stream + local video, etc.
  2. Support windows+linux+mac.
  3. Multi-threaded display images, not stuck in the main interface.
  4. Reconnect the webcam automatically.
  5. Can set whether to save to file and file name.
  6. You can drag files directly to the mpvwidget control to play.
  7. Support common video streams such as h265 video stream + rtmp.
  8. Can pause and resume playing.
  9. Support storage of single video files and timing storage of video files.
  10. Customize the top floating bar, send a click signal notification, and set whether to enable it.
  11. You can set the screen stretch fill or equal proportion fill.
  12. Can take screenshots (original pictures) and screenshots of videos.
  13. The video file stores MP4 files.
  14. Supports hard decoding such as qsv, dxva2, d3d11va, etc.

Three, renderings

Insert picture description here

Four, related sites

  1. Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
  2. International site: https://github.com/feiyangqingyun/QWidgetDemo
  3. Personal homepage: https://blog.csdn.net/feiyangqingyun
  4. Zhihu Homepage: https://www.zhihu.com/people/feiyangqingyun/
  5. Experience address: https://blog.csdn.net/feiyangqingyun/article/details/97565652

Five, the core code

void MpvThread::readMediaInfo()
{
    if (mpvPlayer != NULL) {
        QVariant width = getValue("width");
        QVariant height = getValue("height");
        videoWidth = width.toInt();
        videoHeight = height.toInt();
        qDebug() << TIMEMS << url << "videoWidth:" << videoWidth << "videoHeight:" << videoHeight;
    }
}

void MpvThread::readPlayInfo()
{
    //定义长度变量用于存储文件时长
    uint length = getLength();
    //定义变量存储声音大小,默认值1
    int volume = getVolume();
    //定义变量存储是否静音
    bool mute = getMute();

    //发送文件时长信号
    emit fileLengthReceive(length);
    //发送当前音量值信号
    emit fileVolumeReceive(volume, mute);

    //改变标志位启动读取播放进度
    if (!callbackevent) {
        isReadPosition = true;
    }
}

void MpvThread::readPosition()
{
    //获取播放进度位置
    int position = getPosition();
    //获取是否播放结束
    bool isPlay = (position != 0);
    if (position > 0 && !isRtsp) {
        emit filePositionReceive(position, isPlay);
    }

    //本地文件播放结束
    if (!isPlay && !isRtsp && suffix != "dshow") {
        this->stop();
    }
}

void MpvThread::setSize()
{
    if (mpvPlayer != NULL) {
        double width = playWidget->width();
        double height = playWidget->height();
        setValue("video-aspect", width / height);
    }
}

bool MpvThread::getIsPlaying()
{
    //在视频流模式下,不是暂停状态,当前的位置和上一次的位置一致则表示断了
    //进度为0表示没有播放成功也需要重新加载
    bool isPlay = this->isRunning();
    if (isPlay && isRtsp && !getValue("pause").toBool()) {
        int position = getPosition();
        if (position == 0 || this->position == position) {
            isPlay = false;
        }

        this->position = position;
    }

    return isPlay;
}

uint MpvThread::getLength()
{
    uint length = 0;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("duration");
        length = value.toDouble() * 1000;
    }

    return length;
}

uint MpvThread::getPosition()
{
    uint positon = 0;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("time-pos");
        positon = value.toDouble() * 1000;
    }

    return positon;
}

void MpvThread::setPosition(int position)
{
    if (mpvPlayer != NULL && !isRtsp) {
        command(QVariantList() << "seek" << position / 1000 << "absolute");
    }
}

bool MpvThread::getMute()
{
    bool ok = false;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("mute");
        ok = !value.toBool();
    }

    return ok;
}

void MpvThread::setMute(bool mute)
{
    if (mpvPlayer != NULL) {
        setValue("mute", mute ? "yes" : "no");
    }
}

int MpvThread::getVolume()
{
    int volume = 0;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("volume");
        volume = value.toInt();
    }

    return volume;
}

void MpvThread::setVolume(int volume)
{
    if (mpvPlayer != NULL) {
        setValue("volume", volume);
    }
}

int MpvThread::getTrack()
{
    int track = 0;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("aid");
        track = value.toInt();
    }

    return track;
}

int MpvThread::getTrackCount()
{
    int trackCount = 0;
    if (mpvPlayer != NULL) {
        QVariant value = getValue("track-list/count");
        trackCount = value.toInt();
    }

    return trackCount;
}

void MpvThread::setTrack(int track)
{
    if (mpvPlayer != NULL) {
        setValue("aid", track);
    }
}

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/108128093