[英]Creating `CMSampleBufferRef` from a .mov file
My app captures a video clip for 3 seconds, and programmatically I want to create a 15 sec clip from recorded 3 sec clip by looping it 5 times. 我的应用程序捕获视频剪辑3秒,并以编程方式我想通过循环5次来记录3秒剪辑创建15秒剪辑。 And finally have to save 15 sec clip in CameraRoll. 最后必须在CameraRoll中保存15秒的剪辑。
I have got my 3 sec video clip via AVCaptureMovieFileOutput
and I have its NSURL
from delegate which is currently in NSTemporaryDirectory()
. 我通过AVCaptureMovieFileOutput
获得了我的3秒视频剪辑,我有来自委托的NSURL
,目前在NSTemporaryDirectory()
。
I am using AVAssetWriterInput
for looping it. 我正在使用AVAssetWriterInput
来循环它。 But it ask for CMSampleBufferRef
like : 但它要求CMSampleBufferRef
像:
[writerInput appendSampleBuffer:sampleBuffer];
How will I gee this CMSampleBufferRef
from video in NSTemporaryDirectory() ? 我如何从NSTemporaryDirectory()中的视频中获取此CMSampleBufferRef
?
I have seen code for converting UIImage
to CMSampleBufferRef
, but I can find any for video file. 我见过将UIImage
转换为CMSampleBufferRef
代码,但我可以找到任何视频文件。
Any suggestion will be helpful. 任何建议都会有所帮助。 :) :)
Finally, I fixed my problem using AVMutableComposition
. 最后,我使用AVMutableComposition
修复了我的问题。 Here is my code : 这是我的代码:
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");
}
}];
Take a look at AVAssetReader, this can return an CMSampleBufferRef. 看看AVAssetReader,它可以返回一个CMSampleBufferRef。 Keep in mind, you'll need to manipulate the timestamp for your approach to work. 请记住,您需要操纵工作方法的时间戳。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.