0
我是swift中的新人。我需要從視頻文件中刪除音頻並通過URL播放。我已經通過這些link1 & link2 ...但當我試圖快速轉換它們時出現了很多錯誤。 任何幫助將不勝感激。Swift - 從視頻中刪除音頻
我是swift中的新人。我需要從視頻文件中刪除音頻並通過URL播放。我已經通過這些link1 & link2 ...但當我試圖快速轉換它們時出現了很多錯誤。 任何幫助將不勝感激。Swift - 從視頻中刪除音頻
與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)")
}
}
}
}
從你的鏈接2採取代碼中有一個名爲swiftify是幫助轉換Objective-C代碼來迅速利用工具,它並嘗試瞭解代碼的功能。 –
謝謝...但我用swiftify試過並沒有理解(很多錯誤) –
有人已將代碼轉換爲Swift 3.您應該檢查它。 – HDT