iPhone 合成问题

标签 iphone ios avfoundation

我正在尝试创建一个视频,在 iphone 上使用 avcomposition 一个接一个地显示两个视频。此代码有效,但是在新创建的视频的整个持续时间内我只能看到其中一个视频

- (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];

将第二个视频添加到第一个视频的末尾

干杯

最佳答案

想通了。应该只有一个 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];

关于iPhone 合成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3936671/

相关文章:

iphone - UITableView滚动性能悖论: Make subviews or use -drawRect:?

ios - 在 iOS 上检索多个电话联系人标签

javascript - 如何在正在播放但由于切换到其他应用而暂停的iOS Safari中播放视频?

ios - Uitableview 与 UICollectionviewcell 和 UITableviewcell

iphone - 如何使用 didFinishPickingMediaWithInfo 从视频中查找 "Selected frames"的开始和结束持续时间?

iphone - 如何使用 AVAssetExportSession 在 AVMutableComposition 之上正确导出 CALayer

ios - Alamofire 快速接收并解析字符串数组

iphone - 支持旧的 iOS 版本和设备

iOS显示更多来自xib内部 View 的 View

iOS AVPlayer 不会播放有声读物,但 MPPLayerController 可以