Skip to content

Record the screen and the webcam into one MP4

Try it Download Windows trial dotnet add package VisioForge.DotNet.VideoCapture Pricing →

Video Capture SDK .NET

This tutorial records a webinar or a walkthrough from a C# WPF app with the cross-platform VideoCaptureCoreX engine:

  • the screen fills the picture;
  • the webcam sits in the bottom-right corner, over the screen;
  • the microphone is recorded with them;
  • everything goes into one MP4 file, H.264 video and AAC audio, with a live preview in the window.

VideoMixerSourceSettings puts the screen and the camera into one 1920x1080 picture at 30 frames per second before it is encoded, so the file needs no editing afterwards. The video mixer needs the PREMIUM edition of Video Capture SDK .NET.

SDK version

ScreenCaptureD3D11SourceSettings is a video mixer input since SDK 2026.12.2. Earlier releases take only the deprecated ScreenCaptureDX9SourceSettings there.

YouTube tutorial

Source code on GitHub

The short version

The same recording as a console app: the primary screen, the first camera and the first microphone, without the preview, until Enter is pressed.

using VisioForge.Core;
using VisioForge.Core.Types;
using VisioForge.Core.Types.X.Output;
using VisioForge.Core.Types.X.Sources;
using VisioForge.Core.VideoCaptureX;

await VisioForgeX.InitSDKAsync();
await using var cap = new VideoCaptureCoreX();
cap.OnError += (_, e) =>
    Console.WriteLine(e.Message);
var fps = new VideoFrameRate(30);
var mix = new VideoMixerSourceSettings(
    1920, 1080, fps);
mix.Add(new ScreenCaptureD3D11SourceSettings
    { FrameRate = fps },
    new Rect(0, 0, 1920, 1080));
var cams = await DeviceEnumerator.Shared
    .VideoSourcesAsync();
mix.Add(new VideoCaptureDeviceSourceSettings(
    cams[0]), new Rect(1408, 778, 1888, 1048));
cap.Video_Source = mix;
var mics = await DeviceEnumerator.Shared
    .AudioSourcesAsync();
cap.Audio_Source =
    mics[0].CreateSourceSettingsVC();
cap.Audio_Record = true;
cap.Audio_Play = false;
cap.Outputs_Add(new MP4Output("webinar.mp4"));
if (!await cap.StartAsync()) return;
Console.ReadLine();
await cap.StopAsync();

Stop it with Enter: closing the window or Ctrl+C ends the app without StopAsync, and the MP4 file is left unfinished. Its code: screen-webcam-pip-short-console on GitHub.

Required NuGet packages

These are the x64 packages, so the project sets <PlatformTarget>x64</PlatformTarget>; a 32-bit app takes the .x86 packages instead. See Windows deployment for the full list.

dotnet add package VisioForge.DotNet.VideoCapture
dotnet add package VisioForge.CrossPlatform.Core.Windows.x64
dotnet add package VisioForge.CrossPlatform.Libav.Windows.x64

The window in XAML

The camera and the microphone are picked on the left, the preview is on the right. Start is disabled until the SDK has loaded, and Stop until the recording has started.

<Window x:Class="ScreenWebcamPipWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vf="clr-namespace:VisioForge.Core.UI.WPF;assembly=VisioForge.Core"
        Title="Screen + webcam to MP4" Height="560" Width="1000"
        Loaded="Window_Loaded" Closing="Window_Closing">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="260" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0" Margin="0,0,10,0">
            <Label Content="Camera" />
            <ComboBox x:Name="cbCamera" />
            <Label Content="Microphone" Margin="0,10,0,0" />
            <ComboBox x:Name="cbMicrophone" />

            <StackPanel Orientation="Horizontal" Margin="0,20,0,0">
                <Button x:Name="btStart" Content="Start" Width="100" IsEnabled="False"
                        Click="btStart_Click" />
                <Button x:Name="btStop" Content="Stop" Width="100" Margin="10,0,0,0"
                        IsEnabled="False" Click="btStop_Click" />
            </StackPanel>
        </StackPanel>

        <vf:VideoView x:Name="VideoView1" Grid.Column="1" Background="Black" />
    </Grid>
</Window>

The code behind the window

using System.ComponentModel;
using System.IO;
using System.Windows;
using VisioForge.Core;
using VisioForge.Core.Types;
using VisioForge.Core.Types.X.Output;
using VisioForge.Core.Types.X.Sources;
using VisioForge.Core.VideoCaptureX;
using Rect = VisioForge.Core.Types.Rect; // not the WPF Rect

