我如何访问用我的相机拍摄的电影中的原始素材,以便我可以编辑或转换原始素材(例如:使其成为黑色/白色)。
我知道您可以使用 AVAsset 加载 mov 使用不同的 AVAsset 进行合成,然后将其导出到新电影,但是我如何访问以便我可以编辑电影。
我如何访问用我的相机拍摄的电影中的原始素材,以便我可以编辑或转换原始素材(例如:使其成为黑色/白色)。
我知道您可以使用 AVAsset 加载 mov 使用不同的 AVAsset 进行合成,然后将其导出到新电影,但是我如何访问以便我可以编辑电影。
您需要从输入资源中读取视频帧,为每个帧创建一个 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);
}
}
}
}];
}];
我不知道整个过程,但我知道一些:
您可能需要使用 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 月提出要求以来取得了进展,请在我感兴趣的情况下发布!