Swift - 从视频中删除音频

时间:2017-02-22 05:17:09

标签: ios swift audio video

我很快就是新人。我需要从视频文件中删除音频并通过URL播放它们。我已经完成了这些link1& link2 ...但是当我尝试在swift中转换它们时有很多错误。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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)")
                }
            }
        }
    }

答案 1 :(得分:0)

Swift 4.2

kelasID

注意:此行的添加

var mutableVideoURL: URL! //final video url
func removeAudioFromVideo(_ videoURL: URL) {
    let inputVideoURL: URL = videoURL
    let sourceAsset = AVURLAsset(url: inputVideoURL)
    let sourceVideoTrack: AVAssetTrack? = sourceAsset.tracks(withMediaType: AVMediaType.video)[0]
    let composition : AVMutableComposition = AVMutableComposition()
    let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
    compositionVideoTrack!.preferredTransform = sourceVideoTrack!.preferredTransform
    let x: CMTimeRange = CMTimeRangeMake(start: CMTime.zero, duration: sourceAsset.duration)
    _ = try? compositionVideoTrack!.insertTimeRange(x, of: sourceVideoTrack!, at: CMTime.zero)
    mutableVideoURL = documentsURL.appendingPathComponent("pppppppppp.mp4")
    let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
    exporter.outputFileType = AVFileType.mp4
    exporter.outputURL = mutableVideoURL
    removeFileAtURLIfExists(url: mutableVideoURL)
    exporter.exportAsynchronously(completionHandler:
        {
            switch exporter.status
            {
            case AVAssetExportSession.Status.failed:
                print("1000000000failed \(exporter.error)")
            case AVAssetExportSession.Status.cancelled:
                print("1000000000cancelled \(exporter.error)")
            case AVAssetExportSession.Status.unknown:
                print("1000000000unknown\(exporter.error)")
            case AVAssetExportSession.Status.waiting:
                print("1000000000waiting\(exporter.error)")
            case AVAssetExportSession.Status.exporting:
                print("1000000000exporting\(exporter.error)")
            default:
                print("1000000000-----Mutable video exportation complete.")

            }
    })
}

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

}

保持视频方向