2011-05-06 127 views
1

我想合併Audion CAF文件和視頻/圖像UIImage以創建電影文件(以.mov格式)。合併音頻和視頻/圖像以創建電影文件

說我的音頻長30秒,我有一個UIImage;我想創建一個.mov文件,以便在整個音頻播放過程中顯示UIImage

我發現這個參考: How to add audio to video file on iphone SDK

誰能告訴我,那是在我的情況有幫助的,因爲我的音頻和圖像的長度/視頻有什麼不同?

在此先感謝。

回答

0

Quicktime Pro可以做到這一點。做我的應用程序。 您可以從圖像中創建電影。 Quicktime提供了讀取一系列圖像並從中創建電影的功能。他在進口時也要求FPS。

音軌可以簡單地合併,粘貼到某處或縮放到電影的選定範圍。

+0

哪個類/框架將用於什麼?如果您有任何問題,請提供一些教程或參考鏈接。謝謝。 – 2011-05-06 10:05:52

+0

Quicktime安裝在每臺Mac上。 Quicktime Pro是Quicktime的商業升級版本。你可以在蘋果網上商店購買鑰匙。我認爲可以使用quicktime框架手動進行所有編輯,但我沒有任何示例。由於Quicktime是Apples技術的一部分,因此您將在其開發者網站上提供示例。 – 2011-05-06 11:26:50

1

是的,你應該使用AVMutableComposition。要從UIImage創建視頻軌道,請使用AVAssetWriter。

+1

可以提供示例演示應用程序在這...教程也足夠了..請幫助我在這.. – 2013-02-13 10:56:27

0

使用這個,我發現這個地方的淨,我不記得了,....

 
NSString *fileNamePath = @"audio.caf"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath]; 
    NSURL *audioUrl = [NSURL fileURLWithPath:oldappSettingsPath]; 
    NSString *fileNamePath1 = @"output.mp4"; 
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; 
    NSString *oldappSettingsPath1 = [documentsDirectory1 stringByAppendingPathComponent:fileNamePath1]; 
    NSLog(@"oldpath=%@",oldappSettingsPath); 
    NSURL *videoUrl = [NSURL fileURLWithPath:oldappSettingsPath1]; 
    if (avPlayer.duration >0.00000) 
    { 
     NSLog(@"SOMEDATA  IS THERE "); 
     AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil]; 
     AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil]; 

     AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

     NSLog(@"audio =%@",audioAsset); 
     AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
     [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

     AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
     [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 


     AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough]; 

     NSString* videoName = @"export.mov"; 

     NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName]; 
     NSURL *exportUrl = [NSURL fileURLWithPath:exportPath]; 

     if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
     { 
      [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
     } 

     _assetExport.outputFileType = @"com.apple.quicktime-movie"; 
     NSLog(@"file type %@",_assetExport.outputFileType); 
     _assetExport.outputURL = exportUrl; 
     _assetExport.shouldOptimizeForNetworkUse = YES; 



     [_assetExport exportAsynchronouslyWithCompletionHandler: 
     ^(void) 
     {  

      NSString *fileNamePath = @"sound_record.mov"; 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath]; 


      //    if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) { 
//     
//     NSFileManager *fileManager = [NSFileManager defaultManager]; 
//     [fileManager removeItemAtPath: oldappSettingsPath error:NULL]; 
//     
//    } 
      NSURL *documentDirectoryURL = [NSURL fileURLWithPath:oldappSettingsPath]; 
      [[NSFileManager defaultManager] copyItemAtURL:exportUrl toURL:documentDirectoryURL error:nil]; 
      [audioAsset release]; 
      [videoAsset release]; 
      [_assetExport release]; 
     }  
     ];