Working with Multiple Audio Streams in Video Files¶
Video Edit SDK .NET VideoEditCore
Introduction to Multiple Audio Streams¶
Video files commonly contain multiple audio streams to support different languages, commentary tracks, or audio qualities. For developers building video editing or processing applications, properly handling these multiple streams is essential for creating professional-grade software. This guide explores the technical challenges and solutions for working with multi-audio stream video files in .NET applications.
Multiple audio streams serve several important purposes in video applications:
- Multilingual support: Providing audio tracks in different languages
- Commentary tracks: Including director's commentary or alternative narration
- Audio quality variations: Offering different bitrates or formats (stereo/surround)
- Special audio channels: Supporting descriptive audio for accessibility
Technical Background on Audio Stream Handling¶
How VideoEditCore Opens a Clip¶
VideoEditCore builds its timeline with DirectShow Editing Services (DES). DES takes an audio stream of a clip by the number of the source filter's audio output pin, and AudioSource.StreamNumber (the streamNumber argument of the AudioSource constructor) is that number.
VideoEditCore opens WMV, ASF, WMA, WAV and MP3 files through the Windows filters, and every other clip (MP4, MKV, MOV, AVI, MPEG-TS, WebM and the rest) through the VisioForge FFMPEG Source from the VisioForge.DotNet.Core.Redist.FFMPEG package. LAV Filters are not used, even when they are installed, and DirectShow_Filters_Blacklist_Add does not change which filter opens a clip.
Selecting an Audio Stream¶
The FFMPEG Source gives DES one audio pin: the first audio stream of the file.
StreamNumber = 0, the default, takes the first audio stream.StreamNumber = 1or higher on a clip opened through the FFMPEG Source fails:OnErrorreportsInvalid stream number for a source(HRESULT=0x80040265) andStartAsyncreturnsfalse. This was measured on MP4, MKV and AVI files with two audio streams.
Recommended Approach¶
External Audio File Method¶
To use a stream other than the first one, save it to its own WAV or MP3 file and add that file as the audio input, next to the video from the original file:
// The video from the original file, the audio from its second stream saved as WAV
var duration = TimeSpan.FromSeconds(60);
await videoEdit.Input_AddVideoFileAsync(new VideoSource("movie.mkv", TimeSpan.Zero, duration));
await videoEdit.Input_AddAudioFileAsync(new AudioSource("movie-audio2.wav", TimeSpan.Zero, duration));
Any tool that extracts an audio stream does this; with the FFmpeg command line, ffmpeg -i movie.mkv -map 0:a:1 movie-audio2.wav saves the second audio stream. Use WAV or MP3 for that file: VideoEditCore refuses an audio-only file in another container, such as M4A or MKA, with No audio streams found in input file.
For more detailed implementation examples, refer to the merge video and audio without re-encoding tutorial.
Performance Considerations¶
Working with multiple audio streams can impact performance, especially with high-resolution video or complex processing requirements. Consider these optimization strategies:
- Pre-extract audio streams for complex editing projects
- Use hardware acceleration when available
- Implement buffering mechanisms for smoother playback
- Consider temporary downsampling during preview operations
Required Components and Dependencies¶
To implement the techniques described in this guide, you'll need to include the following dependencies:
- Video Edit SDK redist x86 x64
- FFMPEG redist, which opens MP4, MKV, MOV, AVI and the other clips x86 x64
For information on deploying these dependencies to end-user systems, refer to the deployment documentation.
Conclusion¶
VideoEditCore takes the first audio stream of a clip. To use another one, save it as a separate WAV or MP3 file and add that file as the audio input, as shown above.
For advanced implementation scenarios and additional code examples, check out our GitHub repository.
Visit our GitHub page to get more code samples.