AVIStreamWrite code..

Program Visual C++ 2012. 10. 10. 13:32 Posted by HisPark

 

I tried to use this to add a .wav to an existing .avi (although I had a class CWaveSoundRead). If you check the return codes, you get to AVIStreamWrite() which returns 0x80044065, which turns out to be AVIERR_UNSUPPORTED.

 

In hindsight, I'd say you called AVIFileCreateStream() before you filled in the AVISTREAMINFO object. Actually, now that I see it, it's hard to imagine your code compiling as-is, since audioInfo is defined AFTER AVIFileCreateStream!

 

Here's something I did, although it still mistakes the audio stream length:

struct FmtChunk {

char id[4]; //="fmt "

unsigned long size; //=16 or 0x28

short wFormatTag; //=WAVE_FORMAT_PCM=1

unsigned short wChannels; //=1 or 2 for mono or stereo

unsigned long dwSamplesPerSec; //=11025 or 22050 or 44100

unsigned long dwAvgBytesPerSec; //=wBlockAlign * dwSamplesPerSec

unsigned short wBlockAlign; //=wChannels * (wBitsPerSample==8?1:2)

unsigned short wBitsPerSample; //=8 or 16, for bits per sample

};

 

struct DataChunk {

char id[4]; //="data"

unsigned long size; //=datsize, size of the following array

unsigned char data[1]; //=the raw data goes here

};

 

struct WavChunk {

char id[4]; //="RIFF"

unsigned long size; //=datsize+8+16+4

char type[4]; //="WAVE"

};

 

bool Q_AVI_AddWav(cstring fnameVideo,cstring fnameAudio)

// Adds a .wav file to an existing .avi (with video stream)

{

IAVIStream* m_pStreamAudio=0;

HRESULT hr;

 

AVIFileInit();

 

PAVIFILE avi;

hr=AVIFileOpen(&avi, fnameVideo,OF_WRITE,NULL);

CHECK(hr,"AVIFileOpen");

 

WavChunk wav;

FmtChunk fmt;

DataChunk dat;

 

//read wav file

FILE *fr;

int pos;

 

fr=qfopen(fnameAudio,"rb");

 

// Read header

fread(&wav,1,sizeof(wav),fr);

 

// Read 'fmt' chunk; may be 16 or 40 in length

pos=ftell(fr);

fread(&fmt,1,sizeof(fmt),fr);

if(fmt.size==40)fseek(fr,40-16,SEEK_CUR); // Skip rest of fmt

// else it's ok

 

// Read data specs

fread(&dat,sizeof(dat),1,fr);

char *buf = new char[dat.size];

qdbg("Wav data %d bytes\n",dat.size);

fread(buf,1,dat.size,fr);

qfclose(fr);

 

// set wave format info

WAVEFORMATEX wfx;

wfx.wFormatTag=fmt.wFormatTag;

wfx.cbSize=0;

wfx.nAvgBytesPerSec=fmt.dwAvgBytesPerSec;

wfx.nBlockAlign=fmt.wBlockAlign;

wfx.nChannels=fmt.wChannels;

wfx.nSamplesPerSec=fmt.dwSamplesPerSec;

wfx.wBitsPerSample=fmt.wBitsPerSample;

 

// create audio stream

AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ahdr));

ahdr.fccType=streamtypeAUDIO;

ahdr.dwScale=wfx.nBlockAlign;

ahdr.dwRate=wfx.nSamplesPerSec*wfx.nBlockAlign;

ahdr.dwSampleSize=wfx.nBlockAlign;

ahdr.dwQuality=(DWORD)-1;

hr=AVIFileCreateStream(avi, &m_pStreamAudio, &ahdr);

CHECK(hr,"AVIFileCreateStream");

if(hr!=AVIERR_OK) {if (buf) QDELETE_ARRAY(buf); /*delete[] buf;*/ return false;}

hr = AVIStreamSetFormat(m_pStreamAudio,0,&wfx,sizeof(WAVEFORMATEX));

CHECK(hr,"AVIStreamSetFormat");

if(hr!=AVIERR_OK) {if (buf) QDELETE_ARRAY(buf); /*delete[] buf;*/ return false;}

 

//write audio stream

unsigned long numbytes = dat.size;

unsigned long numsamps = fmt.wChannels*numbytes*8 / wfx.wBitsPerSample;

hr = AVIStreamWrite(m_pStreamAudio,0,numsamps,buf,numbytes,0,0,0);

CHECK(hr,"AVIStreamWrite");

qdbg("Write numsamps %d, numbytes %d\n",numsamps,numbytes);

QDELETE_ARRAY(buf); //if(buf)delete[] buf;

 

// Release audio stream

AVIStreamRelease(m_pStreamAudio);

 

// Close AVI

hr=AVIFileRelease(avi);

CHECK(hr,"AVIFileRelease");

 

// Close VFW

AVIFileExit();

 

return hr==AVIERR_OK;

}

'Program Visual C++' 카테고리의 다른 글

Windows8 개발환경..  (0) 2012.11.05
ACM 오디오 프로그래밍  (0) 2012.10.10
ActiveX 권한2  (0) 2012.04.18
ActiveX 권한..1  (0) 2012.01.12
레지스트리 등록 (RegCreatekeyEx, RegDeleteValue, RegOpenkeyEX, RegSetValueEx  (0) 2012.01.05