[英]Silence is not adding on end of an audio file
我正在演示如何在給定音頻文件的末尾添加額外的靜音音頻。
這里我的音頻文件長度是 29 秒。 & 我正在添加 11 秒的靜音。 因此,最終輸出音頻長度將為 40 秒。
這是我的功能,
func addSilenceInAudio(inputFilePath:URL, silenceTime: Int, minimumAudioLength: Int, completionBlock:@escaping ((String?, Error?) -> Void)) {
let asset = AVURLAsset(url: inputFilePath, options: nil)
//get an original audio length
let endAudioTime = CMTimeMake(value: Int64(silenceTime), timescale: 1)
let composition = AVMutableComposition()
let insertAt = CMTimeRange(start: CMTime.zero , end: endAudioTime)
let assetTimeRange = CMTimeRange(start: CMTime.zero, end:asset.duration)
//here i'm inserting range
try! composition.insertTimeRange(assetTimeRange, of: asset, at: insertAt.end)
let exportSessionNew = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
exportSessionNew?.outputFileType = AVFileType.m4a
let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let dateString = (Date().millisecondsSince1970) //added file name here
let outputURL = documentURL.appendingPathComponent("\(dateString).m4a") //file name must be .m4a
exportSessionNew?.outputURL = outputURL //output url
exportSessionNew?.exportAsynchronously(completionHandler: {
() -> Void in
print(exportSessionNew as Any)
if exportSessionNew!.status == AVAssetExportSession.Status.completed {
// All is working fine!!
print(exportSessionNew?.outputURL as Any) //get outputfile
print("success")
completionBlock("\(String(describing: exportSessionNew?.outputURL))", nil)
} else {
print("failed")
completionBlock(nil, exportSessionNew?.error)
}
})
}
上面的代碼工作正常,我得到了 40 秒的輸出音頻。
但問題是在音頻文件的開頭添加了 11 秒的靜音。
它應該在音頻文件的末尾。
我在這里做錯了什么?
你基本上只需要延長音頻的長度。
所以...
let assetTimeRange = CMTimeRange(start: CMTime.zero, end:asset.duration)
更改為類似的東西
let newDuration = asset.duration + silenceTime
let assetTimeRange = CMTimeRange(start: CMTime.zero, end:newDuration)
免責聲明:我已經有一段時間沒有這樣做了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.