Ios Swift-从视频中删除音频

Ios Swift-从视频中删除音频,ios,swift,audio,video,Ios,Swift,Audio,Video,我是新来的斯威夫特。我需要删除视频文件中的音频,并通过URL播放它们。我已经经历了这些&…但是当我试图用swift转换它们时,出现了很多错误。 任何帮助都将不胜感激。在我的帮助下,我编写了这段代码&这对我很有用 var mutableVideoURL = NSURL() //final video url func removeAudioFromVideo(_ videoURL: URL) { let inputVideoURL: URL = videoURL l

我是新来的斯威夫特。我需要删除视频文件中的音频,并通过URL播放它们。我已经经历了这些&…但是当我试图用swift转换它们时,出现了很多错误。 任何帮助都将不胜感激。

在我的帮助下,我编写了这段代码&这对我很有用

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)")
                }
            }
        }
    }
在我的帮助下,我写了这段代码&这对我来说很有用

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)")
                }
            }
        }
    }
Swift 4.2

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)")
            }
        }

}
注意:此行的添加

compositionVideoTrack?.preferredTransform = sourceVideoTrack!.preferredTransform
保持视频的方向性

Swift 4.2

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)")
            }
        }

}
注意:此行的添加

compositionVideoTrack?.preferredTransform = sourceVideoTrack!.preferredTransform

从你的链接中保留视频方向2获取该代码有一个名为swiftify的工具,可以帮助将objective-c代码转换为swift使用它。并尝试了解该代码的作用。谢谢…但我尝试使用swiftify,但未能理解(很多错误)有人将代码转换为swift 3。你应该去看看。在你的链接2中,有一个名为swiftify的工具,可以帮助你将objective-c代码转换成swift。使用它。并尝试理解该代码的功能。谢谢……但我尝试了swiftify,但未能理解(很多错误)有人将代码转换成了swift 3。你应该去看看。