2

I try to input a .aac in .mp4

as we known

ffmpeg cmd
ffmpeg -i audio.mp3 -i video.mp4 -c copy output

How to do it in c# uwp, i have search in google i not see this any result regarding this issue , everyone has posted converting issue.

But how to merge it with c# any sample prj or information will be great

2 Answers 2

5

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).

Sign up to request clarification or add additional context in comments.

3 Comments

render the MediaComposition to file genereted resolution get so low is there a way to get clone of the mp4 file with the audio
@AsifShariar, MediaComposition.RenderToFileAsync has several overload methods, with this one, you may assign MediaEncodingProfile to what you want to keep quality. If you still have issue when you using these parameters, you can open a new thread. thanks for understanding.
It works but the file size too big ; suppose i use create mp4 HD1080p , my default file resolution also 1080 p but that size 50 mb and the .aac 2 mb, so total 52 mb , so after encode that with the sound by creating new mp4 HD1080p by Rendering file size get 400 mb , any solution about it
0
public async Task<StorageFile> MergeVideoAudioAsync(StorageFile videoFile, StorageFile audioFile, string finalFileName, StorageFolder destinationFolder)
    {
        MediaComposition composition = new MediaComposition();
        var file = await destinationFolder.CreateFileAsync(finalFileName, CreationCollisionOption.GenerateUniqueName);
        var clip = await MediaClip.CreateFromFileAsync(videoFile);
        composition.Clips.Add(clip);
        var backgroundTrack = await BackgroundAudioTrack.CreateFromFileAsync(audioFile);
        composition.BackgroundAudioTracks.Add(backgroundTrack);
        await composition.RenderToFileAsync(file);
        return file;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.