如何从我的相机拍摄的电影中访问原始素材,这样我就可以编辑或转换原始素材(例如:将其设为黑/白)。
我知道您可以使用AVAsset加载mov 用不同的AVAsset制作一个合成 然后将其导出到新电影,但我如何访问,以便我可以编辑电影。
答案 0 :(得分:5)
您需要从输入资源中读取视频帧,为每个帧创建CGContextRef以进行绘制,然后将帧写入新的视频文件。基本步骤如下。我遗漏了所有填充代码和错误处理,因此主要步骤更容易阅读。
// AVURLAsset to read input movie (i.e. mov recorded to local storage)
NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:inputURL options:inputOptions];
// Load the input asset tracks information
[inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{
// Check status of "tracks", make sure they were loaded
AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
if (!tracksStatus == AVKeyValueStatusLoaded)
// failed to load
return;
// Fetch length of input video; might be handy
NSTimeInterval videoDuration = CMTimeGetSeconds([inputAsset duration]);
// Fetch dimensions of input video
CGSize videoSize = [inputAsset naturalSize];
/* Prepare output asset writer */
self.assetWriter = [[[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:&error] autorelease];
NSParameterAssert(assetWriter);
assetWriter.shouldOptimizeForNetworkUse = NO;
// Video output
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:videoSize.width], AVVideoWidthKey,
[NSNumber numberWithInt:videoSize.height], AVVideoHeightKey,
nil];
self.assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
NSParameterAssert(assetWriterVideoInput);
NSParameterAssert([assetWriter canAddInput:assetWriterVideoInput]);
[assetWriter addInput:assetWriterVideoInput];
// Start writing
CMTime presentationTime = kCMTimeZero;
[assetWriter startWriting];
[assetWriter startSessionAtSourceTime:presentationTime];
/* Read video samples from input asset video track */
self.reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];
NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
[outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
self.readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
outputSettings:outputSettings];
// Assign the tracks to the reader and start to read
[reader addOutput:readerVideoTrackOutput];
if ([reader startReading] == NO) {
// Handle error
}
dispatch_queue_t dispatch_queue = dispatch_get_main_queue();
[assetWriterVideoInput requestMediaDataWhenReadyOnQueue:dispatch_queue usingBlock:^{
CMTime presentationTime = kCMTimeZero;
while ([assetWriterVideoInput isReadyForMoreMediaData]) {
CMSampleBufferRef sample = [readerVideoTrackOutput copyNextSampleBuffer];
if (sample) {
presentationTime = CMSampleBufferGetPresentationTimeStamp(sample);
/* Composite over video frame */
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sample);
// Lock the image buffer
CVPixelBufferLockBaseAddress(imageBuffer,0);
// Get information about the image
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a CGImageRef from the CVImageBufferRef
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
/*** Draw into context ref to draw over video frame ***/
// We unlock the image buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// We release some components
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
/* End composite */
[assetWriterVideoInput appendSampleBuffer:sample];
CFRelease(sample);
}
else {
[assetWriterVideoInput markAsFinished];
/* Close output */
[assetWriter endSessionAtSourceTime:presentationTime];
if (![assetWriter finishWriting]) {
NSLog(@"[assetWriter finishWriting] failed, status=%@ error=%@", assetWriter.status, assetWriter.error);
}
}
}
}];
}];
答案 1 :(得分:0)
我不知道整个过程,但我知道一些:
您可能需要使用AV Foundation Framework和Core Video Framework来处理单个帧。您可能会使用AVWriter:
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
[NSURL fileURLWithPath:path]
fileType:AVFileTypeQuickTimeMovie
error:&error];
您可以使用AVFoundation或CV维护像素缓冲区,然后将其写为(此示例适用于CV):
[pixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
要获得框架,AVAssetStillImageGenerator
还不够。
或者,可能有一个过滤器或指令可以与AVVideoMutableComposition,AVMutableComposition或AVAssetExportSession一起使用。
如果您在8月份提出要求后取得了进展,请发布我感兴趣的内容!