Выпуск IPhone для автокомпозиции - PullRequest
3 голосов
/ 14 октября 2010

Я пытаюсь создать видео, которое показывает два видео одно за другим, используя avcomposition на iphone. Этот код работает, однако я могу видеть только одно из видео за все время только что созданного видео

- (void) startEdit{

 AVMutableComposition* mixComposition = [AVMutableComposition composition];

 NSString* a_inputFileName = @"export.mov";
 NSString* a_inputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:a_inputFileName];
 NSURL*    a_inputFileUrl = [NSURL fileURLWithPath:a_inputFilePath];

 NSString* b_inputFileName = @"output.mov";
 NSString* b_inputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:b_inputFileName];
 NSURL*    b_inputFileUrl = [NSURL fileURLWithPath:b_inputFilePath];

 NSString* outputFileName = @"outputFile.mov";
 NSString* outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:outputFileName];
 NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

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



 CMTime nextClipStartTime = kCMTimeZero;

 AVURLAsset* a_videoAsset = [[AVURLAsset alloc]initWithURL:a_inputFileUrl options:nil];
 CMTimeRange a_timeRange = CMTimeRangeMake(kCMTimeZero,a_videoAsset.duration);
 AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
 [a_compositionVideoTrack insertTimeRange:a_timeRange ofTrack:[[a_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

 nextClipStartTime = CMTimeAdd(nextClipStartTime, a_timeRange.duration);

 AVURLAsset* b_videoAsset = [[AVURLAsset alloc]initWithURL:b_inputFileUrl options:nil];
 CMTimeRange b_timeRange = CMTimeRangeMake(kCMTimeZero, b_videoAsset.duration);
 AVMutableCompositionTrack *b_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [b_compositionVideoTrack insertTimeRange:b_timeRange ofTrack:[[b_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];



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

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

}


- (void) saveVideoToAlbum:(NSString*)path{
 if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)){
  UISaveVideoAtPathToSavedPhotosAlbum (path, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
 }
}

- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
 NSLog(@"Finished saving video with error: %@", error);
} 

Я опубликовал весь код, так как он может помочь кому-то еще.

В случае, если не

nextClipStartTime = CMTimeAdd(nextClipStartTime, a_timeRange.duration);

[b_compositionVideoTrack insertTimeRange:b_timeRange ofTrack:[[b_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

добавить второе видео в конец первого

Приветствия

Ответы [ 2 ]

3 голосов
/ 14 октября 2010

Разобрался. Должен был быть только один AVMutableCompositionTrack.

Вроде так:

CMTime nextClipStartTime = kCMTimeZero;

    AVURLAsset* a_videoAsset = [[AVURLAsset alloc]initWithURL:a_inputFileUrl options:nil];
    CMTimeRange a_timeRange = CMTimeRangeMake(kCMTimeZero,a_videoAsset.duration);
    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [a_compositionVideoTrack insertTimeRange:a_timeRange ofTrack:[[a_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

    nextClipStartTime = CMTimeAdd(nextClipStartTime, a_timeRange.duration);

    AVURLAsset* b_videoAsset = [[AVURLAsset alloc]initWithURL:b_inputFileUrl options:nil];
    CMTimeRange b_timeRange = CMTimeRangeMake(kCMTimeZero, b_videoAsset.duration);
    [a_compositionVideoTrack insertTimeRange:b_timeRange ofTrack:[[b_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
1 голос
/ 14 октября 2010

Я еще не заметил недостаток, но у меня есть пара предложений:

Во-первых, перехватите ошибку в insertTimeRange и любой другой возможности и осмотрите ее.

Во-вторых,Для простого случая добавления видео вы можете использовать AVMutableComposition без большого количества треков.Используйте «insertTimeRange: ofAsset: atTime: error:» с AVAssets, инициализированными из файлов, и вы значительно упростите свой код.Если вам нужно сделать что-то более сложное, например, перекрестные переходы, вам нужно будет также использовать композицию видео и аудио-микс, и в этот момент вы можете справиться со сложностью треков.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...