Skip to content

Instantly share code, notes, and snippets.

@jontelang
Created April 4, 2021 14:26
Show Gist options
  • Save jontelang/c0351337bd496a6c7e0c94293adf881f to your computer and use it in GitHub Desktop.
Save jontelang/c0351337bd496a6c7e0c94293adf881f to your computer and use it in GitHub Desktop.
#import "VideoMaker.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
@implementation VideoMaker
-(void)makeVideo {
//
// Setup vars
//
dispatch_queue_attr_t qos = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1);;
dispatch_queue_t recordingQueue = dispatch_queue_create("recordingQueue", qos);
NSError *error = nil;
NSArray *f = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *u = [f firstObject];
NSString *s = [[u absoluteString] stringByReplacingOccurrencesOfString:@"file://" withString:@"file://"];
NSString *name = [NSString stringWithFormat:@"%i.mov", (int)[[NSDate date] timeIntervalSince1970]];
NSString *p = [s stringByAppendingString:name];
NSLog(@"path: %@", p);
NSURL *outputURL = [NSURL URLWithString:p];
//
// Start process
//
AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:&error];
NSDictionary *writerInputParams = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecTypeH264, AVVideoCodecKey,
@(828), AVVideoWidthKey,
@(1792), AVVideoHeightKey,
AVVideoScalingModeResizeAspectFill, AVVideoScalingModeKey, nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:writerInputParams];
[assetWriter addInput:assetWriterInput];
NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey,
@(5), kCVPixelBufferPoolMinimumBufferCountKey,
@(5), kCVPixelBufferPoolAllocationThresholdKey,
@(1), kCVPixelBufferPoolMaximumBufferAgeKey,nil];
AVAssetWriterInputPixelBufferAdaptor *writerAdaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:assetWriterInput
sourcePixelBufferAttributes:bufferAttributes];
[assetWriter startWriting];
[assetWriter startSessionAtSourceTime:kCMTimeZero];
[assetWriterInput requestMediaDataWhenReadyOnQueue:recordingQueue usingBlock:^{
for (int i = 1; i < 200; ++i) {
while (![assetWriterInput isReadyForMoreMediaData]) {
[NSThread sleepForTimeInterval:0.01];
}
NSString *path = [NSString stringWithFormat:@"/Users/jontelang/Desktop/SnapperVideoDump/frames/frame_%i.jpg", i];
UIImage *image = [[UIImage imageWithContentsOfFile:path] retain];
CGImageRef ref = [image CGImage];
CGImageRetain(ref);
CVPixelBufferRef buffer = [self pixelBufferFromCGImage:ref pool:writerAdaptor.pixelBufferPool];
CGImageRelease(ref);
[image release];
CMTime presentTime = CMTimeAdd(CMTimeMake(i, 60), CMTimeMake(1, 60));
[writerAdaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
CVPixelBufferRelease(buffer);
}
[assetWriterInput markAsFinished];
[assetWriter finishWritingWithCompletionHandler:^{}];
}];
}
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image pool:(CVPixelBufferPoolRef)pool {
CVPixelBufferRef pxbuffer = NULL;
CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pxbuffer);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata,
CGImageGetWidth(image),
CGImageGetHeight(image),
8,
CVPixelBufferGetBytesPerRow(pxbuffer),
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment