2017-02-22 110 views
0

Я новый в быстром. Мне нужно удалить аудио из видеофайлов и воспроизвести их по URL-адресу. Я прошел через эти link1 & link2 ... но было много ошибок, когда я попытался их преобразовать быстро. Любая помощь будет принята с благодарностью.Swift - Удалить аудио из видео

+0

Вашего LINK2 принять этот код есть инструмент под названием swiftify, которые помогают преобразовать Objective-C кода для быстрого использования его . И попытайтесь понять, что делает этот код. –

+0

спасибо ... но я пробовал с swiftify и не понял (много ошибок) –

+0

Кто-то преобразовал код в Swift 3. Вы должны его проверить. – HDT

ответ

0

с помощью this link я написал этот код & это работало для меня ...

var mutableVideoURL = NSURL() //final video url 
func removeAudioFromVideo(_ videoURL: URL) { 
     let inputVideoURL: URL = videoURL 
     let sourceAsset = AVURLAsset(url: inputVideoURL) 
     let sourceVideoTrack: AVAssetTrack? = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0] 
     let composition : AVMutableComposition = AVMutableComposition() 
     let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) 
     let x: CMTimeRange = CMTimeRangeMake(kCMTimeZero, sourceAsset.duration) 
     _ = try? compositionVideoTrack!.insertTimeRange(x, of: sourceVideoTrack!, at: kCMTimeZero) 
     mutableVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/FinalVideo.mp4") 
     let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)! 
     exporter.outputFileType = AVFileTypeMPEG4 
     exporter.outputURL = mutableVideoURL as URL 
     removeFileAtURLIfExists(url: mutableVideoURL) 
     exporter.exportAsynchronously(completionHandler: 
      { 
       switch exporter.status 
       { 
        case AVAssetExportSessionStatus.failed: 
         print("failed \(exporter.error)") 
        case AVAssetExportSessionStatus.cancelled: 
         print("cancelled \(exporter.error)") 
        case AVAssetExportSessionStatus.unknown: 
         print("unknown\(exporter.error)") 
        case AVAssetExportSessionStatus.waiting: 
         print("waiting\(exporter.error)") 
        case AVAssetExportSessionStatus.exporting: 
         print("exporting\(exporter.error)") 
        default: 
         print("-----Mutable video exportation complete.") 
       } 
      }) 
    } 

    func removeFileAtURLIfExists(url: NSURL) { 
     if let filePath = url.path { 
      let fileManager = FileManager.default 
      if fileManager.fileExists(atPath: filePath) { 
       do{ 
        try fileManager.removeItem(atPath: filePath) 
       } catch let error as NSError { 
        print("Couldn't remove existing destination file: \(error)") 
       } 
      } 
     } 
    } 

 Смежные вопросы

  • Нет связанных вопросов^_^