栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用AudioMixInputParameters AVFoundation为每个视频轨道设置多个音量在Swift iOS中不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用AudioMixInputParameters AVFoundation为每个视频轨道设置多个音量在Swift iOS中不起作用

这是我的问题的工作解决方案:

func addVolumeToIndividualVideoClip(_ assetURL: URL, video: VideoFileModel, completion : ((_ session: AVAssetExportSession?, _ outputURL : URL?) -> ())?){        //Create Asset from Url        let filteredVideoAsset: AVAsset = AVAsset(url: assetURL)        video.fileID = String(video.videoID)        //Get the path of App document Directory        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]        let url = URL(fileURLWithPath: documentDirectory).appendingPathComponent("(video.fileID)_("FilterVideo").mov")        let filePath = url.path        let fileManager = FileManager.default        do { if fileManager.fileExists(atPath: filePath) {     print("FILE AVAILABLE")     try fileManager.removeItem(atPath:filePath) } else {     print("FILE NOT AVAILABLE") }        } catch _ {        }        let composition: AVMutableComposition = AVMutableComposition()        let compositionVideo: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())        let compositionAudioVideo: AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())        //Add video to the final record        do {  try compositionVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero, filteredVideoAsset.duration), of: filteredVideoAsset.tracks(withMediaType: AVMediaTypeVideo)[0], at: kCMTimeZero)        } catch _ {        }        //Extract audio from the video and the music        let audioMix: AVMutableAudioMix = AVMutableAudioMix()        var audioMixParam: [AVMutableAudioMixInputParameters] = []        let assetVideoTrack: AVAssetTrack = filteredVideoAsset.tracks(withMediaType: AVMediaTypeAudio)[0]        let videoParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: assetVideoTrack)        videoParam.trackID = compositionAudioVideo.trackID        //Set final volume of the audio record and the music        videoParam.setVolume(video.videoVolume, at: kCMTimeZero)        //Add setting        audioMixParam.append(videoParam)        //Add audio on final record        //First: the audio of the record and Second: the music        do { try compositionAudioVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero, filteredVideoAsset.duration), of: assetVideoTrack, at: kCMTimeZero)        } catch _ { assertionFailure()        }        //Fading volume out for background music        let durationInSeconds = CMTimeGetSeconds(filteredVideoAsset.duration)        let firstSecond = CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(1, 1))        let lastSecond = CMTimeRangeMake(CMTimeMakeWithSeconds(durationInSeconds-1, 1), CMTimeMakeWithSeconds(1, 1))        videoParam.setVolumeRamp(fromStartVolume: 0, toEndVolume: video.videoVolume, timeRange: firstSecond)        videoParam.setVolumeRamp(fromStartVolume: video.videoVolume, toEndVolume: 0, timeRange: lastSecond)        //Add parameter        audioMix.inputParameters = audioMixParam        //Remove the previous temp video if exist        let filemgr = FileManager.default        do { if filemgr.fileExists(atPath: "(video.fileID)_("FilterVideo").mov") {     try filemgr.removeItem(atPath: "(video.fileID)_("FilterVideo").mov") } else { }        } catch _ {        }        //Exporte the final record’        let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!        exporter.outputURL = url        exporter.outputFileType = AVFileTypeMPEG4        exporter.audioMix = audioMix        exporter.exportAsynchronously(completionHandler: { () -> Void in completion!(exporter, url)// self.saveVideoToLibrary(from: filePath)        })    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392100.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号