GPUImageMovieComposition.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // GPUImageMovieComposition.m
  3. // Givit
  4. //
  5. // Created by Sean Meiners on 2013/01/25.
  6. //
  7. //
  8. #import "GPUImageMovieComposition.h"
  9. #import "GPUImageMovieWriter.h"
  10. @implementation GPUImageMovieComposition
  11. @synthesize compositon = _compositon;
  12. @synthesize videoComposition = _videoComposition;
  13. @synthesize audioMix = _audioMix;
  14. - (id)initWithComposition:(AVComposition*)compositon
  15. andVideoComposition:(AVVideoComposition*)videoComposition
  16. andAudioMix:(AVAudioMix*)audioMix {
  17. if (!(self = [super init]))
  18. {
  19. return nil;
  20. }
  21. [self yuvConversionSetup];
  22. self.compositon = compositon;
  23. self.videoComposition = videoComposition;
  24. self.audioMix = audioMix;
  25. return self;
  26. }
  27. - (AVAssetReader*)createAssetReader
  28. {
  29. //NSLog(@"creating reader from composition: %@, video: %@, audio: %@ with duration: %@", _compositon, _videoComposition, _audioMix, CFBridgingRelease(CMTimeCopyDescription(kCFAllocatorDefault, _compositon.duration)));
  30. NSError *error = nil;
  31. AVAssetReader *assetReader = [AVAssetReader assetReaderWithAsset:self.compositon error:&error];
  32. NSDictionary *outputSettings = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)};
  33. AVAssetReaderVideoCompositionOutput *readerVideoOutput = [AVAssetReaderVideoCompositionOutput assetReaderVideoCompositionOutputWithVideoTracks:[_compositon tracksWithMediaType:AVMediaTypeVideo]
  34. videoSettings:outputSettings];
  35. #if ! TARGET_IPHONE_SIMULATOR
  36. if( [_videoComposition isKindOfClass:[AVMutableVideoComposition class]] )
  37. [(AVMutableVideoComposition*)_videoComposition setRenderScale:1.0];
  38. #endif
  39. readerVideoOutput.videoComposition = self.videoComposition;
  40. readerVideoOutput.alwaysCopiesSampleData = NO;
  41. [assetReader addOutput:readerVideoOutput];
  42. NSArray *audioTracks = [_compositon tracksWithMediaType:AVMediaTypeAudio];
  43. BOOL shouldRecordAudioTrack = (([audioTracks count] > 0) && (self.audioEncodingTarget != nil) );
  44. AVAssetReaderAudioMixOutput *readerAudioOutput = nil;
  45. if (shouldRecordAudioTrack)
  46. {
  47. [self.audioEncodingTarget setShouldInvalidateAudioSampleWhenDone:YES];
  48. readerAudioOutput = [AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks:audioTracks audioSettings:nil];
  49. readerAudioOutput.audioMix = self.audioMix;
  50. readerAudioOutput.alwaysCopiesSampleData = NO;
  51. [assetReader addOutput:readerAudioOutput];
  52. }
  53. return assetReader;
  54. }
  55. @end