CongUan
asked on
How to record avi with sound from BITMAP sources
Hi Experts,
I have developed the program that record a AVIs and the program that record sound to wav file,
Now I want to develop a other program to merge AVIs and wav file to avi with sound,
Have anybody know how to do this? Please help me!
The follow are some my code:
The separate module that record just avis (no sound) worked well
//Add Frame to avi file
bool CAVIUtil::AddFrame(HBITMAP hBitmap)
{
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biBitCou nt = 0;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(m_hAviDC, hBitmap, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS);
bmpInfo.bmiHeader.biCompre ssion = BI_RGB;
GetDIBits(m_hAviDC, hBitmap, 0, bmpInfo.bmiHeader.biHeight , m_lpBits, &bmpInfo, DIB_RGB_COLORS);
if (FAILED(AVIStreamWrite(m_p AviCompres sedStream, m_lSample++, 1, m_lpBits, bmpInfo.bmiHeader.biSizeIm age, 0, NULL, NULL)))
{
strcpy(m_szErrorMessage, "AppendFrameUsual(): AVIStreamWrite failed");
return (false);
}
return (true);
}
The record wav and record avi run with two other thread, when they finished I just add the follow code to add them togetther, but it can not play(seem to wrong format).
HRESULT CAVIUtil::AddAviWav(const char *src)
{
char *buf=0; WavChunk *wav = (WavChunk*)src;
HANDLE hf=CreateFile(src,GENERIC_ READ,FILE_ SHARE_READ ,NULL,OPEN _EXISTING, 0,NULL);
if (hf==INVALID_HANDLE_VALUE) {
MessageBox(NULL,"AVIERR_FI LEOPEN","E RROR",MB_O K);
return AVIERR_FILEOPEN;
}
DWORD size = GetFileSize(hf,NULL);
buf = new char[size];
DWORD red; ReadFile(hf,buf,size,&red, NULL);
CloseHandle(hf);
wav = (WavChunk*)buf;
// check that format doesn't clash
bool badformat=false;
if (m_wfx.nChannels==0)
{ m_wfx.wFormatTag=wav->fmt. wFormatTag ;
m_wfx.cbSize=0;
m_wfx.nAvgBytesPerSec=wav- >fmt.dwAvg BytesPerSe c;
m_wfx.nBlockAlign=wav->fmt .wBlockAli gn;
m_wfx.nChannels=wav->fmt.w Channels;
m_wfx.nSamplesPerSec=wav-> fmt.dwSamp lesPerSec;
m_wfx.wBitsPerSample=wav-> fmt.wBitsP erSample;
}
else
{
if (m_wfx.wFormatTag!=wav->fm t.wFormatT ag) badformat=true;
if (m_wfx.nAvgBytesPerSec!=wa v->fmt.dwA vgBytesPer Sec) badformat=true;
if (m_wfx.nBlockAlign!=wav->f mt.wBlockA lign) badformat=true;
if (m_wfx.nChannels!=wav->fmt .wChannels ) badformat=true;
if (m_wfx.nSamplesPerSec!=wav ->fmt.dwSa mplesPerSe c) badformat=true;
if (m_wfx.wBitsPerSample!=wav ->fmt.wBit sPerSample ) badformat=true;
}
if (badformat) {if (buf!=0) delete[] buf;
MessageBox(NULL,"AVIERR_BA DFORMAT"," ERROR",MB_ OK);
return AVIERR_BADFORMAT;
}
//
// create the stream if necessary
if(m_pAudioStream==0){
AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ah dr));
ahdr.fccType=streamtypeAUD IO;
ahdr.dwScale=m_wfx.nBlockA lign;
ahdr.dwRate=m_wfx.nSamples PerSec*m_w fx.nBlockA lign;
ahdr.dwSampleSize=m_wfx.nB lockAlign;
ahdr.dwQuality=(DWORD)-1;
HRESULT hr = AVIFileCreateStream(m_pAvi File,&m_pA udioStream , &ahdr);
if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf; return hr;}
hr = AVIStreamSetFormat(m_pAudi oStream,0, &m_wfx,siz eof(WAVEFO RMATEX));
if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf;return hr;}
}
// now we can write the data
unsigned long numbytes = wav->dat.size;
unsigned long numsamps = numbytes*8 / m_wfx.wBitsPerSample;
HRESULT hr = AVIStreamWrite(m_pAudioStr eam,m_lSam ple,numsam ps,wav->da t.data,num bytes,0,NU LL,NULL);
if (buf!=0) delete[] buf;
if (hr!=AVIERR_OK) {
MessageBox(NULL,"AVIERR_OK ","ERROR", MB_OK);
return hr;
}
m_lSample+=numsamps;
MessageBox(NULL,"AVIS_OK", "S_OK",MB_ OK);
return S_OK;
}
Please help me soon if you can
Thank in advance
I have developed the program that record a AVIs and the program that record sound to wav file,
Now I want to develop a other program to merge AVIs and wav file to avi with sound,
Have anybody know how to do this? Please help me!
The follow are some my code:
The separate module that record just avis (no sound) worked well
//Add Frame to avi file
bool CAVIUtil::AddFrame(HBITMAP
{
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biBitCou
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(m_hAviDC, hBitmap, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS);
bmpInfo.bmiHeader.biCompre
GetDIBits(m_hAviDC, hBitmap, 0, bmpInfo.bmiHeader.biHeight
if (FAILED(AVIStreamWrite(m_p
{
strcpy(m_szErrorMessage, "AppendFrameUsual(): AVIStreamWrite failed");
return (false);
}
return (true);
}
The record wav and record avi run with two other thread, when they finished I just add the follow code to add them togetther, but it can not play(seem to wrong format).
HRESULT CAVIUtil::AddAviWav(const char *src)
{
char *buf=0; WavChunk *wav = (WavChunk*)src;
HANDLE hf=CreateFile(src,GENERIC_
if (hf==INVALID_HANDLE_VALUE)
MessageBox(NULL,"AVIERR_FI
return AVIERR_FILEOPEN;
}
DWORD size = GetFileSize(hf,NULL);
buf = new char[size];
DWORD red; ReadFile(hf,buf,size,&red,
CloseHandle(hf);
wav = (WavChunk*)buf;
// check that format doesn't clash
bool badformat=false;
if (m_wfx.nChannels==0)
{ m_wfx.wFormatTag=wav->fmt.
m_wfx.cbSize=0;
m_wfx.nAvgBytesPerSec=wav-
m_wfx.nBlockAlign=wav->fmt
m_wfx.nChannels=wav->fmt.w
m_wfx.nSamplesPerSec=wav->
m_wfx.wBitsPerSample=wav->
}
else
{
if (m_wfx.wFormatTag!=wav->fm
if (m_wfx.nAvgBytesPerSec!=wa
if (m_wfx.nBlockAlign!=wav->f
if (m_wfx.nChannels!=wav->fmt
if (m_wfx.nSamplesPerSec!=wav
if (m_wfx.wBitsPerSample!=wav
}
if (badformat) {if (buf!=0) delete[] buf;
MessageBox(NULL,"AVIERR_BA
return AVIERR_BADFORMAT;
}
//
// create the stream if necessary
if(m_pAudioStream==0){
AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ah
ahdr.fccType=streamtypeAUD
ahdr.dwScale=m_wfx.nBlockA
ahdr.dwRate=m_wfx.nSamples
ahdr.dwSampleSize=m_wfx.nB
ahdr.dwQuality=(DWORD)-1;
HRESULT hr = AVIFileCreateStream(m_pAvi
if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf; return hr;}
hr = AVIStreamSetFormat(m_pAudi
if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf;return hr;}
}
// now we can write the data
unsigned long numbytes = wav->dat.size;
unsigned long numsamps = numbytes*8 / m_wfx.wBitsPerSample;
HRESULT hr = AVIStreamWrite(m_pAudioStr
if (buf!=0) delete[] buf;
if (hr!=AVIERR_OK) {
MessageBox(NULL,"AVIERR_OK
return hr;
}
m_lSample+=numsamps;
MessageBox(NULL,"AVIS_OK",
return S_OK;
}
Please help me soon if you can
Thank in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.