Post

Replies

Boosts

Views

Activity

Reply to AVAssetWriter error code on finishWriting
This error is mostly because you are trying to export an AVAsset. Solve this using AVMutableComposition for export. sample code for trimming a video file let manager = FileManager.default         guard let documentDirectory = try?  manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)  else { print("TRIM Failed to access directory")             return}         let mediaType = "mp4"         if mediaType == kUTTypeMovie as String || mediaType == "mp4" as String {                let asset = AVAsset(url:videoURL!)             let length = Float(asset.duration.value) / Float(asset.duration.timescale)                print("TRIM video length: \(length) seconds")             let composition = AVMutableComposition()             let audioTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)!             let videoTrack: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!             var outURL_speed=documentDirectory.appendingPathComponent("output")             var outputURL = documentDirectory.appendingPathComponent("output")                     do {                         try audioTrack.insertTimeRange(CMTimeRangeFromTimeToTime(start: self.startTime, end: self.endTime), of: asset.tracks(withMediaType: AVMediaType.audio)[0], at: CMTime.zero)                         try videoTrack.insertTimeRange(CMTimeRangeFromTimeToTime(start: self.startTime, end: self.endTime), of: asset.tracks(withMediaType: AVMediaType.video)[0], at: CMTime.zero)                                               try manager.createDirectory(at: outputURL, withIntermediateDirectories: true, attributes: nil)                         outputURL = outputURL.appendingPathComponent("preVideo-\(self.postID).\(mediaType)")                         outURL_speed = outURL_speed.appendingPathComponent("Video-\(self.postID).\(mediaType)")                         print("TRIM output dir: \(outputURL)")                     }catch let error {                         print(error)                     }             _ = try? manager.removeItem(at: outputURL)                                                             guard let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else {return}                          exportSession.outputURL = outputURL             exportSession.shouldOptimizeForNetworkUse = true             exportSession.outputFileType = .mp4             let timeRange = CMTimeRange(start: self.startTime, end: self.endTime)             exportSession.timeRange = timeRange                          exportSession.exportAsynchronously{                 switch exportSession.status {                 case .completed:                     print("TRIM exported at \(outputURL)")                     self.changeSpeed(url: outputURL,outUrl:outURL_speed)                 case .failed:                     print("TRIM failed \(exportSession.error)")                 case .cancelled:                     print("TRIM cancelled \(exportSession.error)")                 default: break                 }             }
Jul ’21