Simultaneously capturing video from two (or more) web cams with Microsoft Expression Encoder

By February 24, 2014.NET, C#

One of the solution to capture video with a USB camera is using Microsoft Expression Encoder 

After adding references to these required assemblies

Microsoft.Expression.Encoder
Microsoft.Expression.Encoder.Types
Microsoft.Expression.Encoder.Utilities

var videoDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video);
First you receive a collection of available video capture devices
and start a live job method for the encoder
video = videoDevices[videoDeviceIndex];
_job = new LiveJob();
// Create a new device source. We use the first audio and video devices on the system
_deviceSource = _job.AddDeviceSource(video, audio);
_deviceSource.PickBestVideoFormat(new Size(640, 480), 15);
// Get the properties of the device video
SourceProperties sp = _deviceSource.SourcePropertiesSnapshot();
// Resize the preview panel to match the video device resolution set
panelVideoPreview.Size = new Size(sp.Size.Width, sp.Size.Height);
// Setup the output video resolution file as the preview
_job.OutputFormat.VideoProfile.Size = new Size(sp.Size.Width, sp.Size.Height);
After properties of the job and deviceSource you set the preview on a System.Windows.Forms.Panel
and set the first web cam as active source
_deviceSource.PreviewWindow =
new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));
_job.ActivateSource(_deviceSource);
An event handler for start recording button will look like this,
you can set the publish format and file name before start encoding to it
// Sets up publishing format for file archival type
FileArchivePublishFormat fileOut = new FileArchivePublishFormat();
// Sets file path and name
fileOut.OutputFileName = String.Format("C:\\VideoClips{0:yyyyMMdd_hhmmss}.wmv", DateTime.Now);
// Adds the format to the job. You can add additional formats as well such as
// Publishing streams or broadcasting from a port
_job.PublishFormats.Add(fileOut);
// Start encoding
_job.StartEncoding();

 

After doing this you can easily switch between connected web cams (assuming you have two available cameras connected)

You can notice that we have two System.Windows.Forms.Panel control and we are switching between the two while the recording is running. For a complete solution see this article

private void SwitchCameraWhenRecording(object sender, EventArgs e)
{
if (CurrentCamera == ActiveCamera.Camera1)
{
_job.ActivateSource(_deviceSource2);
_deviceSource2.PreviewWindow =
new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));
CurrentCamera = ActiveCamera.Camera2;
}
else
{
_job.ActivateSource(_deviceSource);
_deviceSource.PreviewWindow =
new PreviewWindow(new HandleRef(panelVideoPreview, panelVideoPreview.Handle));
CurrentCamera = ActiveCamera.Camera1;
}
}
The following two tabs change content below.