Mikael
Mikael

Reputation: 3612

AVFoundation AVMutableComposition - video with audio - freezes video

I have a recorded video with sound. When adding that to an AVMutableComposition and then export it, the video will freeze when the sound is playing. What am I missing?

I have my two tracks:

_compositionVideoTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
_compositionAudioTrack = [self.composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

Then create the tracks:

AVAssetTrack *clipVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *clipAudioTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

Then I insert the tracks:

[_compositionVideoTrack insertTimeRange:editRange ofTrack:clipVideoTrack atTime:self.composition.duration error:&editError];
[_compositionAudioTrack insertTimeRange:editRange ofTrack:clipAudioTrack atTime:self.composition.duration error:&editError];

Finally I export it:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:urlAsset presetName:AVAssetExportPresetPassthrough];
exportSession.outputFileType = AVFileTypeQuickTimeMovie;

And the file is .mp4. Could it be the file format?

Upvotes: 8

Views: 1434

Answers (3)

DimaC
DimaC

Reputation: 436

You ca try to do like this:

AVMutableComposition *mutableComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *videoCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:clipVideoTrack atTime:kCMTimeZero error:nil];
AVMutableCompositionTrack *audioCompositionTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

and then init AVAssetExportSession with mutable composition

AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality];

hope this will help you

Upvotes: 0

Paul Nadolinskyi
Paul Nadolinskyi

Reputation: 114

I think you should add AVMutableVideoCompositionInstruction

Upvotes: -1

iDeveloper
iDeveloper

Reputation: 498

Try Exporting using below

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:self.inputAsset presetName:AVAssetExportPresetLowQuality];
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

Upvotes: 0

Related Questions