我无法进行全尺寸实时视频编码。但是,作为替代方案,请考虑这一点。
记录发生的动作(及其时间戳),而不是记录帧。然后,当您想回放时,只需回放动作即可。您已经拥有代码,因为您在“现实生活”中执行它。
您所做的就是在时间上相对于彼此重播这些相同的动作。
编辑
如果你想尝试录制,这就是我所做的(注意,我放弃了它......这是一个正在进行中的实验,所以只是把它作为我如何接近它的一个例子......没有什么是生产 -准备好)。我能够以 640x360 录制实时音频/视频,但该分辨率对我来说太低了。它在 iPad 上看起来不错,但当我将视频移到我的 Mac 上并在那里观看时就很糟糕了。
我在使用更高分辨率时遇到了问题。我改编了 RosyWriter 示例项目中的大部分代码。以下是设置资产写入器、开始录制以及将 UIImage 添加到视频流的主要例程。
祝你好运。
CGSize const VIDEO_SIZE = { 640, 360 };
- (void) startRecording
{
dispatch_async(movieWritingQueue, ^{
NSLog(@"startRecording called in state 0x%04x", state);
if (state != STATE_IDLE) return;
state = STATE_STARTING_RECORDING;
NSLog(@"startRecording changed state to 0x%04x", state);
NSError *error;
//assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType:AVFileTypeQuickTimeMovie error:&error];
assetWriter = [[AVAssetWriter alloc] initWithURL:movieURL fileType:AVFileTypeMPEG4 error:&error];
if (error) {
[self showError:error];
}
[self removeFile:movieURL];
[self resumeCaptureSession];
[self.delegate recordingWillStart];
});
}
// TODO: this is where we write an image into the movie stream...
- (void) writeImage:(UIImage*)inImage
{
static CFTimeInterval const minInterval = 1.0 / 10.0;
static CFAbsoluteTime lastFrameWrittenWallClockTime;
CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent();
CFTimeInterval timeBetweenFrames = thisFrameWallClockTime - lastFrameWrittenWallClockTime;
if (timeBetweenFrames < minInterval) return;
// Not really accurate, but we just want to limit the rate we try to write frames...
lastFrameWrittenWallClockTime = thisFrameWallClockTime;
dispatch_async(movieWritingQueue, ^{
if ( !assetWriter ) return;
if ((state & STATE_STARTING_RECORDING) && !(state & STATE_MASK_VIDEO_READY)) {
if ([self setupAssetWriterImageInput:inImage]) {
[self videoIsReady];
}
}
if (state != STATE_RECORDING) return;
if (assetWriter.status != AVAssetWriterStatusWriting) return;
CGImageRef cgImage = CGImageCreateCopy([inImage CGImage]);
if (assetWriterVideoIn.readyForMoreMediaData) {
CVPixelBufferRef pixelBuffer = NULL;
// Resize the original image...
if (!CGSizeEqualToSize(inImage.size, VIDEO_SIZE)) {
// Build a context that's the same dimensions as the new size
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, VIDEO_SIZE.width, VIDEO_SIZE.height));
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(cgImage),
0,
CGImageGetColorSpace(cgImage),
CGImageGetBitmapInfo(cgImage));
// Rotate and/or flip the image if required by its orientation
//CGContextConcatCTM(bitmap, transform);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
// Draw into the context; this scales the image
//CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
CGContextDrawImage(bitmap, newRect, cgImage);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
CGContextRelease(bitmap);
CGImageRelease(cgImage);
cgImage = newImageRef;
}
CFDataRef image = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
int status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, self.assetWriterPixelBufferAdaptor.pixelBufferPool, &pixelBuffer);
if(status != 0){
//could not get a buffer from the pool
NSLog(@"Error creating pixel buffer: status=%d", status);
}
// set image data into pixel buffer
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
uint8_t* destPixels = CVPixelBufferGetBaseAddress(pixelBuffer);
// Danger, Will Robinson!!!!! USE_BLOCK_IN_FRAME warning...
CFDataGetBytes(image, CFRangeMake(0, CFDataGetLength(image)), destPixels);
if(status == 0){
//CFAbsoluteTime thisFrameWallClockTime = CFAbsoluteTimeGetCurrent();
CFTimeInterval elapsedTime = thisFrameWallClockTime - firstFrameWallClockTime;
CMTime presentationTime = CMTimeAdd(firstBufferTimeStamp, CMTimeMake(elapsedTime * TIME_SCALE, TIME_SCALE));
BOOL success = [self.assetWriterPixelBufferAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:presentationTime];
if (!success)
NSLog(@"Warning: Unable to write buffer to video");
}
//clean up
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
CVPixelBufferRelease( pixelBuffer );
CFRelease(image);
CGImageRelease(cgImage);
} else {
NSLog(@"Not ready for video data");
}
});
}
-(BOOL) setupAssetWriterImageInput:(UIImage*)image
{
NSDictionary* videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:1024.0*1024.0], AVVideoAverageBitRateKey,
nil ];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
//[NSNumber numberWithInt:image.size.width], AVVideoWidthKey,
//[NSNumber numberWithInt:image.size.height], AVVideoHeightKey,
[NSNumber numberWithInt:VIDEO_SIZE.width], AVVideoWidthKey,
[NSNumber numberWithInt:VIDEO_SIZE.height], AVVideoHeightKey,
videoCompressionProps, AVVideoCompressionPropertiesKey,
nil];
NSLog(@"videoSettings: %@", videoSettings);
assetWriterVideoIn = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSParameterAssert(assetWriterVideoIn);
assetWriterVideoIn.expectsMediaDataInRealTime = YES;
NSDictionary* bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];
self.assetWriterPixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:assetWriterVideoIn sourcePixelBufferAttributes:bufferAttributes];
//add input
if ([assetWriter canAddInput:assetWriterVideoIn]) {
[assetWriter addInput:assetWriterVideoIn];
}
else {
NSLog(@"Couldn't add asset writer video input.");
return NO;
}
return YES;
}