1

我的应用程序捕获了一个 3 秒的视频剪辑,并且以编程方式我想通过循环 5 次从录制的 3 秒剪辑中创建一个 15 秒的剪辑。最后必须在 CameraRoll 中保存 15 秒的剪辑。

我有我的 3 秒视频剪辑AVCaptureMovieFileOutput,我有NSURL来自代表的,目前在NSTemporaryDirectory().

我正在使用AVAssetWriterInputfor 循环它。但它要求CMSampleBufferRef像:

[writerInput appendSampleBuffer:sampleBuffer];

我将如何CMSampleBufferRef从 NSTemporaryDirectory() 中的视频中看到这一点?

我已经看到了转换UIImage为的代码CMSampleBufferRef,但我可以找到任何用于视频文件的代码。

任何建议都会有所帮助。:)

4

2 回答 2

2

最后,我使用AVMutableComposition. 这是我的代码:

AVMutableComposition *mixComposition = [AVMutableComposition new];
AVMutableCompositionTrack *mutableCompVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:3SecFileURL options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero, [videoAsset duration]);

CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(M_PI_2);
[mutableCompVideoTrack setPreferredTransform:rotationTransform];

CMTime currentCMTime = kCMTimeZero;

for (NSInteger count = 0 ; count < 5 ; count++)
{
    [mutableCompVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:currentCMTime error:nil];
    currentCMTime = CMTimeAdd(currentCMTime, [videoAsset duration]);
}

NSString *fullMoviePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"moviefull" stringByAppendingPathExtension:@"mov"]];
NSURL *finalVideoFileURL = [NSURL fileURLWithPath:fullMoviePath];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
[exportSession setOutputFileType:AVFileTypeQuickTimeMovie];
[exportSession setOutputURL:finalVideoFileURL];

CMTimeValue val = [mixComposition duration].value;
CMTime start = CMTimeMake(0, 1);
CMTime duration = CMTimeMake(val, 1);
CMTimeRange range = CMTimeRangeMake(start, duration);
[exportSession setTimeRange:range];

[exportSession exportAsynchronouslyWithCompletionHandler:^{

    switch ([exportSession status])
    {
        case AVAssetExportSessionStatusFailed:
        {
            NSLog(@"Export failed: %@ %@", [[exportSession error] localizedDescription], [[exportSession error]debugDescription]);
        }
        case AVAssetExportSessionStatusCancelled:
        {
            NSLog(@"Export canceled");
            break;
        }
        case AVAssetExportSessionStatusCompleted:
        {
            NSLog(@"Export complete!");
        }

        default:    NSLog(@"default");
    }
}];
于 2015-07-13T05:51:39.070 回答
1

看看 AVAssetReader,这可以返回一个 CMSampleBufferRef。请记住,您需要为您的工作方法操作时间戳。

于 2015-07-12T15:05:04.300 回答