Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. - (void)videoFromImage:(UIImage *)image
  2. {
  3. NSError *error;
  4. self.videoWriter = [[AVAssetWriter alloc] initWithURL:
  5. [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"]] fileType:AVFileTypeQuickTimeMovie
  6. error:&error];
  7. if (!error) {
  8. NSParameterAssert(self.videoWriter);
  9.  
  10. NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
  11. AVVideoCodecH264, AVVideoCodecKey,
  12. [NSNumber numberWithInt:image.size.width], AVVideoWidthKey,
  13. [NSNumber numberWithInt:image.size.height], AVVideoHeightKey,
  14. nil];
  15.  
  16. AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
  17. assetWriterInputWithMediaType:AVMediaTypeVideo
  18. outputSettings:videoSettings];
  19.  
  20.  
  21. AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
  22. assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
  23. sourcePixelBufferAttributes:nil];
  24.  
  25. NSParameterAssert(videoWriterInput);
  26. NSParameterAssert([self.videoWriter canAddInput:videoWriterInput]);
  27.  
  28. [self.videoWriter addInput:videoWriterInput];
  29. [self.videoWriter startWriting];
  30. [self.videoWriter startSessionAtSourceTime:kCMTimeZero];
  31.  
  32. if (adaptor.assetWriterInput.readyForMoreMediaData) {
  33. CVPixelBufferRef buffer = [self pixelBufferFromImage:image];
  34. [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
  35. }
  36.  
  37. [videoWriterInput markAsFinished];
  38.  
  39. [self.videoWriter finishWritingWithCompletionHandler:^{
  40. NSLog(@"finished"); // Never gets called
  41. }];
  42. }
  43. else {
  44. NSLog(@"%@", error.localizedDescription);
  45. }
  46. }
  47.  
  48. NSFileManager *manager = [[NSFileManager alloc] init];
  49. if ([manager fileExistsAtPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"]]) {
  50. NSError *fileError;
  51. [manager removeItemAtPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"output.mp4"] error:&fileError];
  52. if (fileError) NSLog(@"%@", fileError.localizedDescription);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement