Recompressing AVI Data From a Global Memory Source Example for Visual Basic
The following code demonstrates the process of recompressing from a global memory source, preloaded with AVI media data, to a target file:
' source file name
Const SourceFile = "c:\source.avi"
' declarations for global memory source
Const GMEM_MOVEABLE = &H2
Const FILE_SHARE_READ = &H1
Const OPEN_EXISTING = 3
Const INVALID_HANDLE_VALUE = -1
Const GENERIC_READ = &H80000000
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hfile As Long, ByVal lpFileSizeHigh As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hfile As Long, ByVal lpBuffer As Long, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Sub cmdAbort_Click()
' if running then abort, else exit program
If ltmmConvertCtrl1.State = ltmmConvert_State_Running Then
ltmmConvertCtrl1.StopConvert
Else
' enable to dump current state
' MsgBox "State = " & CStr(ltmmConvertCtrl1.State) & ", Error = " & CStr(ltmmConvertCtrl1.ConvertError) & ", Progress = " & CStr(ltmmConvertCtrl1.PercentComplete) & "%"
Unload Me
End If
End Sub
Private Sub convert_Complete ()
' indicate that the conversion has completed
txtStatus = "Conversion completed."
cmdAbort.Caption = "Exit"
End Sub
Private Sub convert_ErrorAbort (ByVal ErrorCode As Long)
' indicate that a conversion error has occurred
txtStatus = "Error " & CStr(ErrorCode) & ". Conversion aborted."
cmdAbort.Caption = "Exit"
End Sub
Private Sub convert_Progress (ByVal Percent As Long)
' update the percentage text
txtProgress = CStr(Percent) & "%"
End Sub
Private Sub convert_Started ()
' indicate that the conversion has started
Dim Source As String
Dim Target As String
If ltmmConvertCtrl1.SourceType = ltmmConvert_Source_File Then
Source = ltmmConvertCtrl1.SourceFile
ElseIf ltmmConvertCtrl1.SourceType = ltmmConvert_Source_Array Then
Source = "[array]"
ElseIf ltmmConvertCtrl1.SourceType = ltmmConvert_Source_HGlobal Then
Source = "[hglobal]"
End If
If ltmmConvertCtrl1.TargetType = ltmmConvert_Target_File Then
Target = ltmmConvertCtrl1.TargetFile
ElseIf ltmmConvertCtrl1.TargetType = ltmmConvert_Target_Array Then
Target = "[array]"
End If
txtStatus = "Recompressing " & Source & " to " & Target
End Sub
Private Sub convert_UserAbort ()
' indicate that the user aborted
txtStatus = "Conversion Aborted."
cmdAbort.Caption = "Exit"
End Sub
Private Sub Form_Load()
' preload global memory with file and assign it to control
Dim hglobal As Long
Dim hfile As Long
Dim size As Long
Dim cb As Long
Dim buffer As Long
' open the source file
hfile = CreateFile(SourceFile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)
If hfile = INVALID_HANDLE_VALUE Then
Unload Me
Exit Sub
End If
' allocate same-sized global memory
size = GetFileSize(hfile, 0)
hglobal = GlobalAlloc(GMEM_MOVEABLE, size)
If hglobal = 0 Then
CloseHandle hfile
Unload Me
Exit Sub
End If
' read entire source into memory
buffer = GlobalLock(hglobal)
If (ReadFile(hfile, buffer, size, cb, 0) = 0 Or cb <> size) Then
MsgBox CStr(GetLastError())
GlobalUnlock hglobal
CloseHandle hfile
GlobalFree hglobal
Unload Me
Exit Sub
End If
GlobalUnlock hglobal
' close file
CloseHandle hfile
' assign it to the control
ltmmConvertCtrl1.SourceHGlobal = hglobal
' set output file
ltmmConvertCtrl1.TargetFile = "c:\target.avi"
' set the video compressor to the LEAD compressor
ltmmConvertCtrl1.VideoCompressors.Selection = ltmmConvertCtrl1.VideoCompressors.Find("@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\LEAD
MCMP/MJPEG Codec A COmpressor Also known as an encoder, this is a module or algorithm to compress data. Playing that data back requires a decompressor, or decoder. combined with a DECompressor, or encoder Also known as compressor, this is a module or algorithm to compress data. Playing that data back requires a decompressor, or decoder. and a decoder Also known as a decompressor, this is a module or algorithm to decompress data., which allows you to both compress and decompress that same data. (2.0)")
' set the audio compressor to the MP3 compressor
ltmmConvertCtrl1.AudioCompressors.Selection = ltmmConvertCtrl1.AudioCompressors.Find("@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\85MPEG Layer-3")
' set the output format to AVI
ltmmConvertCtrl1.TargetFormat = ltmmConvert_TargetFormat_Avi
' invoke the video compressor property dialog, if available
If ltmmConvertCtrl1.HasDialog (ltmmConvert_Dlg_VideoCompressor) Then
ltmmConvertCtrl1.ShowDialog ltmmConvert_Dlg_VideoCompressor, hWnd
End If
' invoke the audio compressor property dialog, if available
If ltmmConvertCtrl1.HasDialog (ltmmConvert_Dlg_AudioCompressor) Then
ltmmConvertCtrl1.ShowDialog ltmmConvert_Dlg_AudioCompressor, hWnd
End If
cmdAbort.Caption = "Abort"
On Error GoTo StartConvertError
' start the conversion
ltmmConvertCtrl1.StartConvert
Exit Sub
StartConvertError:
txtStatus = Err.Description & "... Error " & CStr(Err.Number)
cmdAbort.Caption = "Exit"
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim hglobal As Long
ltmmConvertCtrl1.StopConvert
If ltmmConvertCtrl1.SourceType = ltmmConvert_Source_HGlobal Then
hglobal = ltmmConvertCtrl1.SourceHGlobal
ltmmConvertCtrl1.ResetSource
GlobalFree hglobal
End If
End Sub