Qt线程封装FFmpeg播放器类

news/2024/7/10 20:26:05 标签: qt, ffmpeg, 开发语言

介绍

工作开发中需要处理的文件很多并无音频,针对这一场景,这里分享工作中自己封装使用的类库。精简的代码实现了播放、暂停、停止、快进、快退、进度更新跳转播放功能。直接放代码,方便后期复制使用。

代码

头文件

#ifndef VIDEOPLAYER_H
#define VIDEOPLAYER_H

#include <QThread>
#include <QDebug>
#include <QImage>

extern "C"
{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "libavutil/time.h"
}

class VideoDecoder : public QThread
{
    Q_OBJECT
public:
    explicit VideoDecoder(QObject *parent = nullptr);
    ~VideoDecoder();

    void startPlay(const QString &path);
    void stopPlay();

    void pausePlay(bool pause);
    void seekPlay(int sec);

signals:
    void sigDuration(int sec);
    void sigPlayPosition(int sec);
    void sigPlayFinished(int ret);

    void sigSendImage(const QImage &image);

protected:
    void run();

private:
    //运行标志
    volatile bool m_isRun = false;
    //暂停状态
    volatile bool m_pause = false;
    //进度跳转状态
    volatile bool m_seek = false;
    //优化跳转速度
    volatile bool m_seekFilter = false;

    //开始时间 ms单位
    int64_t m_startTime;
    //暂停时间 ms单位
    int64_t m_pauseTime;
    //跳转时间 ms单位
    int64_t m_seekTime;

    //文件路径
    QString m_filePath;
    //时长信息 秒
    int m_videoDuration;
};

#endif // VIDEOPLAYER_H

实现文件

#include "videoplayer.h"

VideoDecoder::VideoDecoder(QObject *parent) : QThread(parent)
{

}

VideoDecoder::~VideoDecoder()
{
    quit();
    wait();
}

void VideoDecoder::startPlay(const QString &path)
{
    m_filePath = path;
    m_isRun = true;
    m_pause = false;
    m_seek = false;

    this->start();
}

void VideoDecoder::stopPlay()
{
    m_pause = false;
    m_seek = false;
    m_isRun = false;
}

void VideoDecoder::pausePlay(bool pause)
{
    m_pause = pause;
    if(pause)
    {
        m_pauseTime = av_gettime_relative() / 1000;
    }
    else
    {
        int offset = av_gettime_relative() / 1000 - m_pauseTime;
        m_startTime += offset;
    }
}

void VideoDecoder::seekPlay(int sec)
{
    if(!m_isRun)
        return;

    if(m_videoDuration == sec)
        sec -= 2;

    m_seekTime = sec * 1000;
    m_seekFilter = true;
    m_seek = true;
}

void VideoDecoder::run()
{
    qDebug() << "VideoDecoder start" << m_filePath;

    std::string temp = m_filePath.toStdString();
    AVFormatContext *inFmtCtx = avformat_alloc_context();
    int ret = avformat_open_input(&inFmtCtx, temp.c_str(), NULL, NULL);
    if (ret < 0)
    {
        qDebug() << "open input error";
        return;
    }

    //获取流信息
    ret = avformat_find_stream_info(inFmtCtx, NULL);
    if (ret < 0)
    {
        qDebug() << "find stream info error";
        return;
    }

    //获取视频流信息 目前只有视频流
    bool getVideo = false;
    int videoIndex = av_find_best_stream(inFmtCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    AVStream *videoStream = NULL;
    AVCodec *videoDecoder = NULL;
    AVCodecContext *videoDeCodecCtx = NULL;
    if (videoIndex >= 0)
    {
        videoStream = inFmtCtx->streams[videoIndex];

        //初始化解码器
        videoDecoder = avcodec_find_decoder(videoStream->codecpar->codec_id);
        videoDeCodecCtx = avcodec_alloc_context3(videoDecoder);
        if(videoDeCodecCtx != NULL)
        {
            avcodec_parameters_to_context(videoDeCodecCtx, videoStream->codecpar);
            ret = avcodec_open2(videoDeCodecCtx, videoDecoder, NULL);
            if(ret < 0)
                avcodec_free_context(&videoDeCodecCtx);
            else
                getVideo = true;
        }
    }

    if(!getVideo)
    {
        avformat_close_input(&inFmtCtx);
        return;
    }

    AVFrame *swsFrame = av_frame_alloc();
    SwsContext *swsCtx = nullptr;
    uint8_t *videoData = nullptr;

    //输出视频参数信息
    if(getVideo)
    {
        int srcW = videoStream->codecpar->width;
        int srcH = videoStream->codecpar->height;
        AVPixelFormat format = videoDeCodecCtx->pix_fmt;
        m_videoDuration = inFmtCtx->duration / AV_TIME_BASE;
        emit sigDuration(m_videoDuration);

        int byte = av_image_get_buffer_size(AV_PIX_FMT_RGB32, srcW, srcH, 1);
        videoData = (uint8_t *)av_malloc(byte * sizeof(uint8_t));
        av_image_fill_arrays(swsFrame->data, swsFrame->linesize, videoData, (AVPixelFormat)AV_PIX_FMT_RGB32, srcW, srcH, 1);

        swsCtx = sws_getContext(srcW, srcH, (AVPixelFormat)format, srcW, srcH, (AVPixelFormat)AV_PIX_FMT_RGB32, SWS_FAST_BILINEAR, NULL, NULL, NULL);
    }

    //开始时刻
    m_startTime = av_gettime_relative() / 1000;
    int64_t ptsTime = 0;
    int curPlayPos = 0;
    AVPacket *packet = av_packet_alloc();
    AVFrame *videoFrame = av_frame_alloc();

    while(m_isRun)
    {
        //暂停
        if(m_pause)
        {
            QThread::msleep(200);
            continue;
        }

        //进度切换
        if(m_seek)
        {
            //跳转的播放时刻 单位微秒
            int64_t timeStamp = m_seekTime * 1000;
            if (inFmtCtx->start_time != AV_NOPTS_VALUE)
                timeStamp += inFmtCtx->start_time;

            //注:seek若关键帧间隔大需避免延时
            timeStamp = av_rescale_q(timeStamp, AVRational{1, AV_TIME_BASE}, videoStream->time_base);
            ret = av_seek_frame(inFmtCtx, videoIndex, timeStamp, AVSEEK_FLAG_BACKWARD);
            if(ret < 0)
            {
                qDebug() << "av_seek_frame fail" << m_seekTime;
            }
            else
            {
                //清空内部帧队列
                if(videoDeCodecCtx)
                    avcodec_flush_buffers(videoDeCodecCtx);

                //调整时钟
                int64_t offset = m_seekTime - ptsTime;
                m_startTime -= offset;
            }

            m_seek = false;
        }

        //不断读取packet
        ret = av_read_frame(inFmtCtx, packet);
        if (ret == AVERROR_EOF)
        {
            m_isRun = false;
            break;
        }

        if(packet->stream_index == videoIndex)
        {
            //编码数据进行解码
            ret = avcodec_send_packet(videoDeCodecCtx, packet);
            if (ret < 0)
            {
                av_packet_unref(packet);
                continue;
            }
            ret = avcodec_receive_frame(videoDeCodecCtx, videoFrame);
            if (ret < 0)
            {
                av_packet_unref(packet);
                continue;
            }

            //控制速度 ms单位
            ptsTime = videoFrame->pts * av_q2d(videoStream->time_base) * 1000;
            if(m_seekFilter)
            {
                //跳转播放时间不符合的帧直接丢弃 默认阈值200ms
                int offset = m_seekTime - ptsTime;
                if(0 > offset || offset < 200)
                {
                    m_seekFilter = false;
                }
                else
                {
                    av_frame_unref(videoFrame);
                    av_packet_unref(packet);
                    continue;
                }
            }

            qint64 elapsed = av_gettime_relative() / 1000 - m_startTime;
            int64_t sleepMs = ptsTime - elapsed;
            if(sleepMs > 3)
            {
                QThread::msleep(sleepMs);
            }

            //发送播放位置信息
            int sec = ptsTime / 1000;
            if(sec != curPlayPos)
            {
                curPlayPos = sec;
                emit sigPlayPosition(curPlayPos);
            }

            //将解码后的frame数据转换为Image
            sws_scale(swsCtx, (const uint8_t *const *)videoFrame->data, videoFrame->linesize, 0, videoFrame->height, swsFrame->data, swsFrame->linesize);
            QImage image((uchar *)videoData, videoFrame->width, videoFrame->height, QImage::Format_RGB32);
            QImage copy = image.copy();
            emit sigSendImage(copy);

            av_frame_unref(videoFrame);
        }

        av_packet_unref(packet);
    }

    //释放资源
    sws_freeContext(swsCtx);
    av_frame_free(&swsFrame);
    av_free(videoData);
    av_packet_free(&packet);
    av_frame_free(&videoFrame);
    avcodec_free_context(&videoDeCodecCtx);
    avformat_close_input(&inFmtCtx);

    emit sigPlayFinished(0);
    qDebug() << "VideoDecoder end" << m_filePath;
    return;
}


http://www.niftyadmin.cn/n/5293411.html

相关文章

PostgreSQL 数据库归档最近被问及的问题问题 与 4 毋 处世学

开头还是介绍一下群&#xff0c;如果感兴趣PolarDB ,MongoDB ,MySQL ,PostgreSQL ,Redis, Oceanbase, Sql Server等有问题&#xff0c;有需求都可以加群群内&#xff0c;可以解决你的问题。加群请联系 liuaustin3 &#xff0c;&#xff08;共1790人左右 1 2 3 4 5&#xff0…

Error loading MySQLdb module.Did you install mysqlclient?报错解决方法

出现报错django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? 这个错误意味着你的Django项目在尝试使用MySQL数据库时找不到MySQLdb模块。 首先检查自己有没有安装mysqlclient 运行以下命令来安装mysqlclient&…

C语言 volatile关键字

volatile关键字介绍 volatile 是一个关键字&#xff0c;用于修饰变量&#xff0c;表示该变量是易变的&#xff0c;即可能在任何时候被意外地改变。在多线程编程中&#xff0c;当多个线程同时访问同一个变量时&#xff0c;由于线程之间的交互和优化&#xff0c;可能会导致变量的…

GPT系列概述

OPENAI做的东西 Openai老窝在爱荷华州&#xff0c;微软投资的数据中心 万物皆可GPT下咱们要失业了&#xff1f; 但是世界不仅仅是GPT GPT其实也只是冰山一角&#xff0c;2022年每4天就有一个大型模型问世 GPT历史时刻 GPT-1 带回到2018年的NLP 所有下游任务都需要微调&#x…

6种大模型的使用方式总结,使用领域数据集持续做无监督预训练可能是一个好选择

本文原文来自DataLearnerAI官方网站&#xff1a;6种大模型的使用方式总结&#xff0c;使用领域数据集持续做无监督预训练可能是一个好选择 | 数据学习者官方网站(Datalearner)https://www.datalearner.com/blog/1051703426665726 Sebastian Raschka是LightningAI的首席科学家&…

第八章 创建Callout Library - ZFentry 链接选项

文章目录 第八章 创建Callout Library - ZFentry 链接选项ZFentry 链接选项链接简介 第八章 创建Callout Library - ZFentry 链接选项 ZFentry 链接选项 每个 ZFENTRY 语句都需要一个字符串来确定函数参数的传递方式。本节提供可用链接选项的详细说明。 链接简介 — 概述了各…

机器人中的数值优化之罚函数法

欢迎大家关注我的B站&#xff1a; 偷吃薯片的Zheng同学的个人空间-偷吃薯片的Zheng同学个人主页-哔哩哔哩视频 (bilibili.com) 本文ppt来自深蓝学院《机器人中的数值优化》 目录 1 L2-Penalty Method 1.1等式约束 1.2不等式约束 2 L1-Penalty Method 3 Barrier Method …

浅学Vue3

安装 vue项目 npm init vuelatest 回车装包 npm install 路由 安装 Router npm install vue-router4 -S项目根目录新建 router --> index.js vue2中 index.jsimport Vue from vue; import VueRouter from vue-router; import Home from ../views/Home.vue;Vue.use(V…