Thanks for clarifying! You want to mix multiple WAV files as tracks layered on top of each other (i.e., mixed/summed audio), aligned starting at the same zero timestamp, not concatenated one after another.
That requires a different FFmpeg approach:
- Concat joins files end-to-end (one after another).
- Mixing overlays them (combines audio streams simultaneously).
What's happening with your "weird miscalculations"?
You likely got timing and channel mismatches because concat expects uniform streams for seamless joining, but mixing sums the audio signals.
Here's the right way:
You want to mix all WAV files in the folder into a single combined audio file, aligned at start, scales volumes so it doesn't clip, and outputs that.
Complete C# class for layering (mixing) WAV files using FFmpeg amix filter
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
public class WavMixer
{
/// <summary>
/// Input folder containing WAV files to mix.
/// </summary>
public string InputFolder { get; set; }
/// <summary>
/// Output WAV file path.
/// </summary>
public string OutputFile { get; set; }
/// <summary>
/// Mixes all WAV files in InputFolder into OutputFile layer by layer (simultaneously).
/// </summary>
/// <returns>True if mixing succeeded, false on failure.</returns>
public bool Mix()
{
if (string.IsNullOrWhiteSpace(InputFolder) || !Directory.Exists(InputFolder))
{
MessageBox.Show("Input folder is invalid or does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
var wavFiles = Directory.GetFiles(InputFolder, "*.wav").OrderBy(f => f).ToArray();
if (wavFiles.Length == 0)
{
MessageBox.Show("No WAV files found in the input folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (string.IsNullOrWhiteSpace(OutputFile))
{
MessageBox.Show("Output file path is not set.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
try
{
// Construct FFmpeg input arguments for each file
// Example: -i file1.wav -i file2.wav -i file3.wav ...
string inputArgs = string.Join(" ", wavFiles.Select(f => $"-i \"{f}\""));
// Number of inputs for amix
int inputCount = wavFiles.Length;
// amix filter string to mix all inputs
// low volume to avoid clipping by dividing by number of inputs ('dropout_transition=0' avoids noise artifacts)
// Options: normalize=0 (no normalization), dropout_transition=0 (better mixing edges)
string filter = $"amix=inputs={inputCount}:dropout_transition=0";
// Full ffmpeg args:
// e.g.: -y -i f1.wav -i f2.wav -filter_complex amix=inputs=2:dropout_transition=0 output.wav
string arguments = $"-y {inputArgs} -filter_complex \"{filter}\" -c:a pcm_s16le \"{OutputFile}\"";
var startInfo = new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = arguments,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
using (var process = new Process() { StartInfo = startInfo })
{
process.OutputDataReceived += (s, e) => { /* Optional: handle output */ };
process.ErrorDataReceived += (s, e) => { /* Optional: handle error output or progress info */ };
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.ExitCode != 0)
{
MessageBox.Show($"FFmpeg exited with error code {process.ExitCode}.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("Failed to mix WAV files: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
How to use example (in your WinForms code-behind):
private void btnMix_Click(object sender, EventArgs e)
{
var mixer = new WavMixer
{
InputFolder = txtInputFolder.Text,
OutputFile = txtOutputFile.Text
};
if (mixer.Mix())
MessageBox.Show("WAV files mixed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
txtInputFolder = TextBox where user enters/selects folder with WAV files.
txtOutputFile = TextBox where user enters/selects output WAV file path.
Explanation of the FFmpeg command:
ffmpeg -y -i file1.wav -i file2.wav -i file3.wav \
-filter_complex amix=inputs=3:dropout_transition=0 \
-c:a pcm_s16le output.wav
-i fileX.wav inputs each WAV file.
filter_complex amix=inputs=N mixes them all.
dropout_transition=0 smooths mixing on inputs ending early.
-c:a pcm_s16le encodes output as standard WAV audio (16-bit PCM).
-y forces overwrite of output.
Important notes:
- All WAV files should have the same sample rate and number of channels (e.g., 44100 Hz stereo). Otherwise, mixing can cause unexpected output or unwanted resampling artifacts.
- If your WAVs differ in format, you can optionally convert them first to a common format (e.g., 16-bit, 44.1kHz, stereo) before mixing. This is usually handled with extra FFmpeg steps if needed.
- This method overlays audio starting at the same point, mixing them together.
- Volume is automatically scaled by dividing signals to avoid clipping.
If you want me to help you extend this with sample rate/channel format normalization automatically within FFmpeg or add UI for folder/file browsing, just ask!