Perform the following steps to create and run a multimedia convert application to concatenate two video files using two LEADTOOLS Multimedia ConvertCtrl controls.
Start Visual Studio.
Choose File->New->Project... from the menu.
In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application " in the Templates List.
Type the project name as "Multimedia Concatenate Videos" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
In the "Solution Explorer" window, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and select Leadtools.Multimedia and click OK. Also, right-click the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the "COM" tab and select:
Note: The above COM objects must be registered; in case they were not; using regsvr32. For more information, refer to DirectShow Registry-free Activation.
Next click OK.
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and drag two ConvertCtrl controls on the form. NOTE: If you do not have ConvertCtrl control in your toolbox, select Tools->Choose Toolbox Items from the menu. Click Browse and then select Leadtools.Multimedia.dll from "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet4\Win32" and then click Open and then click OK. After adding these controls to the form, set the following properties:
| Property | Value |
|---|---|
| Name | _convertctrl1 |
| Name | _convertctrl2 |
Go to the toolbox (View->Toolbox) and drag a ProgressBar control on the form (below the convert control) and set the following properties:
| Property | Value |
|---|---|
| Name | _progress |
| Anchor | Bottom, Left, Right |
| Step | 1 |
Go to the toolbox (View->Toolbox) and drag two Button controls to the bottom of the form and set the following properties:
| Property | Value |
|---|---|
| Name | _buttonProcessor |
| Text | Processor... |
| Anchor | Bottom, Right |
| Name | _buttonConvert |
| Text | Convert |
| Anchor | Bottom, Right |
Switch Form1 to code view (right-click Form1 in the Solution Explorer then choose View Code) and add the following lines to the beginning of the file:
Imports Leadtools.Multimedia using Leadtools.Multimedia; Declare the following private variable:
Private _sourceFile1 As StringPrivate _sourceFile2 As StringPrivate _targetFile As String
private string _sourceFile1;private string _sourceFile2;private string _targetFile;
Add an event handler to the _convertctrl2 Progress event and code it as follows:
Private Sub _convertctrl2_Progress(ByVal sender As System.Object, ByVal e As ProgressEventArgs) Handles _convertctrl2.Progress_progress.Value = CInt((_progress.Maximum * e.percent / 100))End Sub
private void _convertctrl2_Progress(object sender, ProgressEventArgs e){_progress.Value = (int)(_progress.Maximum * e.percent / 100);}
Add an event handler to the _convertctrl2 Complete event and code it as follows:
Private Sub _convertctrl2_Complete(ByVal sender As System.Object, ByVal e As EventArgs) Handles _convertctrl2.CompleteMessageBox.Show("Concatenation Complete")_buttonConvert.Enabled = TrueEnd Sub
private void _convertctrl2_Complete(object sender, EventArgs e){MessageBox.Show("Concatenation Complete");_buttonConvert.Enabled = true;}
Add an event handler to the _buttonConvert Click event and code it as follows:
Private Sub _buttonConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _buttonConvert.ClickDim pConvert1 As ConvertCtrl = _convertctrl1Dim pConvert2 As ConvertCtrl = _convertctrl2Dim pMSSource As MultiStreamSourceDim pMSTarget As MultiStreamTargetDim lStart As Int64Dim pmt As MediaTypeDim pInsertedMediaType As MediaTypepMSSource = New MultiStreamSource()pMSTarget = New MultiStreamTarget()' set the start time to be 0lStart = 0' set the input filenamepConvert1.SourceFile = _sourceFile1' set the output sample to a target objectpMSTarget.StreamCount = 2' set the target media types for video and audio streamspmt = New MediaType()pmt.Type = Leadtools.Multimedia.Constants.MEDIATYPE_VideopMSTarget.SetAcceptedMediaType(0, pmt)pmt.Type = Leadtools.Multimedia.Constants.MEDIATYPE_AudiopMSTarget.SetAcceptedMediaType(1, pmt)pmt = Nothing' get the inserted media type for the first streampInsertedMediaType = pMSTarget.GetAcceptedMediaType(0)pInsertedMediaType = Nothing' set convert 1 target objectpConvert1.TargetObject = pMSTarget' start the source conversion, so we can get the media sample formatpConvert1.StartConvert()' initialize convert 2' get the output media sample format and put it into the source objectpMSSource.StreamCount = 2pmt = pMSTarget.GetConnectedMediaType(0)pMSSource.SetMediaType(0, pmt)pmt = Nothingpmt = pMSTarget.GetConnectedMediaType(1)pMSSource.SetMediaType(1, pmt)pmt = Nothing' get the inserted media type for the first streampInsertedMediaType = pMSSource.GetMediaType(0)pInsertedMediaType = Nothing' set the output filenamepConvert2.TargetFile = _targetFile' set the source for convert 2pConvert2.SourceObject = pMSSource' start the dest conversionpConvert2.StartConvert()' convert first fileConcateFile(pConvert1, pMSTarget, pMSSource, lStart)''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'pmt = pMSTarget.GetConnectedMediaType(0)pMSTarget.SetAcceptedMediaType(0, pmt)pmt = Nothingpmt = pMSTarget.GetConnectedMediaType(1)pMSTarget.SetAcceptedMediaType(1, pmt)pmt = Nothing' change the source file to second filepConvert1.SourceFile = _sourceFile2' start converting againpConvert1.StartConvert()' convert second fileConcateFile(pConvert1, pMSTarget, pMSSource, lStart)' deliver end of sample to stop the conversionpMSSource.DeliverEndOfStream(0, 1000)pMSSource.DeliverEndOfStream(1, 1000)If pConvert2.State = ConvertState.Running ThenpConvert2.StopConvert()End If' free the source and target objectspConvert2.ResetSource()pConvert1.ResetTarget()pMSSource.Dispose()pMSTarget.Dispose()End Sub
private void _buttonConvert_Click(object sender, System.EventArgs e){ConvertCtrl pConvert1 = _convertctrl1;ConvertCtrl pConvert2 = _convertctrl2;MultiStreamSource pMSSource;MultiStreamTarget pMSTarget;Int64 lStart;MediaType pmt;MediaType pInsertedMediaType;pMSSource = new MultiStreamSource();pMSTarget = new MultiStreamTarget();// set the start time to be 0lStart = 0;// set the input filenamepConvert1.SourceFile = _sourceFile1;// set the output sample to a target objectpMSTarget.StreamCount = 2;// set the target media types for video and audio streamspmt = new MediaType();pmt.Type = Constants.MEDIATYPE_Video;pMSTarget.SetAcceptedMediaType(0, pmt);pmt.Type = Constants.MEDIATYPE_Audio;pMSTarget.SetAcceptedMediaType(1, pmt);pmt = null;// get the inserted media type for the first streampInsertedMediaType = pMSTarget.GetAcceptedMediaType(0);pInsertedMediaType = null;// set convert 1 target objectpConvert1.TargetObject = pMSTarget;// start the source conversion, so we can get the media sample formatpConvert1.StartConvert();// initialize convert 2// get the output media sample format and put it into the source objectpMSSource.StreamCount = 2;pmt = pMSTarget.GetConnectedMediaType(0);pMSSource.SetMediaType(0, pmt);pmt = null;pmt = pMSTarget.GetConnectedMediaType(1);pMSSource.SetMediaType(1, pmt);pmt = null;// get the inserted media type for the first streampInsertedMediaType = pMSSource.GetMediaType(0);pInsertedMediaType = null;// set the output filenamepConvert2.TargetFile = _targetFile;// set the source for convert 2pConvert2.SourceObject = pMSSource;// start the destination conversionpConvert2.StartConvert();// convert first fileConcateFile(pConvert1, pMSTarget, pMSSource, ref lStart);/*Restrict the output format to the media type of the source for the first fileThat is because the two files must have the same media type for both video and audioWith video, you have to make sure the files have the same frame rate! Minor changes inthe frame rate might make the connection fail!The control will tolerate differences in frame rate if you comment the next line*/pmt = pMSTarget.GetConnectedMediaType(0);pMSTarget.SetAcceptedMediaType(0, pmt);pmt = null;pmt = pMSTarget.GetConnectedMediaType(1);pMSTarget.SetAcceptedMediaType(1, pmt);pmt = null;// change the source file to second filepConvert1.SourceFile = _sourceFile2;// start converting againpConvert1.StartConvert();// convert second fileConcateFile(pConvert1, pMSTarget, pMSSource, ref lStart);// deliver end of sample to stop the conversionpMSSource.DeliverEndOfStream(0, 1000);pMSSource.DeliverEndOfStream(1, 1000);if (pConvert2.State == ConvertState.Running)pConvert2.StopConvert();// free the source and target objectspConvert2.ResetSource();pConvert1.ResetTarget();pMSSource.Dispose();pMSTarget.Dispose();}
Add the following helper methods for the concatenation work:
Private Sub ConcateFile(ByVal pConvert1 As ConvertCtrl, ByVal pMSTarget As MultiStreamTarget, ByVal pMSSource As MultiStreamSource, ByRef lStart As Long)Dim pmsSrc As MediaSample = NothingDim pmsDst As MediaSample = NothingDim MediaTimeStart As LongDim MediaTimeStop As LongDim LastStart As LongDim LastStop As LongDim lSampleStream As IntegerDim lActualDataLength As IntegerLastStop = 0Do' get the sample, allowing 10 s for the operation to completeTrylSampleStream = pMSTarget.WaitForSample(1000)Catch cex As COMExceptionIf cex.ErrorCode = CInt(ErrorCode.VFW_E_TIMEOUT) Then' end of the streamExit DoEnd IfExit DoEnd TryTry' get the target samplepmsSrc = pMSTarget.GetSample(lSampleStream, 0)' get the source bufferpmsDst = pMSSource.GetSampleBuffer(lSampleStream, 2000)Catch e1 As ExceptionExit DoEnd Try' get the media timepmsSrc.GetMediaTime(MediaTimeStart, MediaTimeStop)' get the source sample timepmsSrc.GetTime(LastStart, LastStop)' set the destination sample timepmsDst.SetTime(lStart + LastStart, lStart + LastStop)' copy the datalActualDataLength = pmsSrc.ActualDataLength' set the destination buffer' we could Marshal the unmanaged buffer here, but no need since we are merely' setting the destination to the source buffer contents (unaltered data)pmsDst.SetData(lActualDataLength, pmsSrc.GetData(lActualDataLength))' copy the other flagspmsDst.Discontinuity = pmsSrc.DiscontinuitypmsDst.Preroll = pmsSrc.PrerollpmsDst.SyncPoint = pmsSrc.SyncPoint' release the source samplepmsSrc = Nothing' deliver the destination samplepMSSource.DeliverSample(lSampleStream, 1000, pmsDst)' release the destination samplepmsDst = NothingLoop While TruepConvert1.StopConvert()lStart = LastStopEnd Sub
void ConcateFile(ConvertCtrl pConvert1, MultiStreamTarget pMSTarget, MultiStreamSource pMSSource, ref long lStart){MediaSample pmsSrc = null;MediaSample pmsDst = null;long MediaTimeStart;long MediaTimeStop;long LastStart;long LastStop;int lSampleStream;int lActualDataLength;LastStop = 0;do{// get the sample, allowing 10 s for the operation to completetry{lSampleStream = pMSTarget.WaitForSample(1000);}catch (COMException cex){if (cex.ErrorCode == (int)ErrorCode.VFW_E_TIMEOUT){// end of the streambreak;}break;}try{// get the target samplepmsSrc = pMSTarget.GetSample(lSampleStream, 0);// get the source bufferpmsDst = pMSSource.GetSampleBuffer(lSampleStream, 2000);}catch (Exception){break;}// get the media timepmsSrc.GetMediaTime(out MediaTimeStart, out MediaTimeStop);// get the source sample timepmsSrc.GetTime(out LastStart, out LastStop);// set the destination sample timepmsDst.SetTime(lStart + LastStart, lStart + LastStop);// copy the datalActualDataLength = pmsSrc.ActualDataLength;// set the destination buffer// we could Marshal the unmanaged buffer here, but no need since we are merely// setting the destination to the source buffer contents (unaltered data)pmsDst.SetData(lActualDataLength, pmsSrc.GetData(lActualDataLength));// copy the other flagspmsDst.Discontinuity = pmsSrc.Discontinuity;pmsDst.Preroll = pmsSrc.Preroll;pmsDst.SyncPoint = pmsSrc.SyncPoint;// release the source samplepmsSrc = null;// deliver the destination samplepMSSource.DeliverSample(lSampleStream, 1000, pmsDst);// release the destination samplepmsDst = null;}while (true);pConvert1.StopConvert();lStart = LastStop;}
Build, and Run the program to test it.