namespace ScreenWebcamPipWpf;

public partial class MainWindow : Window
{
    private VideoCaptureCoreX videoCapture1 = null!;

    private VideoCaptureDeviceInfo[] cameras = null!;

    private AudioCaptureDeviceInfo[] microphones = null!;

    public MainWindow()
    {
        InitializeComponent();
    }

    private async void Window_Loaded(object sender, RoutedEventArgs e)
    {
        await VisioForgeX.InitSDKAsync();

        videoCapture1 = new VideoCaptureCoreX(VideoView1);

        // Show SDK errors in the title bar
        videoCapture1.OnError += (_, args) => Dispatcher.InvokeAsync(() => Title = args.Message);

        cameras = await DeviceEnumerator.Shared.VideoSourcesAsync();
        foreach (var device in cameras)
            cbCamera.Items.Add(device.DisplayName);

        microphones = await DeviceEnumerator.Shared.AudioSourcesAsync();
        foreach (var device in microphones)
            cbMicrophone.Items.Add(device.DisplayName);

        if (cbCamera.Items.Count > 0) cbCamera.SelectedIndex = 0;
        if (cbMicrophone.Items.Count > 0) cbMicrophone.SelectedIndex = 0;

        btStart.IsEnabled = true;
    }

    private async void btStart_Click(object sender, RoutedEventArgs e)
    {
        if (cbCamera.SelectedIndex < 0)
        {
            Title = "No camera";
            return;
        }

        Title = "Screen + webcam to MP4";

        // One 1920x1080 picture at 30 fps: the screen fills it, the camera sits in the bottom-right corner
        var mixer = new VideoMixerSourceSettings(1920, 1080, new VideoFrameRate(30));
        mixer.Add(new ScreenCaptureD3D11SourceSettings { FrameRate = new VideoFrameRate(30) },
            new Rect(0, 0, 1920, 1080));
        mixer.Add(new VideoCaptureDeviceSourceSettings(cameras[cbCamera.SelectedIndex]),
            new Rect(1408, 778, 1888, 1048)); // 480x270, 32 pixels from the edges
        videoCapture1.Video_Source = mixer;

        var hasMic = cbMicrophone.SelectedIndex >= 0;
        videoCapture1.Audio_Source = hasMic ? microphones[cbMicrophone.SelectedIndex].CreateSourceSettingsVC() : null;
        videoCapture1.Audio_Record = hasMic; // no microphone: record video only
        videoCapture1.Audio_Play = false; // record the microphone without playing it back

        // H.264 on the GPU encoder when there is one, OpenH264 otherwise; AAC audio
        videoCapture1.Outputs_Clear();
        videoCapture1.Outputs_Add(new MP4Output(Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), $"webinar_{DateTime.Now:yyyyMMdd_HHmmss}.mp4")));

        btStart.IsEnabled = false;
        var started = await videoCapture1.StartAsync();
        btStop.IsEnabled = started;
        btStart.IsEnabled = !started;
    }

    private async void btStop_Click(object sender, RoutedEventArgs e)
    {
        btStop.IsEnabled = false;
        await videoCapture1.StopAsync();
        btStart.IsEnabled = true;
    }

    private async void Window_Closing(object sender, CancelEventArgs e)
    {
        if (btStart.IsEnabled)
        {
            videoCapture1.Dispose();
            VisioForgeX.DestroySDK();
            return;
        }

        e.Cancel = true; // still loading or recording: finish the MP4 first
        if (!btStop.IsEnabled) return; // already loading, starting or stopping
        btStop.IsEnabled = false;
        await videoCapture1.StopAsync();
        btStart.IsEnabled = true;
        await Dispatcher.InvokeAsync(Close); // not Close(): WPF throws inside Closing
    }
}

