Gently down the Streams
Video Capture Help ] AVIFile Tutorial ] Licensing and Distribution ] Resources ] Awards ] Latest News ] Source Code ] Free Controls ]

 

STEP 2 - Working with streams in existing AVI files

HERE ARE THE PROJECT FILES FOR STEP 2 AVITutr2.zip


AVI files contain "streams" which contain "chunks" of data.  There are four types of streams defined in mAVIDecs.bas:

'Stream types for use in VB (translated from C macros)
Global Const streamtypeVIDEO As Long = 1935960438 '= mmioStringToFOURCC("vids", 0&)
Global Const streamtypeAUDIO As Long = 1935963489 '= mmioStringToFOURCC("auds", 0&)
Global Const streamtypeMIDI  As Long = 1935960429 '= mmioStringToFOURCC("mids", 0&)
Global Const streamtypeTEXT  As Long = 1937012852 '= mmioStringToFOURCC("txts", 0&)

Theoretically, AVI files can contain multiple streams of the same type.  However most players assume only one video and one audio stream per file.  The first step in using streams in the AVIFile API is to get a PAVISTREAM handle to the existing stream.  Since we already have a PAVIFILE handle from the previous step, all we need to do is pass that to the AVIFileGetStream function along with the type of stream we are looking for.  First, dimension a new Long variable to hold the stream interface pointer:

Dim pAVIStream As Long 'pointer to AVI stream interface (PAVISTREAM handle)

Then add this code right after the call to AVIFileOpen in the sample program you created in the previous step:

'Get the first available video stream (PAVISTREAM)
res = AVIFileGetStream(pAVIFile, pAVIStream, streamtypeVIDEO, 0)
If res <> AVIERR_OK Then GoTo ErrorOut

When the call to AVIFileGetStream returns, the pAVIStream variable will contain a PAVISTREAM handle that we can pass to other functions.  There is one other important thing to remember.  When you are done using the handle you need to free it by calling AVIStreamRelease.  Add this call right after the ErrorOut tag so that it will be called even if there is an error:

If pAVIStream <> 0 Then
    Call AVIStreamRelease(pAVIStream)
'//closes video stream
End If

Once we have the handle we can get the first frame and the length of the stream by adding a couple of lines of code.  First, dim two variables to hold the start frame and length of the file:

Dim numFrames As Long 'number of frames in video stream
Dim firstFrame As Long 'position of the first video frame

And then add these lines right after the call to AVIFileGetStream:

'get the starting position of the stream (some streams may not start simultaneously)
firstFrame = AVIStreamStart(pAVIStream)
If firstFrame = -1 Then GoTo ErrorOut
'this function returns -1 on error

'get the length of video stream in frames
numFrames = AVIStreamLength(pAVIStream)
If numFrames = -1 Then GoTo ErrorOut
' this function returns -1 on error

In order to make the sample program do something interesting let's show the results in a message box again.  We will also print out the members of the AVI_FILE_INFO and AVI_STREAM_INFO UDTs in the debug window so let's inform the user of that fact in the same message.  Add this line to show the message:

MsgBox "PAVISTREAM handle is " & pAVIStream & vbCrLf & _
"Video stream length - " & numFrames & vbCrLf & _
"Stream starts on frame #" & firstFrame & vbCrLf & _
"File and Stream info will be written to Immediate Window (from IDE - Ctrl+G to view)", vbInformation, App.title

You can call AVIFileInfo and AVIStreamInfo to get information about the AVI file and the video stream you have open.  While you are working in the IDE you can also use the Immediate window to print out this information.  This is extremely useful, since you can immediately see whether you have opened a valid file and whether the API is returning valid information.  I included a couple of utility functions in the mAVIDecs.bas file as an example of how to do this.  You can call these Subs by first dimensioning a couple of UDT variables to hold the information:

Dim fileInfo As AVI_FILE_INFO 'file info struct
Dim streamInfo As AVI_STREAM_INFO
'stream info struct

And then put these lines of code after the MsgBox call:

'get file info struct (UDT)
res = AVIFileInfo(pAVIFile, fileInfo, Len(fileInfo))
If res <> AVIERR_OK Then GoTo ErrorOut

'print file info to Debug Window
Call DebugPrintAVIFileInfo(fileInfo)

'get stream info struct (UDT)
res = AVIStreamInfo(pAVIStream, streamInfo, Len(streamInfo))
If res <> AVIERR_OK Then GoTo ErrorOut

'print stream info to Debug Window
Call DebugPrintAVIStreamInfo(streamInfo)

If you copied all this code properly, you will now have a useful sample program that can open any AVI file and obtain an interface pointer to its video stream.  It should also show the length of the file in frames in a pop-up msgbox window and record more detailed file and stream information in the immediate window of the IDE.  You can download the full project for this step from here (AVITutr2.zip)

Go to the next step

Go back to the Table of Contents

This Shrinkwrap Visual Basic AVIFile Tutorial is Copyright (C) 2000 by Ray Mercer
Redistribution of the tutorial text and/or samples is prohibited.  Please contact the author Ray Mercer <raymer@shrinkwrapvb.com> if you have a question about this policy.