網(wǎng)站首頁 編程語言 正文
一、結(jié)果
可以設(shè)置延時(shí)函數(shù)-----遍歷每一幀的信息進(jìn)行打印
25(fps)*30(秒)=750幀
二、解碼準(zhǔn)備工作+循環(huán)解碼相關(guān)操作
videodecode.h .cpp
#ifndef VIDEODECODE_H
#define VIDEODECODE_H
#include <QObject>
//當(dāng)前C++兼容C語言
extern "C"
{
//avcodec:編解碼(最重要的庫)
#include <libavcodec/avcodec.h>
//avformat:封裝格式處理
#include <libavformat/avformat.h>
//swscale:視頻像素?cái)?shù)據(jù)格式轉(zhuǎn)換
#include <libswscale/swscale.h>
//avdevice:各種設(shè)備的輸入輸出
#include <libavdevice/avdevice.h>
//avutil:工具庫(大部分庫都需要這個(gè)庫的支持)
#include <libavutil/avutil.h>
}
class videoDecode : public QObject
{
Q_OBJECT
public:
explicit videoDecode(QObject *parent = 0);
//視頻文件上下文格式
AVFormatContext* avformat_context;
//編解碼器上下文格式
AVCodecContext* avcodec_context;
//解碼器上下文格式
AVCodec* avcodec;
signals:
public slots:
};
#endif // VIDEODECODE_H
#include "videodecode.h"
#include<QDebug>
#include<QCoreApplication>
#include<QThread>
//解碼初始化操作
//1.注冊(cè)所有組件
//2.打開視頻輸入文件
//3.查找視頻流信息
//4.查找解碼器
//5.打開解碼器
videoDecode::videoDecode(QObject *parent) : QObject(parent)
{
qDebug()<<"1.注冊(cè)所有組件";
av_register_all();
qDebug()<<"2.打開視頻輸入文件";
QString filename = QCoreApplication::applicationDirPath();
qDebug()<<"獲取程序運(yùn)行目錄 "<<filename;
QString cinputFilePath = "test.avi"; //本地視頻文件放入程序運(yùn)行目錄
avformat_context = avformat_alloc_context();
//參數(shù)一:封裝格式上下文->AVFormatContext->包含了視頻信息(視頻格式、大小等等...)
//參數(shù)二:打開文件(入口文件)->url
int avformat_open_result = avformat_open_input(&avformat_context,cinputFilePath.toStdString().c_str(),NULL,NULL);
if (avformat_open_result != 0)
{
//獲取異常信息
char* error_info = new char[32];
av_strerror(avformat_open_result, error_info, 1024);
qDebug()<<QString("異常信息 %1").arg(error_info);
};
qDebug()<<"3.查找視頻流信息";
//參數(shù)一:封裝格式上下文->AVFormatContext
//參數(shù)二:配置
//返回值:0>=返回OK,否則失敗
int avformat_find_stream_info_result = avformat_find_stream_info(avformat_context, NULL);
if (avformat_find_stream_info_result < 0){
//獲取失敗
char* error_info = new char[32];
av_strerror(avformat_find_stream_info_result, error_info, 1024);
qDebug()<<QString("異常信息 %1").arg(error_info);
}
qDebug()<<"4.查找解碼器";
//第一點(diǎn):獲取當(dāng)前解碼器是屬于什么類型解碼器->找到了視頻流
//音頻解碼器、視頻解碼器、字幕解碼器等等...
//獲取視頻解碼器流引用
int av_stream_index = -1;
for (int i = 0; i < avformat_context->nb_streams; ++i) {
//循環(huán)遍歷每一流
//視頻流、音頻流、字幕流等等...
if (avformat_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
//找到了
av_stream_index = i;
break;
}
}
if (av_stream_index == -1)
{
qDebug()<<QString("沒有找到視頻流");
}
//第二點(diǎn):根據(jù)視頻流->查找到視頻解碼器上下文->視頻壓縮數(shù)據(jù)
//編解碼器上下文
avcodec_context = avformat_context->streams[av_stream_index]->codec;
//第三點(diǎn):根據(jù)解碼器上下文->獲取解碼器ID
avcodec = avcodec_find_decoder(avcodec_context->codec_id);
if (avcodec == NULL)
{
qDebug()<<QString("沒有找到視頻解碼器");
}
qDebug()<<"5.打開解碼器";
int avcodec_open2_result = avcodec_open2(avcodec_context,avcodec,NULL);
if (avcodec_open2_result != 0)
{
char* error_info = new char[32];
av_strerror(avformat_find_stream_info_result, error_info, 1024);
qDebug()<<QString("異常信息 %1").arg(error_info);
}
qDebug()<<"視頻詳細(xì)信息輸出";
//此函數(shù)自動(dòng)打印輸入或輸出的詳細(xì)信息
av_dump_format(avformat_context, 0, cinputFilePath.toStdString().c_str(), 0);
qDebug()<<"----------------解碼準(zhǔn)備工作完成-----------------";
qDebug()<<"----------------開始循環(huán)解碼操作-----------------";
qDebug()<<"6.循環(huán)解碼";
//讀取幀數(shù)據(jù)換成到哪里->緩存到packet里面
AVPacket* av_packet = (AVPacket*)av_malloc(sizeof(AVPacket));
//解碼的狀態(tài)類型(0:表示解碼完畢,非0:表示正在解碼)
int current_frame_index = 0;
//>=0:說明有數(shù)據(jù),繼續(xù)讀取 <0:說明讀取完畢,結(jié)束
//從視頻文件上下文中讀取包--- 有數(shù)據(jù)就一直讀取
while (av_read_frame(avformat_context,av_packet) >= 0)
{
//解碼什么類型流(視頻流、音頻流、字幕流等等...)
if (av_packet->stream_index == av_stream_index)
{
//遍歷每一幀的信息進(jìn)行打印
current_frame_index++;
//延時(shí)操作 1秒顯示25幀--1000/25=40
QThread::msleep(40);
qDebug()<<QString("當(dāng)前遍歷第 %1 幀").arg(current_frame_index);
}
}
qDebug()<<"7.關(guān)閉所有解碼組件";
av_packet_free(&av_packet);
//關(guān)閉流
avcodec_close(avcodec_context);
avformat_free_context(avformat_context);
}
main.cpp
#include "widget.h"
#include <QApplication>
#include"indexwin.h"
#include<QGraphicsItem> //圖元
#include<QGraphicsScene> //場(chǎng)景
#include<QGraphicsView> //視圖
#include<QTransform> //變換
#include<QDebug>
#include"myview.h"
#include <QWidget>
#include"usersdata.h"
#include"registerwin.h"
#include"sqlite3.h"
#include"mysqlite.h"http://數(shù)據(jù)庫類
#include"videodecode.h"
//當(dāng)前C++兼容C語言
extern "C"
{
//avcodec:編解碼(最重要的庫)
#include <libavcodec/avcodec.h>
//avformat:封裝格式處理
#include <libavformat/avformat.h>
//swscale:視頻像素?cái)?shù)據(jù)格式轉(zhuǎn)換
#include <libswscale/swscale.h>
//avdevice:各種設(shè)備的輸入輸出
#include <libavdevice/avdevice.h>
//avutil:工具庫(大部分庫都需要這個(gè)庫的支持)
#include <libavutil/avutil.h>
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"sqlite3版本"<<sqlite3_libversion();
qDebug("------------------------------------------------------------------------");
qDebug("%s", avcodec_configuration());
qDebug("version: %d", avcodec_version());
qDebug("------------------------------------------------------------------------");
mySqlite::getInstance("app.db");//創(chuàng)建數(shù)據(jù)庫
mySqlite::createUserTable("user");//創(chuàng)建用戶表
videoDecode *p = new videoDecode;
//開機(jī)動(dòng)畫
myView kaiji;//對(duì)象創(chuàng)建
kaiji.show();//調(diào)用方法
return a.exec();
}
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/125050168
相關(guān)推薦
- 2022-06-28 ES6基礎(chǔ)語法之對(duì)象介紹_基礎(chǔ)知識(shí)
- 2022-08-05 python內(nèi)置模塊之上下文管理contextlib_python
- 2022-09-26 Go并發(fā)同步Mutex典型易錯(cuò)使用場(chǎng)景_Golang
- 2023-10-16 修改el-textarea的字體以及placeholder字體和顏色
- 2022-12-09 C#調(diào)用Windows的API實(shí)現(xiàn)窗體動(dòng)畫_C#教程
- 2022-05-12 Kotlin 代數(shù)/枚舉/密封類
- 2022-08-21 HTTP協(xié)議詳細(xì)介紹_其它綜合
- 2022-07-11 pandas實(shí)現(xiàn)按照多列排序-ascending_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支