How to Combine Video and Audio from Separate Sources

LEADTOOLS Multimedia SDK has made a name for itself by offering high level controls that take the complexity out of developing DirectShow and Media Foundation applications. Most core operations such as conversion, capture and playback can be done with only a few lines of code. In addition to the high level features, LEADTOOLS is flexible enough for any advanced application with more difficult scenarios and still offers as much or as little control as desired over the graphs and topologies under the hood.

The ConvertCtrl is a perfect example of this. By setting a source file, destination file and format you can make an audio/video conversion application in minutes. Developers who wish to dive deeper can supply their own media samples and process the conversion frame by frame.

That is precisely the scenario described in the following example. Usually, conversion is a one to one process where you have a single file being converted to another single file. But what happens if you have your audio and video in separate files? This can be accomplished by employing three ConvertCtrl objects: one for decoding the audio file, one for decoding the video file, and the final control for splicing them together. In between the decoding and encoding you will get the media samples, merge them into a MultiStreamSource and use that source in the 3rd ConvertCtrl:


vidConvert = new ConvertCtrl();
audConvert = new ConvertCtrl();

//Init the SampleTargets. The Video and Audio data from our files will write to these.
SampleTarget vidTarget = new SampleTarget();
SampleTarget audTarget = new SampleTarget();


//Set the Media Type of the VideoTarget to Video
MediaType mt = new MediaType();
mt.Type = Constants.MEDIATYPE_Video;
vidTarget.SetAcceptedMediaType(mt);
vidConvert.TargetObject = vidTarget;

//Clear
mt = null;

//Set the Media type for our AudioTarget to Audio.
mt = new MediaType();
mt.Type = Constants.MEDIATYPE_Audio;
audTarget.SetAcceptedMediaType(mt);
audConvert.TargetObject = audTarget;

//Clear
mt = null;

//Set the ConvertCtrls to point to the Files as their sources.
vidConvert.SourceFile = videoFile;
audConvert.SourceFile = audioFile;

//Start running the two conversion controls. These are writing to the Sample Targets.
vidConvert.StartConvert();
audConvert.StartConvert();

//Enter the Combine Method
CombineVideoAndAudio(vidTarget, audTarget);

This is just the beginning of the process. To see what goes on inside the CombineVideoAndAudio function and try it out for yourself, you can download the full example from the original forum post.

This entry was posted in Multimedia Imaging and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *