🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.multimedia
Article #530

How to add wav audio to an avi?

#530
From: 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;
}
Technical Details
Message-ID: <3fea1ab0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!priapus.visi.com!orange.octanews.net!news.octanews.net!news-out.visi.com!petbe.visi.com!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d0fc,42d840425e61d461,start
X-Google-Attributes: gid10d0fc,public
X-Google-ArrivalTime: 2003-12-24 15:09:23 PST
From: "teroni" <teroni@iname.com>
Sender: "teroni" <teroni@iname.com>
Reply-To: "teroni" <teroni@iname.com>
Subject: How to add wav audio to an avi?
Newsgroups: borland.public.cppbuilder.multimedia
X-User-Info: 67.3.136.47     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <3fea1ab0$1@newsgroups.borland.com>
Date: 24 Dec 2003 15:01:04 -0700
X-Trace: newsgroups.borland.com 1072306864 207.105.83.66 (24 Dec 2003 15:01:04 -0700)
Lines: 116
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!priapus.visi.com!orange.octanews.net!news.octanews.net!news-out.visi.com!petbe.visi.com!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail