Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)videoFromImage:(UIImage *)image
- {
- NSError *error;
- self.videoWriter = [[AVAssetWriter alloc] initWithURL:
- [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"]] fileType:AVFileTypeQuickTimeMovie
- error:&error];
- if (!error) {
- NSParameterAssert(self.videoWriter);
- NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
- AVVideoCodecH264, AVVideoCodecKey,
- [NSNumber numberWithInt:image.size.width], AVVideoWidthKey,
- [NSNumber numberWithInt:image.size.height], AVVideoHeightKey,
- nil];
- AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
- assetWriterInputWithMediaType:AVMediaTypeVideo
- outputSettings:videoSettings];
- AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
- assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
- sourcePixelBufferAttributes:nil];
- NSParameterAssert(videoWriterInput);
- NSParameterAssert([self.videoWriter canAddInput:videoWriterInput]);
- [self.videoWriter addInput:videoWriterInput];
- [self.videoWriter startWriting];
- [self.videoWriter startSessionAtSourceTime:kCMTimeZero];
- if (adaptor.assetWriterInput.readyForMoreMediaData) {
- CVPixelBufferRef buffer = [self pixelBufferFromImage:image];
- [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
- }
- [videoWriterInput markAsFinished];
- [self.videoWriter finishWritingWithCompletionHandler:^{
- NSLog(@"finished"); // Never gets called
- }];
- }
- else {
- NSLog(@"%@", error.localizedDescription);
- }
- }
- NSFileManager *manager = [[NSFileManager alloc] init];
- if ([manager fileExistsAtPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"]]) {
- NSError *fileError;
- [manager removeItemAtPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"] error:&fileError];
- if (fileError) NSLog(@"%@", fileError.localizedDescription);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement