ffmpeg学习4--ffmpeg类的简单封装,包含解码和定时录像功能
参考网上的资料,简单封装了一下ffmpeg,这里记录一下,其它传感器编码及项目中用到的已经抽离,这里只包含解码和录像部分。这只是个玩具级别的测试。完整测试代码下载:代码下载
ffmpegDeCode.h
#pragma once
#include "stdafx.h"
#include<iostream>
using namespace std;
extern char *VideoPath;
extern int CameraIndex;
extern int fps;
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include <libavdevice/avdevice.h>
};
class ffmpegDeCode
{
public:
ffmpegDeCode();
~ffmpegDeCode();
bool DecoderInit(const char *sdpurl);
void DecodeFrame();
void DecodeLastFrame();
void StopDecodeFrame();
void GetopencvFrame();
//////////解码部分////////
AVFrame *pFrame;//解码后的帧数据
AVFormatContext *pFormatCtx ;//视频流编码上下文的指针
int videoindex;//视频流索引
AVCodecContext *pCodecCtx;//用于编解码器的类型(视频,音频...)及设置
AVCodec *pCodec ;//解码器指针
long m_framenums;//帧序号
SwsContext *img_convert_ctx;//根据编码信息设置渲染格式
AVPacket *packet;////是存储压缩编码数据相关信息的结构体,数据流分包(一帧的分包)
//1个AVPacket的data通常对应一个NAL。
IplImage * pCVFrame ;
char datatime[60];
char Recordname[100];
//////录像部分/////////
AVStream *i_video_stream;
AVFormatContext *opFormatCtx;
AVStream *o_video_stream;
void WriteVideoHead();
void WriteVideoTail();
int m_recordframenums;
int m_Toatlframenums;
int last_pts ;
int last_dts;
int64_t pts, dts;
int count;//录像文件计数
};
ffmpegDeCode.cpp
#include "stdafx.h"
#include"stdio.h"
#include"ffmpegDeCode.h"
#include "sqlite/sqlite3.h"
#include <fstream>
#include <windows.h>
SYSTEMTIME sys; //系统时间
char systime[50];
ofstream outfile;//打印异常信息
ffmpegDeCode :: ffmpegDeCode()
{
pFrame = NULL/**pFrameRGB = NULL*/;
pFormatCtx = NULL;
videoindex=0;
pCodecCtx = NULL;
pCodec = NULL;
m_framenums = 0;
img_convert_ctx = NULL;
packet = NULL;
pCVFrame = NULL;
//////////////
i_video_stream= NULL;
opFormatCtx= NULL;
o_video_stream= NULL;
m_recordframenums=1;
last_pts = 0;
last_dts = 0;
pts=0;
dts=0;
count=1;
m_Toatlframenums=fps*60*60;//fps*3600
memset(datatime, 0, 100*sizeof(char));
memset(Recordname, 0, 100*sizeof(char));
}
ffmpegDeCode :: ~ffmpegDeCode()
{
WriteVideoTail();
i