Atlanta Custom Software Development 

   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


In previous Article Working with AVI Files - Part 1 (Opening and Closing AVI file) We learned how to open and close AVI File. We also learned How to read AVI file information using AVIFileInfo api. In this article we will go one step further. Today we will work with AVI Streams.

Avi file can contain one or more than one streams. Stream is chunk of data. In our project we have defined 4 types of streams

Click here to copy the following block
'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&)

In out first article we learned how to get a valid handle for the AVI file (Check the previous article Working with AVI Files - Part 1 (Opening and Closing AVI file)) . Once you have vaid handle of AVI file you can pass it to AVIFileGetStream function along with the type of stream we are looking for. Here is the declaration of AVIFileGetStream function.

Click here to copy the following block
Declare Function AVIFileGetStream Lib "avifil32.dll" _
  (ByVal pfile As Long, _
  ByRef ppaviStream As Long, _
  ByVal fccType As Long, _
  ByVal lParam As Long) As Long

Parameters :

pfile
[in] Handle to an open AVI file.

ppavi
[in, out] Pointer to the new stream interface.

fccType
[in] Four-character code indicating the type of stream to open. Zero indicates any stream can be opened. The following definitions apply to the data commonly found in AVI streams.
  • streamtypeAUDIO : Indicates an audio stream.
  • streamtypeMIDI : Indicates a MIDI stream.
  • streamtypeTEXT : Indicates a text stream.
  • streamtypeVIDEO : Indicates a video stream.

lParam
[in] Count of the stream type. Identifies which occurrence of the specified stream type to access.

Return values :

Returns zero if successful or an error otherwise. Possible error values include the following.
  • AVIERR_NODATA : The file does not contain a stream corresponding to the values of fccType and lParam.
  • AVIERR_MEMORY : Not enough memory.


Our Sample call may look like as below

Click here to copy the following block
'Get the first available video stream (hStream)
res = AVIFileGetStream(hFile, hStream, streamtypeVIDEO, 0)
If res <> AVIERR_OK Then
  MsgBox "Error while retrieving VIDEO stream information... " & GetAPIErrorDesc(Err.LastDllError)
  GoTo errHandler
End If

Once we get handle to the required stream you can perform various operations on that stream. Here in our sample code we retrive Number of frames in the requested stream and startting position of that stream (Most of the time it will be 0 but stream may start anywhere in the avi file)

To Get Number of frames in the requested stream call AVIStreamLength with stream handle as an input parameter

To Get start position of stream in the avi file call AVIStreamStart with stream handle as an input parameter

To Get detail stream information you may call AVIStreamInfo function.

Click here to copy the following block
Public Declare Function AVIStreamInfo Lib "avifil32.dll" _
  (ByVal pAVIStream As Long, _
  ByRef psi As AVI_STREAM_INFO, _
  ByVal lSize As Long) As Long

Parameters :

pavi
[in] Handle to an open stream.

psi
[in, out] Pointer to a structure to contain the stream information.

lSize
[in] Size, in bytes, of the structure used for psi.

Return Values

Returns zero if successful or an error otherwise.

And here is the full implementation of our example.

Step-By-Step Example

- Create a standard exe project
- Place one command button
- Place 2 text box. Set MultiLine=True and Scrollbar=both for text2 and text3
- Run the project
- Specify valid AVI path and click on Command button

Place following code in Form1

Form1.frm

