ffmpeg系列:使用C++类封装ffmpeg,封装视频帧解码功能Decode()方法
2017-11-23 00:08
537 查看
新增AVFrame *Decode(const AVPacket *pkt)方法
MyFFmpeg.h文件:
#pragma once
extern "C"{
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <string>
#include <QMutex>
class MyFFmpeg
{
public:
/*设置成为单件模式*/
static MyFFmpeg *Get()
{
static MyFFmpeg ff;
return &ff;
}
/**
*打开指定路径的视频文件,如果已有打开的视频文件则先关闭
*/
bool Open(const char *path);
//关闭之前打开的视频文件
void Close();
//读取视频帧
AVPacket Read();
//解码功能
AVFrame *Decode(const AVPacket *pkt);
/*获取相关错误信息*/
std::string GetError();
/*类析构函数*/
virtual ~MyFFmpeg();
/*视频文件总的毫秒数*/
int totalMs = 0;
protected:
int videoStream = 0;
/*相关错误信息*/
char errorbuf[1024];
//应对多线程访问时的同步锁
QMutex mutex;
AVFormatContext *ac = NULL;
AVFrame *yuv = NULL;
/*设置成为单件模式,所以要把构造函数设置为私有*/
MyFFmpeg();
};
MyFFmpeg.cpp文件:
#include "MyFFmpeg.h"
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
bool MyFFmpeg::Open(const char *path){
Close();
mutex.lock();
int re = avformat_open_input(&ac, path, 0, 0);
if (re != 0){//打开文件失败
mutex.unlock();
av_strerror(re, errorbuf, sizeof(errorbuf));
return false;
}
//得到视频总时长的毫秒数
totalMs = ((ac->duration / AV_TIME_BASE)*1000);
for (int i = 0; i < ac->nb_streams; i++)
{
AVCodecContext *enc = ac->streams[i]->codec;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO){
videoStream = i;
AVCodec *codec = avcodec_find_decoder(enc->codec_id);
if (!codec){
mutex.unlock();
printf("无法解码此视频文件\n");
return false;
}
int err = avcodec_open2(enc, codec, NULL);
if (err != 0){
mutex.unlock();
char buf[1024] = { 0 };
av_strerror(err, buf, sizeof(buf));
printf(buf);
return false;
}
printf("\n");
printf("成功打开视频编码流\n");
}
}
mutex.unlock();
return true;
}
void MyFFmpeg::Close(){
mutex.lock();
if (ac) avformat_close_input(&ac);
if (yuv) av_frame_free(&yuv);
mutex.unlock();
}
std::string MyFFmpeg::GetError(){
mutex.lock();
std::string re = this->errorbuf;
mutex.unlock();
return re;
}
AVPacket MyFFmpeg::Read(){
AVPacket pkt;
memset(&pkt, 0, sizeof(AVPacket));
mutex.lock();
if (!ac){
mutex.unlock();
return pkt;
}
int err = av_read_frame(ac, &pkt);
if (err != 0){//读取失败
av_strerror(err, errorbuf, sizeof(errorbuf));
}
mutex.unlock();
return pkt;
}
AVFrame * MyFFmpeg::Decode(const AVPacket *pkt){
mutex.lock();
if (!ac){
mutex.unlock();
return NULL;
}
if (yuv == NULL){
yuv = av_frame_alloc();
}
int re = avcodec_send_packet(ac->streams[pkt->stream_index]->codec, pkt);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
re = avcodec_receive_frame(ac->streams[pkt->stream_index]->codec, yuv);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
mutex.unlock();
return yuv;
}
MyFFmpeg::MyFFmpeg()
{
errorbuf[0] = '\0';
av_register_all();
}
MyFFmpeg::~MyFFmpeg()
{
}
main.cpp调用:
#include "myplayer.h"
#include <QtWidgets/QApplication>
#include "MyFFmpeg.h"
int main(int argc, char *argv[])
{
char path[1024] = "test.mp4";
if (MyFFmpeg::Get()->Open(path)){
printf("文件[%s]打开成功",path);
}
else
{
printf("\n文件[%s]打开失败;错误信息:%s", path,MyFFmpeg::Get()->GetError().c_str());
getchar();
return -1;
}
for (;;){
AVPacket pkt = MyFFmpeg::Get()->Read();
if (pkt.size == 0){
break;
}
printf("pts = %lld\n",pkt.pts);
AVFrame *yuv = MyFFmpeg::Get()->Decode(&pkt);
if (yuv){
printf("解码成功 --- ");
}
//释放空间
av_packet_unref(&pkt);
}
QApplication a(argc, argv);
MyPlayer w;
w.show();
return a.exec();
}
运行结果:
MyFFmpeg.h文件:
#pragma once
extern "C"{
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <string>
#include <QMutex>
class MyFFmpeg
{
public:
/*设置成为单件模式*/
static MyFFmpeg *Get()
{
static MyFFmpeg ff;
return &ff;
}
/**
*打开指定路径的视频文件,如果已有打开的视频文件则先关闭
*/
bool Open(const char *path);
//关闭之前打开的视频文件
void Close();
//读取视频帧
AVPacket Read();
//解码功能
AVFrame *Decode(const AVPacket *pkt);
/*获取相关错误信息*/
std::string GetError();
/*类析构函数*/
virtual ~MyFFmpeg();
/*视频文件总的毫秒数*/
int totalMs = 0;
protected:
int videoStream = 0;
/*相关错误信息*/
char errorbuf[1024];
//应对多线程访问时的同步锁
QMutex mutex;
AVFormatContext *ac = NULL;
AVFrame *yuv = NULL;
/*设置成为单件模式,所以要把构造函数设置为私有*/
MyFFmpeg();
};
MyFFmpeg.cpp文件:
#include "MyFFmpeg.h"
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"swscale.lib")
bool MyFFmpeg::Open(const char *path){
Close();
mutex.lock();
int re = avformat_open_input(&ac, path, 0, 0);
if (re != 0){//打开文件失败
mutex.unlock();
av_strerror(re, errorbuf, sizeof(errorbuf));
return false;
}
//得到视频总时长的毫秒数
totalMs = ((ac->duration / AV_TIME_BASE)*1000);
for (int i = 0; i < ac->nb_streams; i++)
{
AVCodecContext *enc = ac->streams[i]->codec;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO){
videoStream = i;
AVCodec *codec = avcodec_find_decoder(enc->codec_id);
if (!codec){
mutex.unlock();
printf("无法解码此视频文件\n");
return false;
}
int err = avcodec_open2(enc, codec, NULL);
if (err != 0){
mutex.unlock();
char buf[1024] = { 0 };
av_strerror(err, buf, sizeof(buf));
printf(buf);
return false;
}
printf("\n");
printf("成功打开视频编码流\n");
}
}
mutex.unlock();
return true;
}
void MyFFmpeg::Close(){
mutex.lock();
if (ac) avformat_close_input(&ac);
if (yuv) av_frame_free(&yuv);
mutex.unlock();
}
std::string MyFFmpeg::GetError(){
mutex.lock();
std::string re = this->errorbuf;
mutex.unlock();
return re;
}
AVPacket MyFFmpeg::Read(){
AVPacket pkt;
memset(&pkt, 0, sizeof(AVPacket));
mutex.lock();
if (!ac){
mutex.unlock();
return pkt;
}
int err = av_read_frame(ac, &pkt);
if (err != 0){//读取失败
av_strerror(err, errorbuf, sizeof(errorbuf));
}
mutex.unlock();
return pkt;
}
AVFrame * MyFFmpeg::Decode(const AVPacket *pkt){
mutex.lock();
if (!ac){
mutex.unlock();
return NULL;
}
if (yuv == NULL){
yuv = av_frame_alloc();
}
int re = avcodec_send_packet(ac->streams[pkt->stream_index]->codec, pkt);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
re = avcodec_receive_frame(ac->streams[pkt->stream_index]->codec, yuv);
if (re != 0){//失败
mutex.unlock();
return NULL;
}
mutex.unlock();
return yuv;
}
MyFFmpeg::MyFFmpeg()
{
errorbuf[0] = '\0';
av_register_all();
}
MyFFmpeg::~MyFFmpeg()
{
}
main.cpp调用:
#include "myplayer.h"
#include <QtWidgets/QApplication>
#include "MyFFmpeg.h"
int main(int argc, char *argv[])
{
char path[1024] = "test.mp4";
if (MyFFmpeg::Get()->Open(path)){
printf("文件[%s]打开成功",path);
}
else
{
printf("\n文件[%s]打开失败;错误信息:%s", path,MyFFmpeg::Get()->GetError().c_str());
getchar();
return -1;
}
for (;;){
AVPacket pkt = MyFFmpeg::Get()->Read();
if (pkt.size == 0){
break;
}
printf("pts = %lld\n",pkt.pts);
AVFrame *yuv = MyFFmpeg::Get()->Decode(&pkt);
if (yuv){
printf("解码成功 --- ");
}
//释放空间
av_packet_unref(&pkt);
}
QApplication a(argc, argv);
MyPlayer w;
w.show();
return a.exec();
}
运行结果:
相关文章推荐
- ffmpeg系列:使用C++类封装ffmpeg,封装视频帧缩放转换rgb功能ToRGB()方法
- ffmpeg系列:使用C++类封装ffmpeg,封装读取视频帧功能Read()方法
- ffmpeg系列:使用C++类封装ffmpeg,实现打开视频文件功能
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- Android开发系列(二十):AutoCompleteTextView(自己主动完毕文本框)的功能和使用方法
- Android开发系列(二十一):Spinner的功能和使用方法以及实现列表选择框
- Android开发系列(二十四):Notification的功能与使用方法
- ffmpeg学习4--ffmpeg类的简单封装,包含解码和定时录像功能
- EXTJS学习系列基础篇:第四篇(转载)作者殷良胜,Ext中有两个很重要的方法,一个是decode;一个是encode.顾名思义,一个是编码,一个是解码,你难道真的这么想吗?严格的说,一个是将json字符串转换成对象;一个是将对象转换成json字符串
- Android开发系列(二十四):Notification的功能与使用方法
- java.net.URLEncoder和java.net.URLDecoder的使用和js 中编码(encode)和解码(decode)方法
- 多媒体编解码学习: ffmpeg(更新ffmpeg库使用方法)
- 视频编解码之windows平台下编译ffmpeg的方法和使用教程
- %25%37%DD 。。。上述形式不是乱码。 这是urlencoding。 你可以使用js内置的方法 encodeURIComponent进行编码, 而使用decodeURIComponent把上述形式再解码为普通字符
- Android开发系列(二十二):AdapterViewFlipper的功能和使用方法
- 【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)
- FCKeditor的常用配置方法和无法使用上传功能的解决!
- 在.NET中发送邮件,使用回执功能中遇到问题的解决方法[在NOTES服务器上,C#,.NET2.0环境]
- Windows 2003不能使用include file功能的解决方法
- document.designMode的功能与使用方法介绍