Concatenate two AVI files using IltmmMultiStreamSource and IltmmMultiStreamTarget Example for Visual Basic
' *******************************************
' * This sample will join two files in one file *
' * The two files must be the same dimensions *
' '*******************************************
Private Sub Form_Load()
Dim StartHi As Long ' start time high
Dim StartLo As Long ' start time low
Dim SampleTarget As ltmmMultiStreamTarget ' sample target for ltmmConvertCtrl1
Dim SampleSource As ltmmMultiStreamSource ' sample target for ltmmConvertCtrl1
Dim mt As ltmmMediaType
Dim InsertedMediaType As ltmmMediaType
' set the start time to be 0
StartHi = 0
StartLo = 0
' INITIALIZE ltmmConvertCtrl1
' set the input filename
ltmmConvertCtrl1.SourceFile = "c:\Src1.avi"
' set the output sample to a target object
Set SampleTarget = New ltmmMultiStreamTarget
SampleTarget.StreamCount = 2
Set mt = New ltmmMediaType
mt.Type = ltmmMEDIATYPE_Video
SampleTarget.SetAcceptedMediaType 0, mt
mt.ResetMediaType
mt.Type = ltmmMEDIATYPE_Audio
SampleTarget.SetAcceptedMediaType 1, mt
‘ get the inserted media type for the first stream
Set InsertedMediaType = SampleTarget.GetAcceptedMediaType (0)
Set InsertedMediaType = Nothing
ltmmConvertCtrl1.TargetObject = SampleTarget
' START the source conversion, so we can get the media sample format
ltmmConvertCtrl1.StartConvert
' INITIALIZE ltmmConvertCtrl2
' create a source object
Set SampleSource = New ltmmMultiStreamSource
' get the output media sample format and put it into the source object
SampleSource.StreamCount = 2
SampleSource.SetMediaType 0, SampleTarget.GetConnectedMediaType(0)
SampleSource.SetMediaType 1, SampleTarget.GetConnectedMediaType(1)
‘ get the inserted media type for the first stream
Set InsertedMediaType = SampleSource.GetMediaType(0)
Set InsertedMediaType = Nothing
' set the output filename
ltmmConvertCtrl2.TargetFile = "C:\Dest.avi"
ltmmConvertCtrl2.SourceObject = SampleSource
' start the dest conversion
ltmmConvertCtrl2.StartConvert
Form1.Caption = "Converting file1..."
' convert first file
ConvertFile SampleTarget, SampleSource, StartHi, StartLo
' Restrict the output format to the media type of the source for the first file
' That is because the two files must have the same media type for both video and audio
' With video, you have to make sure the files have the same frame rate! Minor changes in
' the frame rate might make the connection fail!
' The control will tolerate differences in frame rate if you comment the next line
SampleTarget.SetAcceptedMediaType 0, SampleTarget.GetConnectedMediaType(0)
SampleTarget.SetAcceptedMediaType 1, SampleTarget.GetConnectedMediaType(1)
' change the source file to second file
ltmmConvertCtrl1.SourceFile = "C:\Src2.avi"
' start converting again
ltmmConvertCtrl1.StartConvert
Form1.Caption = "Converting file2..."
' convert second file
ConvertFile SampleTarget, SampleSource, StartHi, StartLo
' deliver end of sample to stop the conversion
SampleSource.DeliverEndOfStream 0, 1000
SampleSource.DeliverEndOfStream 1, 1000
Do While (ltmmConvertCtrl2.State = ltmmConvert_State_Running)
DoEvents
Loop
Form1.Caption = "Done"
' free the source and target objects
ltmmConvertCtrl1.ResetTarget
ltmmConvertCtrl2.ResetSource
End Sub
Private Sub ConvertFile(SampleTarget As ltmmMultiStreamTarget, SampleSource As ltmmMultiStreamSource, ByRef StartHi As Long, ByRef StartLo As Long)
Dim msSrc As ltmmMediaSample
Dim msDst As ltmmMediaSample
Dim LastStartHi As Long
Dim LastStartLo As Long
Dim LastStopHi As Long
Dim LastStopLo As Long
Dim SampleStream As Long
Dim nFrames As Long
LastStopHi = 0
LastStopLo = 0
nFrames = 0
Do
nFrames = nFrames + 1
Debug.Print "nFrames=", nFrames
' get the sample, allowing 10 s for the operation to complete
On Error GoTo EndConvert
SampleStream = SampleTarget.WaitForSample (1000)
Set msSrc = SampleTarget.GetSample(SampleStream, 0)
Debug.Print "Stream=", SampleStream
Set msDst = SampleSource.GetSampleBuffer (SampleStream, 1000)
' copy the sample time
On Error GoTo ResetTime
' set the sample time
msSrc.GetTime LastStartHi, LastStartLo, LastStopHi, LastStopLo
' get the sample time
msDst.SetTime StartHi + LastStartHi, StartLo + LastStartLo, StartHi + LastStopHi, StartLo + LastStopLo
GoTo NoResetTime
ResetTime:
msDst.ResetTime
NoResetTime:
' copy the data
On Error GoTo EndConvert
msDst.SetData msSrc.ActualDataLength, msSrc.GetData (msSrc.ActualDataLength)
' copy the other flags
msDst.Discontinuity = msSrc.Discontinuity
msDst.Preroll = msSrc.Preroll
msDst.SyncPoint = msSrc.SyncPoint
' release the source sample
Set msSrc = Nothing
' deliver the destination sample
SampleSource.DeliverSample SampleStream, 1000, msDst
' release the destination sample
Set msDst = Nothing
Loop While (1)
ltmmConvertCtrl1.StopConvert
StartHi = LastStopHi
StartLo = LastStopLo
Exit Sub
EndConvert:
ltmmConvertCtrl1.StopConvert
StartHi = LastStopHi
StartLo = LastStopLo
Exit Sub
End Sub