C# universal platform audio merge or input in video
UWP provide APIs about this requirement. This feature can be implemented in uwp by BackgroundAudioTracks method of MediaComposition class. Details for how to do please reference Add a background audio track to a composition. And you can find a sample from scenario 3 of MediaEditing official sample.
For example:
// Create the original MediaComposition
var clip = await MediaClip.CreateFromFileAsync(pickedFile);
composition = new MediaComposition();
composition.Clips.Add(clip);
// Add background audio
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".wav");
picker.FileTypeFilter.Add(".flac");
var audioFile = await picker.PickSingleFileAsync();
if (audioFile == null)
{
rootPage.NotifyUser("File picking cancelled", NotifyType.ErrorMessage);
return;
}
var backgroundTrack = await BackgroundAudioTrack.CreateFromFileAsync(audioFile);
composition.BackgroundAudioTracks.Add(backgroundTrack);
// Render to MediaElement
mediaElement.Position = TimeSpan.Zero;
mediaStreamSource = composition.GeneratePreviewMediaStreamSource((int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight);
mediaElement.SetMediaStreamSource(mediaStreamSource);
The MediaComposition is created from the video file. The BackgroundAudioTrack is created from the Mp3 or other audio files you want to merge to the video. At last, we need to render the MediaComposition to file(the sample is render to MediaElement for playing).