How it works

  1. SDK initialization — VisioForgeX.InitSDKAsync runs once, when the window loads. The first run on a PC takes longer, so Start is enabled only after it returns. VisioForgeX.DestroySDK releases it when the window closes.
  2. The mixer — VideoMixerSourceSettings is the video source of the engine. Its constructor sets the size and the frame rate of the recorded picture, and each Add places one input in it. Rect is left, top, right and bottom in that picture, so new Rect(1408, 778, 1888, 1048) is a 480x270 box 32 pixels from the right and bottom edges. An input added later is drawn over the ones before it, so the camera goes in second.
  3. The screen — ScreenCaptureD3D11SourceSettings captures the whole primary monitor with the mouse cursor. It uses DXGI Desktop Duplication, which starts at once even on a still screen, and falls back to Windows Graphics Capture where Desktop Duplication is not supported. Its default frame rate is 10 fps, so the code sets 30, the rate of the mixer. The monitor is scaled to the 1920x1080 box: a 4K screen is recorded at half its size.
  4. The camera — new VideoCaptureDeviceSourceSettings(camera) picks 1920x1080 at 25 fps or more when the camera has it, otherwise 1280x720, otherwise another format. The mixer scales it to the corner box.
  5. The microphone — CreateSourceSettingsVC turns the chosen microphone into the engine's Audio_Source. Audio_Record puts it in the file, and Audio_Play is false, so it is not played back on the speakers.
  6. MP4 output — MP4Output with no encoder settings picks NVIDIA NVENC, then AMD AMF, then Intel Quick Sync, and OpenH264 on the CPU when none of them is present. Audio is AAC. An output stays in the engine after a recording, so Outputs_Clear runs before each new one.
  7. Start, stop and close — StartAsync returns whether the recording started, and StopAsync finalizes the MP4 file. Closing the window during a recording cancels the close, awaits StopAsync, and closes again.

Notes

  • WPF has its own Rect type in System.Windows, so the file names the SDK's one with using Rect = VisioForge.Core.Types.Rect;.
  • The mixer does not keep the aspect ratio of an input: a 4:3 camera or a 16:10 monitor is stretched to its 16:9 box. Give such an input a box of its own proportions, for example new Rect(1528, 778, 1888, 1048) (360x270) for a 4:3 camera.
  • The preview shows the screen it records, so the app window appears inside itself, in the preview and in the file. On a PC with two monitors, move the window to the one that is not recorded, or record another monitor with MonitorIndex; with one monitor, minimize it after Start and restore it to stop.
  • The first camera in the list is not always a webcam: a capture card with no signal is listed as a camera too, and records nothing. Pick the camera by name in that case.
  • With no microphone in the list, the MP4 holds video only.
  • Without a licence the SDK runs as a trial and draws its watermark into the recording.

Frequently Asked Questions

How do I move the webcam to another corner or change its size?

Change the Rect of the camera input. It is left, top, right and bottom in the mixer's picture: for the bottom-left corner use new Rect(32, 778, 512, 1048), for the top-right new Rect(1408, 32, 1888, 302). A bigger box makes a bigger camera; keep its proportions the same as the camera's to avoid stretching. VideoMixerSourceSettings also takes the input as left, top, right, bottom integers in the Add(source, left, top, right, bottom) overload.

Can I record another monitor or a part of the screen?

Yes. Set MonitorIndex on ScreenCaptureD3D11SourceSettings to record another monitor (0 is the first monitor, -1 the primary), or set its Rectangle to record an area of the monitor. Keep the mixer box of the screen in the area's proportions, or the area is stretched. See Screen Source Configuration for the full set of options.

Can I record the system sound as well as the microphone?

Yes. AudioMixerSourceSettings mixes several audio sources into one track. In btStart_Click, replace the hasMic line and the three Audio_ lines after it with the code below: it adds the chosen microphone and a LoopbackAudioCaptureDeviceSourceSettings for the system's default speakers (the first ones when none is marked as default) to the mixer's Inputs, and records both, or the one the PC has. DeviceEnumerator.Shared.AudioOutputsAsync(AudioOutputDeviceAPI.WASAPI2) lists the speakers; the screen capture to MP4 tutorial lets the user pick one instead.

// The microphone and the sound of the default speakers, mixed into one track
var speakers = await DeviceEnumerator.Shared.AudioOutputsAsync(AudioOutputDeviceAPI.WASAPI2);
var speaker = Array.Find(speakers, device => device.IsDefault) ?? speakers.FirstOrDefault();

var audio = new AudioMixerSourceSettings();
if (cbMicrophone.SelectedIndex >= 0) audio.Inputs.Add(microphones[cbMicrophone.SelectedIndex].CreateSourceSettingsVC());
if (speaker != null) audio.Inputs.Add(new LoopbackAudioCaptureDeviceSourceSettings(speaker));

// Record the mix, or video only when the PC has neither
videoCapture1.Audio_Source = audio.Inputs.Count > 0 ? audio : null;
videoCapture1.Audio_Record = audio.Inputs.Count > 0;
videoCapture1.Audio_Play = false;

Which edition of Video Capture SDK .NET do I need?

The video mixer, and with it picture-in-picture, needs the PREMIUM edition. Without a licence the SDK runs as a trial with a watermark in the video. Visit the product page for the editions and prices.

See Also