Change audio gain in C#


Note: This example was written in Visual Studio 2010.

This example shows how you can re-encode a video to change its audio gain. Earlier examples such as Change video quality in C# show how to use Microsoft Expression Encoder to work with videos. This example follows a similar pattern.

This example requires the following references:

  • Microsoft.Expression.Encoder
  • Microsoft.Expression.Encoder.Api2
  • Microsoft.Expression.Encoder.Types
  • Microsoft.Expression.Encoder.Utilities

It uses the following using directives to make using the Encoder objects and methods easier.

using Microsoft.Expression.Encoder;
using Microsoft.Expression.Encoder.Profiles;
using System.IO;

The following code shows how the program does its most interesting work (re-encoding the video to change the sound gain).

private void btnCreate_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 a MediaItem containing the splash video.
            MediaItem media_item = new MediaItem(txtMovie.Text);

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

            // Set the quality.
            double gain;
            if (trkGain.Value <= 0)
                gain = (10.0 + trkGain.Value) / 10.0;
                gain = trkGain.Value;
            Console.WriteLine("Gain: " + gain);
            media_item.AudioGainLevel = gain;

            // 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;

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

            // Encode.
    catch (Exception ex)

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

Ths code displays a dialog to let you select the name of the output file. If you don’t pick a video file and click Save, the method returns.

If you do pick a file, the code creates a new Job object and adds a MediaItem representing the video you selected for input.

The code sets the media item’s size so it uses the original size. It then sets the item’s AudioGainLevel. This is a multiplicative factor. For example, the value 0.5 decreases the video’s volume by half and the value 3 increases it by a factor of 3.

The program uses a simple calculation to convert from the slider’s value between -5 and +5 into the factor. If the value is less than or equal to 0, the code adds the value to 10 and divides by 10. For example, suppose the value is -3. Then the code uses (10 – 3) / 10 = 0.7 for he gain.

If the slider’s value is greater than 0, the program uses it directly as a multiplier. For example, if the value is 3, the code sets the gain to 3.

After setting AudioGainLevel, the code sets the output directory and the output file name, installs a progress event handler, and calls the Encode method to create the output video. Download the example to see additional details.

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.

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.