Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. func removeAudioFromVideo(_ videoPath: String) {
  2. let initPath1: String = videoPath
  3. let composition = AVMutableComposition()
  4. let inputVideoPath: String = initPath1
  5. let sourceAsset = AVURLAsset(url: URL(fileURLWithPath: inputVideoPath), options: nil)
  6. let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
  7. let sourceVideoTrack: AVAssetTrack? = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
  8. let x: CMTimeRange = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration)
  9. _ = try? compositionVideoTrack!.insertTimeRange(x, of: sourceVideoTrack!, at: kCMTimeZero)
  10. if FileManager.default.fileExists(atPath: initPath1) {
  11. try? FileManager.default.removeItem(atPath: initPath1)
  12. }
  13. let url = URL(fileURLWithPath: initPath1)
  14. let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
  15. exporter?.outputURL = url
  16. exporter?.outputFileType = "com.apple.quicktime-movie"
  17. exporter?.exportAsynchronously(completionHandler: {() -> Void in
  18. self.saveFinalVideoFile(toDocuments: exporter!.outputURL!)
  19. })
  20. }
  21.  
  22. func saveFinalVideoFile(toDocuments url: URL) {
  23. let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Videos")
  24. let movieData = try? Data(contentsOf: url)
  25. try? movieData?.write(to: fileURL, options: .atomic)
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement