www.pudn.com > VideoAVIEdit.rar > AviEditor.cpp
// AviEditor.cpp: implementation of the CAviEditor class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "newvesion.h"
#include "AviEditor.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAviEditor::CAviEditor()
{
AVIFileInit();
}
CAviEditor::~CAviEditor()
{
AVIFileExit();
}
int CAviEditor::InitAviFileToWrite(CString filename)
{
HRESULT hr;
BITMAPINFOHEADER *bi;
hr=AVIFileOpen(&pf, filename, OF_CREATE|OF_WRITE , NULL);
if (hr != AVIERR_OK)
{
AfxMessageBox("AVIFileOpen");
return -1;
}
ZeroMemory(&m_VideoInfo,sizeof(m_VideoInfo));
ZeroMemory(&m_AudioInfo,sizeof(m_AudioInfo));
///////设置图像位图格式
memset(&m_BitMapInfoHeader, 0, sizeof(BITMAPINFOHEADER));
m_BitMapInfoHeader.biBitCount=24;
m_BitMapInfoHeader.biHeight=144;
m_BitMapInfoHeader.biWidth=176;
m_BitMapInfoHeader.biSize=sizeof(BITMAPINFOHEADER);
m_BitMapInfoHeader.biPlanes=1;
m_BitMapInfoHeader.biSizeImage=144*176*3;
m_BitMapInfoHeader.biCompression=BI_RGB;
///////设置声音格式
m_WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
m_WaveFormat.cbSize = 0;
m_WaveFormat.nAvgBytesPerSec = 16000;
m_WaveFormat.nBlockAlign = 2;
m_WaveFormat.nChannels = 1;
m_WaveFormat.nSamplesPerSec = 8000;
m_WaveFormat.wBitsPerSample = 16;
//m_VideoInfo.dwLength=100;
m_VideoInfo.fccType=streamtypeVIDEO;
m_VideoInfo.fccHandler=0;
m_VideoInfo.dwFlags=0;
m_VideoInfo.dwCaps=0;
m_VideoInfo.wPriority=0;
m_VideoInfo.wLanguage=0;
m_VideoInfo.dwScale=1;
m_VideoInfo.dwRate=10;
m_VideoInfo.dwStart=0;
m_VideoInfo.dwLength=100;
m_VideoInfo.dwInitialFrames=0;
m_VideoInfo.dwSuggestedBufferSize=144*176*3;
m_VideoInfo.dwQuality=10000;
m_VideoInfo.dwSampleSize=24;
m_VideoInfo.dwEditCount=0;
m_VideoInfo.dwFormatChangeCount=0;
SetRect(&m_VideoInfo.rcFrame, 0, 0,176, 144);
m_AudioInfo.fccType=streamtypeAUDIO;
m_AudioInfo.fccHandler=0;
m_AudioInfo.dwScale=1;
m_AudioInfo.dwSampleSize=16;
m_AudioInfo.dwSuggestedBufferSize=16000;
m_AudioInfo.dwRate=50;
m_AudioInfo.dwFlags=AVISTREAMINFO_DISABLED;
m_AudioInfo.dwScale= m_WaveFormat.nBlockAlign;
m_AudioInfo.dwRate = m_WaveFormat.nSamplesPerSec*m_WaveFormat.nBlockAlign;
m_AudioInfo.dwSuggestedBufferSize = 0;
m_AudioInfo.dwSampleSize = m_WaveFormat.nBlockAlign;
m_AudioInfo.dwQuality = (DWORD)-1;
///////
/*bi= (BITMAPINFOHEADER*)aviFrameHeader;
bi->biBitCount=m_BitMapInfoHeader.biBitCount;
bi->biClrImportant=0;
bi->biClrUsed=0;
bi->biCompression=BI_RGB;
bi->biHeight=m_BitMapInfoHeader.biHeight;
bi->biPlanes=1;
bi->biSize=sizeof(BITMAPINFOHEADER);
bi->biSizeImage=m_BitMapInfoHeader.biSizeImage;
bi->biWidth=m_BitMapInfoHeader.biWidth;
bi->biXPelsPerMeter=0;
bi->biYPelsPerMeter=0;
*/
AVICOMPRESSOPTIONS optsVideo;
memset(&optsVideo, 0, sizeof(AVICOMPRESSOPTIONS));
/*optsVideo.fccType=streamtypeVIDEO;
optsVideo.fccHandler=mmioFOURCC('X','V','I','D');
optsVideo.dwBytesPerSecond=20000;
optsVideo.dwKeyFrameEvery=40;
optsVideo.dwQuality=20;
optsVideo.dwFlags=AVICOMPRESSF_DATARATE;*/
AVICOMPRESSOPTIONS optsAudio;
/*
optsAudio.fccType=streamtypeAUDIO;
optsAudio.fccHandler=mmioFOURCC('M','P','G','3');
optsAudio.dwBytesPerSecond=8000;
optsAudio.cbFormat=sizeof(WAVEFORMATEX);
optsAudio.lpFormat=&m_WaveFormat;*/
//创建视频压缩数据流
hr=AVIFileCreateStream(pf, &m_pStreamInVideo, &m_VideoInfo); //
if (hr != AVIERR_OK)
{
AfxMessageBox("AVIFileCreateStream");
return -1;
}
//创建音频压缩数据流
/*hr=AVIFileCreateStream(pf, &m_pStreamInAudio, &m_AudioInfo); //
if (hr != AVIERR_OK)
{
AfxMessageBox("AVIFileCreateStream");
return -1;
}*/
///////////////////////////////////////////////////////////
LPAVICOMPRESSOPTIONS aopts[2]={&optsVideo,&optsAudio};
/**************************/
/*if(!AVISaveOptions(NULL, ICMF_CHOOSE_KEYFRAME| ICMF_CHOOSE_DATARATE|ICMF_CHOOSE_PREVIEW, 1, &m_pStreamInVideo,(LPAVICOMPRESSOPTIONS *)aopts))
{
AfxMessageBox("AVISaveOptions");
return -1;
}*/
/*hr=AVIMakeCompressedStream(&m_psCompressed, m_pStreamInVideo, aopts[0], NULL);
if (hr != AVIERR_OK)
{
AfxMessageBox("AVIMakeCompressedStream");
return -1;
}
//AVIStreamInfo(m_psCompressed, &m_VideoInfo, sizeof(AVISTREAMINFO));
hr = AVIStreamSetFormat(m_psCompressed, 0, aviFrameHeader, sizeof(BITMAPINFOHEADER));
if (hr != AVIERR_OK) {
return -1;
}*/
AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *)&aopts);
return 1;
}
BYTE a[1600];
int CAviEditor::WriteAviStream(unsigned char *data,int size,int flag)
{
HRESULT hr;
static int i=0;
static int i1=0;
memset(a,0,1600);
if(flag==1)//视频流
{
hr = AVIStreamSetFormat(m_pStreamInVideo, i, &m_BitMapInfoHeader, sizeof(BITMAPINFOHEADER));
if (hr != AVIERR_OK) {
return -1;
}
/**/
hr=AVIStreamWrite(m_pStreamInVideo, (i++), 1, data,
176*144*3, AVIIF_KEYFRAME, NULL, NULL);
if (hr != AVIERR_OK) {
return -1;
}
}
else //音频流
{
hr = AVIStreamSetFormat(m_pStreamInAudio, i1*100, &m_WaveFormat, sizeof(WAVEFORMATEX));
if (hr != AVIERR_OK) {
return -1;
}
hr=AVIStreamWrite(m_pStreamInAudio, (i1++)*100,800,a,1600,AVIIF_KEYFRAME, NULL, NULL);
if (hr != AVIERR_OK)
{
AfxMessageBox("AVIStreamWrite");
return -1;
}
}
return 1;
}
int CAviEditor::CloseFile()
{
while(AVIStreamClose(m_pStreamInVideo)!=0)
{};
//while(AVIStreamRelease(m_psCompressed)!=0)
//{};
//while (AVIStreamClose(m_pStreamInAudio)!=0)
//{};
while(AVIFileClose(pf)!=0)
{};
return 1;
}