🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.multimedia
2 messages
2 total messages Started by teroni Wed, 24 Dec 2003 22:01
How to add wav audio to an avi?
#530
Author: teroni
Date: Wed, 24 Dec 2003 22:01
116 lines
4690 bytes
 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;
}
Re: How to add wav audio to an avi?
#531
Author: Netdevel
Date: Sat, 27 Dec 2003 22:05
129 lines
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;
> }
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads