2012-05-10 2 views
0

이미지 및 오디오 파일 배열을 사용하여 동영상을 만들고 해당 영화를 시뮬레이터의 사진 라이브러리에 저장하고 있습니다. 이미지 및 오디오 파일 배열을 사용하여 올바른 동영상을 얻지 만 while 장치에서 실행 나는 대신 내 결과 파일 (혼합 오디오 및 이미지 파일 배열)을 작성하려면 내가 추가 한 (OutPut.mov 내 코드에서 사용한) 비디오를 얻고있다. 여기 PhotoLibrary iPad에서 결과 동영상을 가져 오지 못함

는 사진 lib 디렉토리에 파일을 내 혼합 오디오 및 이미지의 배열을 저장하는 내 코드입니다 .. 당신이 좋아하는 photolibrary 경로로 동영상 파일을 작성해야

AVMutableComposition* mixComposition = [AVMutableComposition composition]; 
NSString* audio_inputFilePath = [[NSBundle mainBundle] pathForResource:@"Ruler" ofType:@"caf"]; 
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 


NSString *video_inputFilePath = [documentsDirectory stringByAppendingPathComponent:@"videoOutput1234videoOutput1234.mp4"]; 
NSLog(@"output url %@",video_inputFilePath); 


NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath]; 


NSString* outputFilePath = [[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"mov"]; 
NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil]; 


if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil]; 

CMTime nextClipStartTime = kCMTimeZero; 

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil]; 
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration); 
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 



AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil]; 
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration); 
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality]; 
_assetExport.outputFileType = @"com.apple.quicktime-movie"; 
_assetExport.outputURL = outputFileUrl; 

[_assetExport exportAsynchronouslyWithCompletionHandler:^(void) {[self saveVideoToAlbum:outputFilePath]; }  ]; 

답변

5

- (void)writeVideoToPhotoLibrary:(NSURL *)url 
{ 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    [library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){ 
     if (error) { 
      NSLog(@"Video could not be saved"); 
     } 
    }]; 
    [library release]; 
} 

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 
    // Let's see what happened 
    NSLog(@"finished"); 
    NSLog(@":error : %@", error); 
} 

관련 문제