Sti
Sti

Reputation: 8493

Is it possible to edit the volume and pan of a video's audio-track and export to a stand-alone video with that information?

I'm recording video in my app. When the video is recorded, I want to let the user preview the recording with controls for changing volume and panning of the audio track. To preview the video recording, I'm using an AVMutableComposition and adding the AVAsset from the recorded file's local URL, and using the Composition as an AVPlayerItem to play through AVPlayer. I am doing this because I want to add volume and pan-"filters(?)" to the audio tracks in the composition, and later export the entire composition.

Playback is working perfectly, but I can't find any way to add such "filters" (to change the volume or the panning of the AVAssetTrack or anything else on this level). So far, I've only been able to change the volume on playback by using this:

 NSMutableArray *audioParam = [NSMutableArray array];   
 AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
 [audioInputParams setVolume:volumeSlider.value atTime:kCMTimeZero];
 [audioInputParams setTrackID:[audioTrack trackID]];
 [audioParam addObject:audioInputParams];

 AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
 [audioMix setInputParameters:audioParam];

 [playerItem setAudioMix:audioMix];

Here, I believe, that I'm only changing the representation of the composition, and not the composition itself. When the user is happy with the level of volume and pan, he should click "Accept", and I will export the composition to a stand alone movie-file (I.E it won't make any difference to the composition if I change the volume of the player this way).

Is there a way to add the source movie to an AVComposition, change the volume/pan of the audio, and export to a new file with the new information?

I'm also, as mentioned, looking for this for panning an audio stereo or mono track, but I can't find anything at all. The only mentioning of panning in Apple Docs is by using AVAudioPlayer, which I can't use as this is a video-file. Also, changing pan in the player would do nothing good just as I explained with volume. Is there really no way to change panning through either AVAsset, AVAssetTrack, AVComposition or anything to do with this?

I was thinking I might have to do it manually, by reducing the volume on each channel for a stereo audio track, but I can't find any way to do this either.

Simply put; I'm looking for a way to change the volume- and pan(left/right)-level of the audio-track of a single video(prefferably during preview/playback of said video), and then be able to export this video to a new video-file, which then will have this new volume- and pan-information as source information.

Example:

1. Record Video.

2. Preview Video.

3. Set Pan of audio to far left (audio only in left channel), and Volume to 20%

4. Export video with new information

5. If I now upload this video-file to internet, and sombody downloads it on any device/computer, it will only have sound in the left speaker, and it will be fairly low volume.

Upvotes: 6

Views: 2425

Answers (1)

nsdebug
nsdebug

Reputation: 364

Since you want these parameters (volume and pan) applied to your output you will likely want to create an AVMutableComposition and then an AVAssetExportSession to spit it all out as you expect.

To handle panning, I usually deal with that in the audio I'm recording or about to put into an AVMutableComposition by simply doing a quick multiply on the samples of either channel as appropriate. This is dependent on how your app is structured where you might fit this in.

To get volume control you can set the volume of each AVMutableCompositionTrack or AVAssetTrack added to your AVComposition or you can apply an AVMutableAudioMix to the AVAssetExportSession audioMix property. When you perform the export, it will be exported with said audio mix. This will "print" your volume changes so you don't have to alter them during playback.

The audioMix is nice because it lets you do fades, etc.

Upvotes: 0

Related Questions