0
私は、2つのビデオを結合して1つにマージし、ビデオにカスタムオーディオトラックを配置するアプリケーションを構築しています。これはすべて完璧に機能します。今私は自分のオーディオをフェードアウトしたい。これに対して、これはうまくいかず、理由もわかりません。 これは私のコードです:AVAssetExportSession、AudioMixでAVMutableCompositionが機能しなくなってしまいました。
let duration = composition.duration
let durationInSeconds = CMTimeGetSeconds(duration) * 10
let composition = AVMutableComposition()
let item = AVPlayerItem(asset: composition)
let params = AVMutableAudioMixInputParameters(track: composition.tracks.first! as AVAssetTrack)
let lastSecond = CMTimeRangeMake(CMTimeMakeWithSeconds(durationInSeconds-10, 10), CMTimeMakeWithSeconds(1, 1))
params.setVolumeRamp(fromStartVolume: 1, toEndVolume: 0, timeRange: lastSecond)
let mix = AVMutableAudioMix()
mix.inputParameters = [params]
item.audioMix = mix
// Put the track under the video
do {
try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, current), of: backgroundAudio.tracks(withMediaType: AVMediaTypeAudio)[0], at: kCMTimeZero)
} catch _ {
print("Failed to load Audio track")
}
guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.audioMix = mix
exporter.outputURL = URL(fileURLWithPath: finalVideoPath)
exporter.outputFileType = AVFileTypeMPEG4
exporter.shouldOptimizeForNetworkUse = true
そして、このコードブロックの後には、映像自体がレンダリングされている別の方法に続きます。 誰かがなぜこれがうまくいかず、これを修正する方法を教えてもらえますか?
ありがとうございます!