[英]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.