Article View: borland.public.cppbuilder.multimedia
Article #531Re: How to add wav audio to an avi?
From: Netdevel
Date: Sat, 27 Dec 2003 22:05
Date: Sat, 27 Dec 2003 22:05
129 lines
5227 bytes
5227 bytes
Hi, theres an example in the WinSDK hlp that comes with BCB.
NETDEVEL
"teroni" <teroni@iname.com> wrote in message
news:3fea1ab0$1@newsgroups.borland.com...
>
> I have written a program that compiles a list of bitmaps into an AVI. I
have tried to add a wav file to it using code from Lucian Wischik:
> http://www.wischik.com/lu/programmer/avi_utils.html
> Somehow when I join the audio with the avi, the playing
> time of the file ends up being twice the length that it should be. The
audio is 1 minute 10 seconds normally (the avi without audio is about the
same as long). But when joined I get 2 minutes 20 seconds for the avi
duration. Windows media player still plays the video but it thinks it is
twice as long as it should be.
> If any one has any source code that joins a WAV file to an AVI, I would
appreciate a sample. Lets say I have an already created AVI and just want to
add audio to it. Thanks.
>
> Here is the source that I am using:
>
> HRESULT AddAviWav(HAVI avi, const char *src, DWORD flags)
> {
> if (avi == NULL)
> return AVIERR_BADHANDLE;
> if (flags != SND_MEMORY && flags != SND_FILENAME)
> return AVIERR_BADFLAGS;
> if (src==0)
> return AVIERR_BADPARAM;
> TAviUtil *au = (TAviUtil*)avi;
> if (au->iserr)
> return AVIERR_ERROR;
>
> char *buf = 0;
> WavChunk *wav = (WavChunk*)src;
> if (flags == SND_FILENAME)
> {
> HANDLE hf = CreateFile(src, GENERIC_READ, FILE_SHARE_READ,
> NULL, OPEN_EXISTING, 0, NULL);
> if (hf == INVALID_HANDLE_VALUE)
> {
> au->iserr=true;
> 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 (au->wfx.nChannels == 0)
> {
> au->wfx.wFormatTag = wav->fmt.wFormatTag;
> au->wfx.cbSize = 0;
> au->wfx.nAvgBytesPerSec = wav->fmt.dwAvgBytesPerSec;
> au->wfx.nBlockAlign = wav->fmt.wBlockAlign;
> au->wfx.nChannels = wav->fmt.wChannels;
> au->wfx.nSamplesPerSec = wav->fmt.dwSamplesPerSec;
> au->wfx.wBitsPerSample = wav->fmt.wBitsPerSample;
> }
> else
> {
> if (au->wfx.wFormatTag != wav->fmt.wFormatTag)
> badformat=true;
> if (au->wfx.nAvgBytesPerSec != wav->fmt.dwAvgBytesPerSec)
> badformat=true;
> if (au->wfx.nBlockAlign != wav->fmt.wBlockAlign)
> badformat=true;
> if (au->wfx.nChannels != wav->fmt.wChannels)
> badformat=true;
> if (au->wfx.nSamplesPerSec != wav->fmt.dwSamplesPerSec)
> badformat=true;
> if (au->wfx.wBitsPerSample != wav->fmt.wBitsPerSample)
> badformat=true;
> }
> if (badformat)
> {
> if (buf!=0)
> delete[] buf;
> return AVIERR_BADFORMAT;
> }
> //
> if (au->as == 0) // create the stream if necessary
> {
> AVISTREAMINFO ahdr;
> ZeroMemory(&ahdr,sizeof(ahdr));
> ahdr.fccType = streamtypeAUDIO;
> ahdr.dwScale = au->wfx.nBlockAlign;
> ahdr.dwRate = au->wfx.nSamplesPerSec * au->wfx.nBlockAlign;
> ahdr.dwSampleSize = au->wfx.nBlockAlign;
> ahdr.dwQuality = (DWORD) - 1;
> HRESULT hr = AVIFileCreateStream(au->pfile, &au->as, &ahdr);
> if (hr!=AVIERR_OK)
> {
> if (buf!=0)
> delete[] buf;
> au->iserr = true;
> return hr;
> }
> hr = AVIStreamSetFormat(au->as, 0, &au->wfx, sizeof(WAVEFORMATEX));
> if (hr!=AVIERR_OK)
> {
> if (buf!=0)
> delete[] buf;
> au->iserr=true;
> return hr;
> }
> }
> //
> // now we can write the data
> unsigned long numbytes = wav->dat.size;
> unsigned long numsamps = numbytes * 8 / au->wfx.wBitsPerSample;
> HRESULT hr = AVIStreamWrite(au->as, au->nsamp, numsamps,
> wav->dat.data, numbytes, 0, NULL, NULL);
> if (buf!=0)
> delete[] buf;
> if (hr!=AVIERR_OK)
> {
> au->iserr=true;
> return hr;
> }
> au->nsamp += numsamps;
> return S_OK;
> }
Technical Details
Message-ID:
<3fee022d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-05!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3fea1ab0$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d0fc,42d840425e61d461 X-Google-Attributes: gid10d0fc,public X-Google-ArrivalTime: 2003-12-27 14:09:19 PST From: "Netdevel" <netdevel@netdevel.net.nz> Newsgroups: borland.public.cppbuilder.multimedia References: <3fea1ab0$1@newsgroups.borland.com> Subject: Re: How to add wav audio to an avi? Date: Sun, 28 Dec 2003 11:05:29 +1300 Lines: 131 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 202.74.197.77 Message-ID: <3fee022d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1072562733 202.74.197.77 (27 Dec 2003 14:05:33 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-05!sn-xit-08!supernews.com!newsfeed.news2me.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail