Merge videos in C#

[merge videos]

(Note that this example was written in Visual Studio 2010.)

My original problem was that I had created a bunch of videos and needed to add a splash screen at the beginning of each. This example merges videos so it lets me do just that.

Before you start writing code, you need to add references to the Microsoft Expression Encoder libraries. See the previous post Change video quality in C# for details on how to get started.

When you click this example’s + button, the program displays a OpenFileDialog to let you add a new video file to the list. If you select a file in the list and click the × button, the program removes it from the list.

After you select your files, click the Merge button. The following code concatenates the videos into a single output video.

// Build the merged video.
private void btnMerge_Click(object sender, EventArgs e)
    if (sfdMerged.ShowDialog() != DialogResult.OK) return;
    Cursor = Cursors.WaitCursor;
    prgEncode.Value = 0;
    prgEncode.Visible = true;

        // Create a job.
        using (Job job = new Job())
            // Make one MediaItem containing all of the sources.
            MediaItem media_item =
                new MediaItem(lstFiles.Items[0].ToString());

            for (int i = 1; i < lstFiles.Items.Count; i++)
                    new Source(lstFiles.Items[i].ToString()));

            // Set the output directory.
            FileInfo file_info = new FileInfo(sfdMerged.FileName);
            job.OutputDirectory = file_info.DirectoryName;

            // Set the output file name.
            media_item.OutputFileName = file_info.Name;

            // Don't create a subdirectory.
            job.CreateSubfolder = false;

            // Use the original size.
            media_item.OutputFormat.VideoProfile.Size =

            // Install the progress event handler.
            job.EncodeProgress += job_EncodeProgress;

            // Encode.
    catch (Exception ex)

    Cursor = Cursors.Default;
    prgEncode.Visible = false;

The code first uses a SaveFileDialog to let you select the output file name. If you select a file and click Save, the program displays the ProgressBar named prgEncode.

Next the program creates a Job object. It then makes a MediaItem representing the first video in the list and adds it to the Job.

The code then loops through the other video files, creating a Source for each and adding the Source to the MediaItem object’s Sources collection. When the loop is finished, the Job contains a single MedieItem that contains all of the video sources.

The program then sets the job’s output directory and the media item’s output file name. It sets the job’s CreateSubfolder property to false, sets the media item’s output size equal to its original video size (so the video’s dimensions aren’t changed), and installs the progress event handler.

Finally the code calls the job’s Encode method. That method concatenates the videos in the media item’s Sources collection and creates a combined video.

Note that this example doesn’t set the resulting video’s quality, so the result tends to be fairly high quality and a fairly large file. If you want to change the quality, see the example Change video quality in C#.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in animation, multimedia and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

10 Responses to Merge videos in C#

  1. reem ahmed saber says:

    but i didn’t understand what will i do
    any help please to merge videos c# and display final video

    • RodStephens says:

      To use this example, download it and build it in Visual Studio. Click the + button to load videos and then click Merge to create the merged video.

      You should be able to look at the code to figure out how to copy the merging part into your program if you want to do that. Basically it’s the code inside the try block.

      If you want to display the merged video in your program after you create it, you should be able to do that. Depending on whether you’re using Windows Forms or WPF, you’ll use a different method. For example, see:

  2. Luthfi says:

    Hi, thank you for good stuff.
    Is it possible to merge mp4 files?

  3. S.K says:

    Hello RodStephens,

    Is it necessary to install Microsoft expression encoder or library is enough for doing these things.

    With library I am facing “An exception of type ‘System.TypeInitialization occured in Microsoft.Expression.Encoder.dll but was not handle in user code.

    Additional information: The type initializer for ‘Microsoft.Expression.Encoder.SkuManager’ threw an exception.

    This issue I am facing in creating media items.


  4. RodStephens says:

    I’m not sure because I don’t do this very often. I think you need the SDK installed but I can’t figure out how to get it. It might be installed as part of the Expression Encoder installation. Here are a couple of pages that may help.

  5. zain says:

    Hi RodStephens,

    First of all thanks a lot for such a good post.
    So can we do the same in Visual C++, and what resources will be required to do the same??

    • RodStephens says:

      It should be possible but I don’t have any details because I haven’t used VC++ in ages. You’ll need to download Expression Encoder, which you can get from here:

      Download Expression Encoder 4 with Service Pack 1.

      You should also look at this post, which is the one where I got started with Encoder. It has a little more background.

      Change video quality in C#

      Maybe if you add references to the library Visual Studio can help you figure what’s available to a VC++ project.

      Good luck. If you figure it out, post a little note here in case anyone needs to follow in your footsteps.

      • zain alvi says:

        Hi RodStephens,

        Thanks for your reply, I’ve tried working it out, however, it couldn’t bring me any success. Can u please look into this matter?

        Thanks. 🙂

        • RodStephens says:

          Sorry but I don’t use VC++ and haven’t used any flavor of C++ in more than 20 years. You’ll be able to figure it out more quickly than I can. Everything I know about working with Expression Encoder is in this post. Sorry I can’t be more help.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.