Click here to copy the following block
Private Sub Command1_Click()
  On Error GoTo errHandler

  Dim res As Long
  Dim hFile As Long  'pointer to AVI file interface (hFile handle)
  Dim hStream As Long  'pointer to AVI stream interface (hStream handle)

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

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


  'initialize the AVIFile library
  Call AVIFileInit
  'create a handle to the AVI file
  If AVIFileOpen(hFile, Text1, OF_SHARE_DENY_WRITE, ByVal 0&) = 0 Then

    'Get the first available video stream (hStream)
    res = AVIFileGetStream(hFile, hStream, streamtypeVIDEO, 0)
    If res <> AVIERR_OK Then
      MsgBox "Error while retrieving VIDEO stream information... " & GetAPIErrorDesc(Err.LastDllError)
      GoTo errHandler
    End If

    'get the starting position of the stream (some streams may not start simultaneously)
    firstFrame = AVIStreamStart(hStream)

    If firstFrame = -1 Then
      MsgBox "Error while retrieving the start position of the stream... " & GetAPIErrorDesc(Err.LastDllError)
      GoTo errHandler
    End If

    'get the length of video stream in frames
    numFrames = AVIStreamLength(hStream)
    If numFrames = -1 Then
      MsgBox "Error while retrieving the length of the stream in frames... " & GetAPIErrorDesc(Err.LastDllError)
      GoTo errHandler
    End If

    Text2 = Text2 & "Stream Handle     : " & hStream & vbCrLf
    Text2 = Text2 & "Video stream length  : " & numFrames & " (frames)" & vbCrLf
    Text2 = Text2 & "Stream start on frame : " & firstFrame & vbCrLf

    'retrieve the AVI Stream info
    If AVIStreamInfo(hStream, streamInfo, Len(streamInfo)) = 0 Then
      Call PrintAVIStreamInfo(streamInfo, Text3)
    Else
      MsgBox "Error while retrieving AVI stream information... " & GetAPIErrorDesc(Err.LastDllError)
    End If
  Else
    MsgBox "Error while opening the AVI file... " & GetAPIErrorDesc(Err.LastDllError)
  End If

errHandler:
  If hFile <> 0 Then
    'closes the stream
    If hStream <> 0 Then Call AVIStreamRelease(hStream)

    'closes the file
    Call AVIFileRelease(hFile)

    'exit the AVIFile library and decrement the reference count for the library
    Call AVIFileExit
  End If
End Sub

Private Sub Form_Load()
'//Check if application of root (i.e. C:\ or D:\ etc)
  If Len(App.Path) > 3 Then
    Text1 = App.Path & "\" & "test.avi"
  Else
    Text1 = App.Path & "test.avi"
  End If
End Sub

Place following code in the module

Module1.bas

Click here to copy the following block
'////////////////////////////////////////
'//To diaplay API Error
'////////////////////////////////////////
Global Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Global Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Global Const LANG_NEUTRAL = &H0
Global Const SUBLANG_DEFAULT = &H1

Public Declare Function GetLastError Lib "kernel32" () As Long
Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
'////////////////////////////////////////
'//API for AVI
'////////////////////////////////////////

Public Declare Function AVIFileInfo Lib "avifil32.dll" _
    (ByVal pfile As Long, _
    pfi As AVI_FILE_INFO, _
    ByVal lSize As Long) As Long  'HRESULT

Public Declare Sub AVIFileInit Lib "avifil32.dll" ()

Public Declare Function AVIFileOpen Lib "avifil32.dll" _
    (ByRef ppfile As Long, _
    ByVal szFile As String, _
    ByVal uMode As Long, _
    ByVal pclsidHandler As Long) As Long  'HRESULT

Public Declare Function AVIFileRelease Lib "avifil32.dll" _
    (ByVal pfile As Long) As Long

Public Declare Sub AVIFileExit Lib "avifil32.dll" ()

Public Declare Function AVIFileGetStream Lib "avifil32.dll" _
    (ByVal pfile As Long, _
    ByRef ppaviStream As Long, _
    ByVal fccType As Long, _
    ByVal lParam As Long) As Long

Public Declare Function AVIStreamInfo Lib "avifil32.dll" _
    (ByVal pAVIStream As Long, _
    ByRef psi As AVI_STREAM_INFO, _
    ByVal lSize As Long) As Long

Public Declare Function AVIStreamStart Lib "avifil32.dll" _
    (ByVal pavi As Long) As Long

Public Declare Function AVIStreamLength Lib "avifil32.dll" _
    (ByVal pavi As Long) As Long

