@@ -259,37 +259,19 @@ func makeValidUri(filePath: String) -> String {
259259 }
260260
261261 func autoCompressionHelper( url: URL , options: [ String : Any ] , onProgress: @escaping ( Float ) -> Void , onCompletion: @escaping ( URL ) -> Void , onFailure: @escaping ( Error ) -> Void ) {
262- var bitRate = options [ " bitrate " ] as! Float ? ;
263- var tmpURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true )
264- . appendingPathComponent ( ProcessInfo ( ) . globallyUniqueString)
265- . appendingPathExtension ( " mp4 " )
266- tmpURL = URL ( string: makeValidUri ( filePath: tmpURL. absoluteString) ) !
267-
268262 let maxSize : Float = options [ " maxSize " ] as! Float ;
269- var _bitRate = bitRate;
263+
270264 let asset = AVAsset ( url: url)
271265 guard asset. tracks. count >= 1 else {
272266 let error = CompressionError ( message: " Invalid video URL, no track found " )
273267 onFailure ( error)
274268 return
275269 }
276- var videoTrackIndex : Int = 0 ;
277- let trackLength = asset. tracks. count;
278- if ( trackLength== 2 )
279- {
280- if ( asset. tracks [ 0 ] . mediaType. rawValue== " soun " )
281- {
282- videoTrackIndex= 1 ;
283- }
284- }
285- let track = asset. tracks [ videoTrackIndex] ;
286- let exporter = NextLevelSessionExporter ( withAsset: asset)
287- exporter. outputURL = tmpURL
288- exporter. outputFileType = AVFileType . mp4
270+ let track = getVideoTrack ( asset: asset) ;
289271
290272 let videoSize = track. naturalSize. applying ( track. preferredTransform) ;
291- var actualWidth = Float ( abs ( videoSize. width) )
292- var actualHeight = Float ( abs ( videoSize. height) )
273+ let actualWidth = Float ( abs ( videoSize. width) )
274+ let actualHeight = Float ( abs ( videoSize. height) )
293275
294276 let bitrate = Float ( abs ( track. estimatedDataRate) ) ;
295277 let scale : Float = actualWidth > actualHeight ? ( Float ( maxSize) / actualWidth) : ( Float ( maxSize) / actualHeight) ;
@@ -301,63 +283,17 @@ func makeValidUri(filePath: String) -> String {
301283 originalBitrate: Int ( bitrate) ,
302284 height: Int ( resultHeight) , width: Int ( resultWidth)
303285 ) ;
304-
305-
306- let compressionDict : [ String : Any ] = [
307- AVVideoAverageBitRateKey: videoBitRate,
308- AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
309- ]
310- exporter. optimizeForNetworkUse = true ;
311- exporter. videoOutputConfiguration = [
312- AVVideoCodecKey: AVVideoCodecType . h264,
313- AVVideoWidthKey: resultWidth,
314- AVVideoHeightKey: resultHeight,
315- AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
316- AVVideoCompressionPropertiesKey: compressionDict
317- ]
318- exporter. audioOutputConfiguration = [
319- AVFormatIDKey: kAudioFormatMPEG4AAC,
320- AVEncoderBitRateKey: NSNumber ( integerLiteral: 128000 ) ,
321- AVNumberOfChannelsKey: NSNumber ( integerLiteral: 2 ) ,
322- AVSampleRateKey: NSNumber ( value: Float ( 44100 ) )
323- ]
324-
325286
326- exporter. export ( progressHandler: { ( progress) in
327- let _progress : Float = progress*100;
328- if ( Int ( _progress) == self . videoCompressionCounter)
329- {
330- self . videoCompressionCounter= Int ( _progress) + self . videoCompressionThreshold
287+ exportVideoHelper ( url: url, asset: asset, bitRate: videoBitRate, resultWidth: resultWidth, resultHeight: resultHeight) { progress in
331288 onProgress ( progress)
332- }
333-
334- } , completionHandler: { result in
335- self . videoCompressionCounter= 0 ;
336- switch result {
337- case . success( let status) :
338- switch status {
339- case . completed:
340- onCompletion ( exporter. outputURL!)
341- break
342- default :
343- let error = CompressionError ( message: " Compression didn't complete " )
344- onFailure ( error)
345- break
346- }
347- break
348- case . failure( let error) :
289+ } onCompletion: { outputURL in
290+ onCompletion ( outputURL)
291+ } onFailure: { error in
349292 onFailure ( error)
350- break
351- }
352- } )
293+ }
353294 }
354295
355-
356296 func manualCompressionHelper( url: URL , bitRate: Float ? , onProgress: @escaping ( Float ) -> Void , onCompletion: @escaping ( URL ) -> Void , onFailure: @escaping ( Error ) -> Void ) {
357- var tmpURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true )
358- . appendingPathComponent ( ProcessInfo ( ) . globallyUniqueString)
359- . appendingPathExtension ( " mp4 " )
360- tmpURL = URL ( string: makeValidUri ( filePath: tmpURL. absoluteString) ) !
361297
362298 var _bitRate = bitRate;
363299 let asset = AVAsset ( url: url)
@@ -366,19 +302,7 @@ func makeValidUri(filePath: String) -> String {
366302 onFailure ( error)
367303 return
368304 }
369- var videoTrackIndex : Int = 0 ;
370- let trackLength = asset. tracks. count;
371- if ( trackLength== 2 )
372- {
373- if ( asset. tracks [ 0 ] . mediaType. rawValue== " soun " )
374- {
375- videoTrackIndex= 1 ;
376- }
377- }
378- let track = asset. tracks [ videoTrackIndex] ;
379- let exporter = NextLevelSessionExporter ( withAsset: asset)
380- exporter. outputURL = tmpURL
381- exporter. outputFileType = AVFileType . mp4
305+ let track = getVideoTrack ( asset: asset) ;
382306
383307 let videoSize = track. naturalSize. applying ( track. preferredTransform) ;
384308 var width = Float ( abs ( videoSize. width) )
@@ -399,15 +323,34 @@ func makeValidUri(filePath: String) -> String {
399323
400324 let videoBitRate = _bitRate ?? height*width*1. 5
401325
326+ exportVideoHelper ( url: url, asset: asset, bitRate: Int ( videoBitRate) , resultWidth: width, resultHeight: height) { progress in
327+ onProgress ( progress)
328+ } onCompletion: { outputURL in
329+ onCompletion ( outputURL)
330+ } onFailure: { error in
331+ onFailure ( error)
332+ }
333+ }
334+
335+ func exportVideoHelper( url: URL , asset: AVAsset , bitRate: Int , resultWidth: Float , resultHeight: Float , onProgress: @escaping ( Float ) -> Void , onCompletion: @escaping ( URL ) -> Void , onFailure: @escaping ( Error ) -> Void ) {
336+ var tmpURL = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true )
337+ . appendingPathComponent ( ProcessInfo ( ) . globallyUniqueString)
338+ . appendingPathExtension ( " mp4 " )
339+ tmpURL = URL ( string: makeValidUri ( filePath: tmpURL. absoluteString) ) !
340+
341+ let exporter = NextLevelSessionExporter ( withAsset: asset)
342+ exporter. outputURL = tmpURL
343+ exporter. outputFileType = AVFileType . mp4
344+
402345 let compressionDict : [ String : Any ] = [
403- AVVideoAverageBitRateKey: videoBitRate ,
346+ AVVideoAverageBitRateKey: bitRate ,
404347 AVVideoProfileLevelKey: AVVideoProfileLevelH264HighAutoLevel,
405348 ]
406349 exporter. optimizeForNetworkUse = true ;
407350 exporter. videoOutputConfiguration = [
408351 AVVideoCodecKey: AVVideoCodecType . h264,
409- AVVideoWidthKey: width ,
410- AVVideoHeightKey: height ,
352+ AVVideoWidthKey: resultWidth ,
353+ AVVideoHeightKey: resultHeight ,
411354 AVVideoScalingModeKey: AVVideoScalingModeResizeAspectFill,
412355 AVVideoCompressionPropertiesKey: compressionDict
413356 ]
@@ -436,15 +379,31 @@ func makeValidUri(filePath: String) -> String {
436379 onCompletion ( exporter. outputURL!)
437380 break
438381 default :
439- let error = CompressionError ( message: " Compression didn't complete " )
440- onFailure ( error)
382+ // let error = CompressionError(message: "Compression didn't complete")
383+ // onFailure(error)
384+ onCompletion ( url)
441385 break
442386 }
443387 break
444388 case . failure( let error) :
445- onFailure ( error)
389+ // onFailure(error)
390+ onCompletion ( url)
446391 break
447392 }
448393 } )
449- }
394+ }
395+
396+ func getVideoTrack( asset: AVAsset ) -> AVAssetTrack {
397+ var videoTrackIndex : Int = 0 ;
398+ let trackLength = asset. tracks. count;
399+ if ( trackLength== 2 )
400+ {
401+ if ( asset. tracks [ 0 ] . mediaType. rawValue== " soun " )
402+ {
403+ videoTrackIndex= 1 ;
404+ }
405+ }
406+ let track = asset. tracks [ videoTrackIndex] ;
407+ return track;
408+ }
450409 }
0 commit comments