@@ -48,7 +48,7 @@ - (id)initWithAsset:(AVAsset *)asset
4848 _asset = asset;
4949 _timeRange = CMTimeRangeMake (kCMTimeZero , kCMTimePositiveInfinity );
5050 }
51-
51+
5252 return self;
5353}
5454
@@ -57,7 +57,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
5757 NSParameterAssert (handler != nil );
5858 [self cancelExport ];
5959 self.completionHandler = handler;
60-
60+
6161 if (!self.outputURL )
6262 {
6363 _error = [NSError errorWithDomain: AVFoundationErrorDomain code: AVErrorExportFailed userInfo: @
@@ -67,7 +67,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
6767 handler ();
6868 return ;
6969 }
70-
70+
7171 NSError *readerError;
7272 self.reader = [AVAssetReader.alloc initWithAsset: self .asset error: &readerError];
7373 if (readerError)
@@ -76,7 +76,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
7676 handler ();
7777 return ;
7878 }
79-
79+
8080 NSError *writerError;
8181 self.writer = [AVAssetWriter assetWriterWithURL: self .outputURL fileType: self .outputFileType error: &writerError];
8282 if (writerError)
@@ -85,14 +85,14 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
8585 handler ();
8686 return ;
8787 }
88-
88+
8989 self.reader .timeRange = self.timeRange ;
9090 self.writer .shouldOptimizeForNetworkUse = self.shouldOptimizeForNetworkUse ;
9191 self.writer .metadata = self.metadata ;
92-
92+
9393 NSArray *videoTracks = [self .asset tracksWithMediaType: AVMediaTypeVideo];
94-
95-
94+
95+
9696 if (CMTIME_IS_VALID (self.timeRange .duration ) && !CMTIME_IS_POSITIVE_INFINITY (self.timeRange .duration ))
9797 {
9898 duration = CMTimeGetSeconds (self.timeRange .duration );
@@ -119,7 +119,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
119119 {
120120 [self .reader addOutput: self .videoOutput];
121121 }
122-
122+
123123 //
124124 // Video input
125125 //
@@ -144,12 +144,12 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
144144 };
145145 self.videoPixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput: self .videoInput sourcePixelBufferAttributes: pixelBufferAttributes];
146146 }
147-
147+
148148 //
149149 // Audio output
150150 //
151151 NSArray *audioTracks = [self .asset tracksWithMediaType: AVMediaTypeAudio];
152- if (audioTracks.count > 0 ) {
152+ if (self. audioSettings && audioTracks.count > 0 ) {
153153 self.audioOutput = [AVAssetReaderAudioMixOutput assetReaderAudioMixOutputWithAudioTracks: audioTracks audioSettings: nil ];
154154 self.audioOutput .alwaysCopiesSampleData = NO ;
155155 self.audioOutput .audioMix = self.audioMix ;
@@ -161,7 +161,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
161161 // Just in case this gets reused
162162 self.audioOutput = nil ;
163163 }
164-
164+
165165 //
166166 // Audio input
167167 //
@@ -173,11 +173,11 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
173173 [self .writer addInput: self .audioInput];
174174 }
175175 }
176-
176+
177177 [self .writer startWriting ];
178178 [self .reader startReading ];
179179 [self .writer startSessionAtSourceTime: self .timeRange.start];
180-
180+
181181 __block BOOL videoCompleted = NO ;
182182 __block BOOL audioCompleted = NO ;
183183 __weak typeof (self) wself = self;
@@ -201,7 +201,7 @@ - (void)exportAsynchronouslyWithCompletionHandler:(void (^)())handler
201201 else {
202202 videoCompleted = YES ;
203203 }
204-
204+
205205 if (!self.audioOutput ) {
206206 audioCompleted = YES ;
207207 } else {
@@ -231,20 +231,20 @@ - (BOOL)encodeReadySamplesFromOutput:(AVAssetReaderOutput *)output toInput:(AVAs
231231 {
232232 BOOL handled = NO ;
233233 BOOL error = NO ;
234-
234+
235235 if (self.reader .status != AVAssetReaderStatusReading || self.writer .status != AVAssetWriterStatusWriting)
236236 {
237237 handled = YES ;
238238 error = YES ;
239239 }
240-
240+
241241 if (!handled && self.videoOutput == output)
242242 {
243243 // update the video progress
244244 lastSamplePresentationTime = CMSampleBufferGetPresentationTimeStamp (sampleBuffer);
245245 lastSamplePresentationTime = CMTimeSubtract (lastSamplePresentationTime, self.timeRange .start );
246246 self.progress = duration == 0 ? 1 : CMTimeGetSeconds (lastSamplePresentationTime) / duration;
247-
247+
248248 if ([self .delegate respondsToSelector: @selector (exportSession:renderFrame:withPresentationTime:toBuffer: )])
249249 {
250250 CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer (sampleBuffer);
@@ -264,7 +264,7 @@ - (BOOL)encodeReadySamplesFromOutput:(AVAssetReaderOutput *)output toInput:(AVAs
264264 error = YES ;
265265 }
266266 CFRelease (sampleBuffer);
267-
267+
268268 if (error)
269269 {
270270 return NO ;
@@ -276,15 +276,15 @@ - (BOOL)encodeReadySamplesFromOutput:(AVAssetReaderOutput *)output toInput:(AVAs
276276 return NO ;
277277 }
278278 }
279-
279+
280280 return YES ;
281281}
282282
283283- (AVMutableVideoComposition *)buildDefaultVideoComposition
284284{
285285 AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition ];
286286 AVAssetTrack *videoTrack = [[self .asset tracksWithMediaType: AVMediaTypeVideo] objectAtIndex: 0 ];
287-
287+
288288 // get the frame rate from videoSettings, if not set then try to get it from the video track,
289289 // if not set (mainly when asset is AVComposition) then use the default frame rate of 30
290290 float trackFrameRate = 0 ;
@@ -304,26 +304,26 @@ - (AVMutableVideoComposition *)buildDefaultVideoComposition
304304 {
305305 trackFrameRate = [videoTrack nominalFrameRate ];
306306 }
307-
307+
308308 if (trackFrameRate == 0 )
309309 {
310310 trackFrameRate = 30 ;
311311 }
312-
312+
313313 videoComposition.frameDuration = CMTimeMake (1 , trackFrameRate);
314314 CGSize targetSize = CGSizeMake ([self .videoSettings[AVVideoWidthKey] floatValue ], [self .videoSettings[AVVideoHeightKey] floatValue ]);
315315 CGSize naturalSize = [videoTrack naturalSize ];
316-
316+
317317 CGAffineTransform transform = videoTrack.preferredTransform ;
318318 videoComposition.frameDuration = CMTimeMake (1 , trackFrameRate);
319-
319+
320320 CGFloat videoAngleInDegree = atan2 (transform.b , transform.a ) * 180 / M_PI;
321321 if (videoAngleInDegree == 90 || videoAngleInDegree == -90 ) {
322322 CGFloat width = naturalSize.width ;
323323 naturalSize.width = naturalSize.height ;
324324 naturalSize.height = width;
325325 }
326-
326+
327327 if (transform.a == 0 && transform.b == 1.0 && transform.c == -1.0 && transform.d == 0 ) {
328328 // portrait, home button at bottom (upright)
329329 if ( (transform.tx == 0 ) && (transform.ty == 0 ) ) {
@@ -353,14 +353,14 @@ - (AVMutableVideoComposition *)buildDefaultVideoComposition
353353 videoComposition.renderSize = naturalSize;
354354 AVMutableVideoCompositionInstruction *passThroughInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction ];
355355 passThroughInstruction.timeRange = CMTimeRangeMake (kCMTimeZero , self.asset .duration );
356-
356+
357357 AVMutableVideoCompositionLayerInstruction *passThroughLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack: videoTrack];
358-
358+
359359 [passThroughLayer setTransform: transform atTime: kCMTimeZero ];
360-
360+
361361 passThroughInstruction.layerInstructions = @[passThroughLayer];
362362 videoComposition.instructions = @[passThroughInstruction];
363-
363+
364364 return videoComposition;
365365}
366366
@@ -371,7 +371,7 @@ - (void)finish
371371 {
372372 return ;
373373 }
374-
374+
375375 if (self.writer .status == AVAssetWriterStatusFailed)
376376 {
377377 [self complete ];
@@ -395,7 +395,7 @@ - (void)complete
395395 {
396396 [NSFileManager .defaultManager removeItemAtURL: self .outputURL error: nil ];
397397 }
398-
398+
399399 if (self.completionHandler )
400400 {
401401 self.completionHandler ();
0 commit comments