从.mov文件创建`CMSampleBufferRef`

时间:2015-07-12 09:23:52

标签: ios ios8 video-capture cmsamplebufferref

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

我已经通过AVCaptureMovieFileOutput获得了我的3秒视频片段,我的代理人NSURL目前位于NSTemporaryDirectory()

我正在使用AVAssetWriterInput进行循环播放。但它要求CMSampleBufferRef喜欢:

[writerInput appendSampleBuffer:sampleBuffer];

我如何从NSTemporaryDirectory()中的视频中获取此CMSampleBufferRef

我见过将UIImage转换为CMSampleBufferRef的代码,但我可以找到任何视频文件。

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

2 个答案:

答案 0 :(得分: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");
    }
}];

答案 1 :(得分:1)

看一下AVAssetReader,它可以返回一个CMSampleBufferRef。请记住,您需要操纵时间戳以便您的工作方式。