-
-
Save jontelang/8f01b895321d761cbb8cda9d7a5be3bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]; | |
AVAssetWriterInputPixelBufferAdaptor *writerAdaptor = [AVAssetWriterInputPixelBufferAdaptor | |
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:assetWriterInput | |
sourcePixelBufferAttributes:nil]; | |
[assetWriter startWriting]; | |
[assetWriter startSessionAtSourceTime:kCMTimeZero]; | |
[assetWriterInput requestMediaDataWhenReadyOnQueue:recordingQueue usingBlock:^{ | |
for (int i = 1; i < 300; ++i) { | |
while (![assetWriterInput isReadyForMoreMediaData]) { | |
[NSThread sleepForTimeInterval:0.01]; | |
} | |
NSString *path = [NSString stringWithFormat:@"/Users/jontelang/Desktop/frames/frame_%i.jpg", i]; | |
UIImage *image = [[UIImage imageWithContentsOfFile:path] retain]; | |
CGImageRef ref = [image CGImage]; | |
CGImageRetain(ref); | |
CVPixelBufferRef buffer = [self pixelBufferFromCGImage:ref]; | |
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 { | |
CVPixelBufferRef pxbuffer = NULL; | |
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: | |
@YES, kCVPixelBufferCGBitmapContextCompatibilityKey, | |
@YES, kCVPixelBufferCGImageCompatibilityKey, nil]; | |
CVPixelBufferCreate(kCFAllocatorDefault, | |
CGImageGetWidth(image), | |
CGImageGetHeight(image), | |
kCVPixelFormatType_32ARGB, | |
(__bridge CFDictionaryRef _Nullable)(attributes), | |
&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