FFMpeg编码MPEG-1,2视频

//STMPEG.h  
 #include<stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma once  
 #ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include <libswscale/swscale.h>


#ifdef __cplusplus
}
#endif


 enum MPEG_TYPE{  
   VCD_PAL, 
   VCD_NTSC, 
   SVCD_PAL, 
   SVCD_NTSC, 
   DVD_PAL, 
   DVD_NTSC, 
   CUSTOM_MPEG1, 
   CUSTOM_MPEG2 
  };  


 
class CSTMPEG  
{  
public:  
 CSTMPEG();  
 ~CSTMPEG(void);  
 
 //打开一个文件写mpeg  
 bool OpenMPEG(const char* strfile); 
 
 //写一帧图象数据到mpeg文件
 int  AddFrame(int width, intheight, int bpp, uint8_t* pRGBBuffer); 
 //释放资源   
  void CloseMPEG(); 
  //设置MPEG的类型
  void SetMPEGFormat(MPEG_TYPEtype);  
   //设置画面宽度 
   void SetMPEGWidth(int width); 
   //设置画面高度 
   void SetMPEGHeight(intheight);  
   //设置帧率(fps) 
   void SetFrameRate(float rate); 
   //设置视频码率(kbits/sec) rate: 30004000 6000 8000  
   void SetMPEGVideoBitRate(floatrate);  
private:  
void InitMPEGData(MPEG_TYPE type);  
//把图象颠倒过来  
void RGBBuffer2RGBFrame(AVFrame *pRGBFrame, int width, int height,int bpp, uint8_t* pRGBBuffer);  
AVFrame *alloc_picture(int pix_fmt, int width, int height); 
 
bool open_video(AVFormatContext *pFormatContext, AVStream*pVideoStream);  
AVStream *add_video_stream(AVFormatContext *pFormatContext, intcodec_id);
bool write_video_frame(AVFormatContext *pFormatcontext, AVStream*pAudioStream);  
void close_video(AVFormatContext *pFormatcontext, AVStream*pVideoStream);  
 
private:  
MPEG_TYPE m_MPEGType;  
int m_nWidth, m_nHeight;  
float m_fFrameRate;  
float m_fBitRate;  
int   m_nFrame;  
int srcWidth;
int srcHeight;


AVFrame   *m_pRGBFrame;  //YUV帧数据   
AVFrame   *m_pYUVFrame;  //RGB帧数据   
uint8_t*  m_pOutBuf;  //将一帧数据编码到这个缓冲区,用于写入到文件  
const int  m_nOutBufSize;     //编码缓冲区的大小 


//使用流来读写文件  
AVOutputFormat *m_pOutputFormat;  
AVFormatContext *m_pFormatContext;  
AVStream  *m_pVideoStream; 
AVStream  *m_pAudioStream; 
}; 

实现文件:

//STMPEG.cpp   


#include "stmpeg.h"  
#define LineWidthBytes(biWidth, biBitCount) ((biWidth * biBitCount+ 31) / 32 * 4)  
 
CSTMPEG::CSTMPEG(): m_nOutBufSize(200000)  
{  
m_MPEGType  = VCD_PAL;  
m_nWidth  = 352;  
m_nHeight  = 288;  
m_fFrameRate = 25;  
m_fBitRate  = 800000;  
m_nFrame  = -1;  
 
 
m_pRGBFrame     = NULL;  
m_pYUVFrame     = NULL; 
 
m_pFormatContext= NULL;  
m_pVideoStream = NULL;  
m_pAudioStream = NULL;  
}  
 
CSTMPEG::~CSTMPEG(void)  
{  
}  
 
void CSTMPEG::InitMPEGData(MPEG_TYPE type)  
{  
switch(type) {  
case VCD_PAL:  
{  
 m_nWidth = 352;  
 m_nHeight = 288;  
 m_fFrameRate = 25;  
}  
break;  
case VCD_NTSC:  
{  
 m_nWidth = 352;  
 m_nHeight = 240;  
 m_fFrameRate = 29.97;  
}  
break;  
case SVCD_PAL:  
{  
 m_nWidth = 480;  
 m_n

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值