ios - Swift - 从视频中删除音频

标签 ios swift audio video

我是 swift 的新手。我需要从视频文件中删除音频并通过 URL 播放它们。我经历了这些 link1 & link2 ...但是当我试图快速转换它们时出现了很多错误。 任何帮助将不胜感激。

最佳答案

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

保留视频的方向

关于ios - Swift - 从视频中删除音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383260/

相关文章:

ios - didSelectRowAt 未触发(Swift 5)

ios - 如何使用 Alamofire 发送表单数据主体

ios - iOS Swift 中使用 CBC 模式的 AES 128 解密

ios - 仅删除图像的一部分(目前整个图像被删除)

ios - 使用NSTimer自定义声音

iPhone:以编程方式混合两个音频文件?

java - 如何通过MediaView播放音频流

ios - iPhone - 需要一些关于 EXC_BAD_ACCESS 的帮助,这让我发疯(包括完整的源代码)

ios - Xamarin:为 Visual Studio 2015 添加新项目时没有 iOS 类

ios - 切换选项卡时WKWebview PDF问题