Merge splash screens and videos in a directory in C#

[Merge splash screens and videos]

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

This example shows how you can merge splash screens and videos in a directory. The example Merge videos in C# shows how you can concatenate two videos. Because I had to convert a lot of videos, I wrote this program to merge all of the videos in a directory. The directory should contain pairs of files with similar names, one with name ending in ” splash.wmv” and one with name ending in ” base.wmv.” The program merges the two into an output video.

When you enter a directory path in the text box and click List, the program executes the following code.

private void btnList_Click(object sender, EventArgs e)
        foreach (string filename in Directory.GetFiles(
            txtDirectory.Text, "* splash.wmv"))
        btnGo.Enabled = true;
    catch (Exception ex)

This code uses Directory.GetFiles to get a list of files in the directory with names that contain the text ” splash.wmv.” It loops through the files and adds their names to the lstSplash ListBox.

Then if you click the Go button, the following code executes.

private void btnGo_Click(object sender, EventArgs e)
    Cursor = Cursors.WaitCursor;

    for (int i = 0; i < lstSplash.Items.Count; i++)
        lstSplash.SelectedIndex = i;

    Cursor = Cursors.Default;

The button’s Click event handler loops through the file names in the list box and calls the following MergeSplash method for each.

private void MergeSplash(string splash_file)
    prgEncode.Value = 0;
    prgEncode.Visible = true;

        // Create a job.
        using (Job job = new Job())
            // Make a MediaItem containing the splash video.
            MediaItem media_item = new MediaItem(splash_file);

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

            // Restrict the splash video to 5 seconds.
            media_item.Sources[0].Clips[0].EndTime =
                new TimeSpan(0, 0, 5);

            // Add the movie.
            string base_name =
                splash_file.Replace(" splash", " base");
            media_item.Sources.Add(new Source(base_name));

            // Set the quality.
            media_item.OutputFormat.VideoProfile.Bitrate =
                new VariableQualityBitrate(90);

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

            // Set the output file name.
            media_item.OutputFileName =
                file_info.Name.Replace(" splash", "");

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

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

            // Encode.
    catch (Exception ex)

    prgEncode.Visible = false;

The MergeSplash method creates a new Job, makes a MediaItem representing the splash screen, and adds it to the job. It then sets the MediaItem‘s size so it uses the original video’s dimensions.

Next the code sets the EndTime for the video. The MediaItem contains a single source and only one clip in that source, so the clip object that represents the splash video is media_item.Sources[0].Clips[0]. The code sets its EndTime property to 5 seconds, so that video is clipped to 5 seconds.

Next the method replaces the text ” splash” with the text ” base” to get the name of the corresponding video file. It creates a new Source object for that video file and adds it to the MediaItem.

The code then finishes preparing to generate the new video file. It sets the bitrate quality to 90, sets the output directory and file name, sets CreateSubfolder to false, and installs the progress event handler. Finally the code calls the job’s Encode method to produce the merged video.

Creating a video can take a while, so processing a whole directory containing a bunch of files can take a long time.

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, files, multimedia and tagged , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

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.