Public Declare Function AVIStreamRelease Lib "avifil32.dll" _
    (ByVal pavi As Long) As Long  'ULONG

Public Type AVI_STREAM_INFO
  fccType As Long
  fccHandler As Long
  dwFlags As Long
  dwCaps As Long
  wPriority As Integer
  wLanguage As Integer
  dwScale As Long
  dwRate As Long
  dwStart As Long
  dwLength As Long
  dwInitialFrames As Long
  dwSuggestedBufferSize As Long
  dwQuality As Long
  dwSampleSize As Long
  rcFrame As AVI_RECT
  dwEditCount As Long
  dwFormatChangeCount As Long
  szName As String * 64
End Type
'// file open mode
Global Const OF_CREATE = &H1000
Global Const OF_READ = &H0
Global Const OF_READWRITE = &H2
Global Const OF_SHARE_DENY_WRITE = &H20
Global Const OF_WRITE = &H1

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


Public Sub PrintAVIStreamInfo(asi As AVI_STREAM_INFO, txt As TextBox)
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_STREAM_INFO (START) *****"
  txt = txt & vbCrLf & "**********************************"
  With asi
    txt = txt & vbCrLf & "fccType = " & .fccType
    txt = txt & vbCrLf & "fccHandler = " & .fccHandler
    txt = txt & vbCrLf & "dwFlags = " & .dwFlags
    txt = txt & vbCrLf & "dwCaps = " & .dwCaps
    txt = txt & vbCrLf & "wPriority = " & .wPriority
    txt = txt & vbCrLf & "wLanguage = " & .wLanguage
    txt = txt & vbCrLf & "dwScale = " & .dwScale
    txt = txt & vbCrLf & "dwRate = " & .dwRate
    txt = txt & vbCrLf & "dwStart = " & .dwStart
    txt = txt & vbCrLf & "dwLength = " & .dwLength
    txt = txt & vbCrLf & "dwInitialFrames = " & .dwInitialFrames
    txt = txt & vbCrLf & "dwSuggestedBufferSize = " & .dwSuggestedBufferSize
    txt = txt & vbCrLf & "dwQuality = " & .dwQuality
    txt = txt & vbCrLf & "dwSampleSize = " & .dwSampleSize
    txt = txt & vbCrLf & "rcFrame.left = " & .rcFrame.left
    txt = txt & vbCrLf & "rcFrame.top = " & .rcFrame.top
    txt = txt & vbCrLf & "rcFrame.right = " & .rcFrame.right
    txt = txt & vbCrLf & "rcFrame.bottom = " & .rcFrame.bottom
    txt = txt & vbCrLf & "dwEditCount = " & .dwEditCount
    txt = txt & vbCrLf & "dwFormatChangeCount = " & .dwFormatChangeCount
    txt = txt & vbCrLf & "szName = " & .szName
  End With
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_STREAM_INFO (END) *******"
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & ""

  If TypeOf txt Is TextBox Then
    '//No need do do anything TextBox is updated
  Else
    '//No TextBox is supplied so just dump in Immidiate window
    Debug.Print txt
  End If

End Sub

Public Function GetAPIErrorDesc(ErrorCode As Long) As String
  Dim Buffer As String
  Buffer = Space(200)
  FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorCode, LANG_NEUTRAL, Buffer, 200, ByVal 0&
  GetAPIErrorDesc = Buffer
End Function

Other articles in this series

Working with AVI Files - Part 1 (Opening and Closing AVI file)
Working with AVI Files - Part 2 (Working with AVI streams)
Working with AVI Files - Part 3 (Working With Frames, AVI to BMP)

********* UNDER CONSTRUCTION **********


Audit, Notify, Deploy and Manage SSIS
Download the Free 30-day Trial Version...Learn More About This Product...

Submitted By : Jojo Desuja  (Member Since : 8/10/2004 10:56:17 PM)

Job Description : The King of Night....... Love to do programming specially night time....
View all (22) submissions by this author  (Birth Date : )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  Blogs

© 2008 BinaryWorld LLC. All rights reserved.