wgpubs
wgpubs

Reputation: 8261

Anyone have an example of how to write audio and video simultaneously using AVAssetWriter?

Been trying to figure this out with zero success.

I can write video output no problem ... but once I try to introduce a second AVAssetWriterInput to include audio the final quicktime movie is jumpy with frames being loss left and right and audio constantly going in and out.

Thanks - wg

Upvotes: 16

Views: 3959

Answers (4)

ruoho ruotsi
ruoho ruotsi

Reputation: 1313

For anyone searching on how to do this, this apple developer example should be all you need. Good luck.

Upvotes: 0

user2616346
user2616346

Reputation: 473

I got the same problem. My solution:

1) Create two AVAssetWriter objects to write audio and video in two files.

2) Use AVMutableComposition and AVAssetExportSession to compose them into one file as suggested above.

Maybe it is ugly. But in my project I should have compose several video files, so composing audio and video adds low overhead.

Upvotes: 0

Alex Chugunov
Alex Chugunov

Reputation: 748

Are you using requestMediaDataWhenReadyOnQueue:usingBlock: to write data? If not then you should set expectsMediaDataInRealTime to YES.

Upvotes: 1

Peter DeWeese
Peter DeWeese

Reputation: 18333

If you include source we might be able to help you more, but this is a method with which I have had success in writing many audio and video tracks to a quicktime movie – I use a single AVMutableComposition with AVMutableVideoComposition and AVAudioMix. I then write it like so:

AVAssetExportSession *session = [[[AVAssetExportSession alloc] initWithAsset:[project.composition copy] presetName:presetName] retain];
    session.outputFileType = [session.supportedFileTypes objectAtIndex:0];
    session.outputURL = [NSURL fileURLWithPath:[VeporterAppDelegate createMoviePath]];
    session.videoComposition = project.videoComposition;
    session.audioMix = project.audioMix;

    session.metadata = project.metadata;

    [session exportAsynchronouslyWithCompletionHandler:^{}];

Upvotes: 1

Related Questions