Swift-从视频中删除音频



我是新手。我需要从视频文件中删除音频并通过URL播放。我经历了这些链接1&link2 ...但是当我尝试将它们转换为Swift时,有很多错误。任何帮助将不胜感激。

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

保留视频的方向

在此链接的帮助下,我编写了此代码&这对我有用...

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

最新更新