網站首頁 編程語言 正文
一、結果
可以設置延時函數-----遍歷每一幀的信息進行打印
25(fps)*30(秒)=750幀
二、解碼準備工作+循環解碼相關操作
videodecode.h .cpp
#ifndef VIDEODECODE_H
#define VIDEODECODE_H
#include <QObject>
//當前C++兼容C語言
extern "C"
{
//avcodec:編解碼(最重要的庫)
#include <libavcodec/avcodec.h>
//avformat:封裝格式處理
#include <libavformat/avformat.h>
//swscale:視頻像素數據格式轉換
#include <libswscale/swscale.h>
//avdevice:各種設備的輸入輸出
#include <libavdevice/avdevice.h>
//avutil:工具庫(大部分庫都需要這個庫的支持)
#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.注冊所有組件
//2.打開視頻輸入文件
//3.查找視頻流信息
//4.查找解碼器
//5.打開解碼器
videoDecode::videoDecode(QObject *parent) : QObject(parent)
{
qDebug()<<"1.注冊所有組件";
av_register_all();
qDebug()<<"2.打開視頻輸入文件";
QString filename = QCoreApplication::applicationDirPath();
qDebug()<<"獲取程序運行目錄 "<<filename;
QString cinputFilePath = "test.avi"; //本地視頻文件放入程序運行目錄
avformat_context = avformat_alloc_context();
//參數一:封裝格式上下文->AVFormatContext->包含了視頻信息(視頻格式、大小等等...)
//參數二:打開文件(入口文件)->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.查找視頻流信息";
//參數一:封裝格式上下文->AVFormatContext
//參數二:配置
//返回值: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.查找解碼器";
//第一點:獲取當前解碼器是屬于什么類型解碼器->找到了視頻流
//音頻解碼器、視頻解碼器、字幕解碼器等等...
//獲取視頻解碼器流引用
int av_stream_index = -1;
for (int i = 0; i < avformat_context->nb_streams; ++i) {
//循環遍歷每一流
//視頻流、音頻流、字幕流等等...
if (avformat_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
//找到了
av_stream_index = i;
break;
}
}
if (av_stream_index == -1)
{
qDebug()<<QString("沒有找到視頻流");
}
//第二點:根據視頻流->查找到視頻解碼器上下文->視頻壓縮數據
//編解碼器上下文
avcodec_context = avformat_context->streams[av_stream_index]->codec;
//第三點:根據解碼器上下文->獲取解碼器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()<<"視頻詳細信息輸出";
//此函數自動打印輸入或輸出的詳細信息
av_dump_format(avformat_context, 0, cinputFilePath.toStdString().c_str(), 0);
qDebug()<<"----------------解碼準備工作完成-----------------";
qDebug()<<"----------------開始循環解碼操作-----------------";
qDebug()<<"6.循環解碼";
//讀取幀數據換成到哪里->緩存到packet里面
AVPacket* av_packet = (AVPacket*)av_malloc(sizeof(AVPacket));
//解碼的狀態類型(0:表示解碼完畢,非0:表示正在解碼)
int current_frame_index = 0;
//>=0:說明有數據,繼續讀取 <0:說明讀取完畢,結束
//從視頻文件上下文中讀取包--- 有數據就一直讀取
while (av_read_frame(avformat_context,av_packet) >= 0)
{
//解碼什么類型流(視頻流、音頻流、字幕流等等...)
if (av_packet->stream_index == av_stream_index)
{
//遍歷每一幀的信息進行打印
current_frame_index++;
//延時操作 1秒顯示25幀--1000/25=40
QThread::msleep(40);
qDebug()<<QString("當前遍歷第 %1 幀").arg(current_frame_index);
}
}
qDebug()<<"7.關閉所有解碼組件";
av_packet_free(&av_packet);
//關閉流
avcodec_close(avcodec_context);
avformat_free_context(avformat_context);
}
main.cpp
#include "widget.h"
#include <QApplication>
#include"indexwin.h"
#include<QGraphicsItem> //圖元
#include<QGraphicsScene> //場景
#include<QGraphicsView> //視圖
#include<QTransform> //變換
#include<QDebug>
#include"myview.h"
#include <QWidget>
#include"usersdata.h"
#include"registerwin.h"
#include"sqlite3.h"
#include"mysqlite.h"http://數據庫類
#include"videodecode.h"
//當前C++兼容C語言
extern "C"
{
//avcodec:編解碼(最重要的庫)
#include <libavcodec/avcodec.h>
//avformat:封裝格式處理
#include <libavformat/avformat.h>
//swscale:視頻像素數據格式轉換
#include <libswscale/swscale.h>
//avdevice:各種設備的輸入輸出
#include <libavdevice/avdevice.h>
//avutil:工具庫(大部分庫都需要這個庫的支持)
#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");//創建數據庫
mySqlite::createUserTable("user");//創建用戶表
videoDecode *p = new videoDecode;
//開機動畫
myView kaiji;//對象創建
kaiji.show();//調用方法
return a.exec();
}
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/125050168
相關推薦
- 2022-05-21 一次線上mongo慢查詢問題排查處理記錄_MongoDB
- 2022-12-27 python中內置庫csv的使用及說明_python
- 2023-02-15 Android跳轉系統設置Settings的各個界面詳解_Android
- 2022-12-11 Go?map發生內存泄漏解決方法_Golang
- 2022-07-07 Python使用captcha庫制作帶參數輸入驗證碼案例_python
- 2021-12-31 element 級聯下拉菜單 獲取value 同時 獲取label
- 2022-11-14 React前端路由應用介紹_React
- 2022-12-23 Kubernetes存儲系統數據持久化管理詳解_云其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支