2011-02-23 30 views
5

我正在使用AVFoundation創建使用圖像和聲音文件的電影文件(mp4)。如何在iOS4上使用AVFoundation在電影中添加音頻文件內容?

我已經使用AVAssetWriterInputPixelBufferAdaptor成功創建了影片文件,該文件在影片文件中追加CVPixelBufferRef(從UIImage對象中抽取)。

現在,我想添加該電影中文件的音頻內容。從設備麥克風獲取數據並不是我在這裏想的。而且,我找不到任何類似於AVAssetWriterInputPixelBufferAdaptor的東西,它可以幫助將音頻數據寫入該電影文件。

我在這裏錯過了什麼嗎?

+0

我有非常類似的問題。在我的應用程序中,我創建了一個基於存儲在相機膠捲目錄中的視頻文件的電影(使用直接像素訪問進行了一些更改)。爲此,我使用了AVAsset/AVAssetReader/AVAssetWriter類和相關的。 我現在唯一的問題是我的電影不包含任何音軌,只是視頻數據。我想將音軌追加到來自原始視頻文件的電影中。出於某種原因,我無法在視頻正在寫入時這樣做,所以現在我正在尋找一種在視頻轉換完成後執行此操作的方法。 – peter 2011-02-27 15:59:28

回答

5

至少對我來說,解決方案是使用AVMutableComposition類。

1)創建AVMutableComposition類對象
2)建立2個AVURLAsset類對象,首先要根據您的視頻文件,並根據文件,您想從
3中提取音軌第二)創建2 AVMutableCompositionTrack類對象,像一個具有音頻軌道之前,第二個視頻軌道(基於適當的資產從2對象))
4)創建AVAssetExportSession類基於從1組合物的對象)
5)導出會話

致以問候

0

謝謝@peter。這是代碼解決方案。

-(BOOL)compositeVideo{ 
    //Record cur video 
    NSURL *curAudio = [[NSBundle mainBundle]URLForResource:@"a" withExtension:@".pcm"]; 
    NSURL *curVideo = [[NSBundle mainBundle]URLForResource:@"v" withExtension:@".mp4"]; 
    AVAsset *video = [AVAsset assetWithURL:curVideo]; 
    AVAsset *audio = [AVAsset assetWithURL:curAudio]; 
    AVAssetTrack *vTrack = [[video tracksWithMediaType:AVMediaTypeVideo] firstObject]; 
    NSArray *arr = [audio tracksWithMediaType:AVMediaTypeAudio]; 
    AVAssetTrack *aTrack = [arr firstObject]; 

    AVMutableComposition *composition = [AVMutableComposition composition]; 
    AVMutableCompositionTrack *visualTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:1]; 
    AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    NSError *error; 
    [visualTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video.duration) ofTrack:vTrack atTime:kCMTimeZero error:&error]; 
    if (error) { 
     NSLog(@"video composition failed! error:%@", error); 
     return NO; 
    } 
    [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audio.duration) ofTrack:aTrack atTime:kCMTimeZero error:&error]; 
    if (error) { 
     NSLog(@"audio composition failed! error:%@", error); 
     return NO; 
    } 

    AVAssetExportSession *exporter = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetHighestQuality]; 
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; 
    exporter.outputURL = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:@"compositedVideo.mp4"]]; 
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; 
    exporter.outputFileType = AVFileTypeQuickTimeMovie; 
    [exporter exportAsynchronouslyWithCompletionHandler:^{ 
     if (exporter.error) { 
      NSLog(@"exporter synthesization failed! error:%@", error); 
      [self.delegate compositeDidFinishAtURL:nil duration:-1]; 
     }else{ 
      [self.delegate compositeDidFinishAtURL:exporter.outputURL duration:CMTimeGetSeconds(video.duration)]; 
     } 
    }]; 
    return YES; 
} 
相關問題