- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个视频,我正在将另一个音频合并到那个视频,所以现在我有两个音频和那个视频,一个是原始声音,另一个是视频自己的声音。现在的问题是我可以为视频自己的声音设置音量,但我无法设置外部音频的音量。我用下面的代码来做同样的事情。
func mergeAV(videoUrl: URL, audioUrl: URL, shouldFlipHorizontally: Bool = false) {
print("\(#function)")
process_merge.startAnimating()
let mixComposition = AVMutableComposition()
var mutableCompositionVideoTrack = [AVMutableCompositionTrack]()
var mutableCompositionAudioTrack = [AVMutableCompositionTrack]()
var mutableCompositionAudioOfVideoTrack = [AVMutableCompositionTrack]()
let aVideoAsset = AVAsset(url: videoUrl)
let aAudioAsset = AVAsset(url: audioUrl)
let compositionAddVideo = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let compositionAddAudio = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
let compositionAddAudioOfVideo = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
let aVideoAssetTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
let aAudioOfVideoAssetTrack: AVAssetTrack? = aVideoAsset.tracks(withMediaType: AVMediaType.audio).first
let aAudioAssetTrack: AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaType.audio)[0]
compositionAddVideo?.preferredTransform = aVideoAssetTrack.preferredTransform
mutableCompositionVideoTrack.append(compositionAddVideo!)
mutableCompositionAudioTrack.append(compositionAddAudio!)
mutableCompositionAudioOfVideoTrack.append(compositionAddAudioOfVideo!)
do {
try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: CMTime.zero)
try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: CMTime.zero)
if let aAudioOfVideoAssetTrack = aAudioOfVideoAssetTrack {
try mutableCompositionAudioOfVideoTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration), of: aAudioOfVideoAssetTrack, at: CMTime.zero)
}
} catch {
print(error.localizedDescription)
}
// Exporting
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let savePathUrl = documentsDirectory.appendingPathComponent("MergeAudioVideo.mp4")
do {
// delete old video
try FileManager.default.removeItem(at: savePathUrl)
} catch {
print(error.localizedDescription)
}
var videoVolume : Float = 0.0
var audioVolume : Float = 0.0
videoVolume = self.vol_Video.value
audioVolume = self.vol_Audio.value
let audioMix: AVMutableAudioMix = AVMutableAudioMix()
var audioMixParam: [AVMutableAudioMixInputParameters] = []
let videoParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: aVideoAssetTrack)
videoParam.trackID = (compositionAddVideo?.trackID)!
let musicParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: aAudioAssetTrack)
musicParam.trackID = (compositionAddAudio?.trackID)!
videoParam.setVolume(videoVolume, at: CMTime.zero)
musicParam.setVolume(audioVolume, at: CMTime.zero)
videoParam.setVolumeRamp(fromStartVolume: videoVolume, toEndVolume: videoVolume, timeRange: aVideoAssetTrack.timeRange)
musicParam.setVolumeRamp(fromStartVolume: videoVolume, toEndVolume: audioVolume, timeRange: aAudioAssetTrack.timeRange)
audioMixParam.append(musicParam)
audioMixParam.append(videoParam)
audioMix.inputParameters = [musicParam,videoParam]
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.outputFileType = AVFileType.mp4
assetExport.outputURL = savePathUrl
assetExport.shouldOptimizeForNetworkUse = true
assetExport.audioMix = audioMix
print("Export Action")
assetExport.exportAsynchronously { () -> Void in
self.process_merge.stopAnimating()
switch assetExport.status {
case AVAssetExportSession.Status.completed:
print("Merge video+Audio URL : \(savePathUrl)")
print("Export : success")
self.showMess(strMess: "Success...\nMerge video+Audio URL: \(savePathUrl)", isAlert: false)
break
default:
print("Error : unknown")
self.showMess(strMess: "Something was wrong. Please try again.\nError: \(assetExport.error?.localizedDescription ?? "unknown")", isAlert: true)
break
}
}
}
我该如何解决这个问题?请帮忙!
最佳答案
func mergeFilesWithUrl(videoUrl: URL, audioUrl: URL) {
let mixComposition: AVMutableComposition = AVMutableComposition()
var mutableCompositionVideoTrack: [AVMutableCompositionTrack] = []
var mutableCompositionAudioTrack: [AVMutableCompositionTrack] = []
var mutableCompositionAudioOfVideoTrack: [AVMutableCompositionTrack] = []
let totalVideoCompositionInstruction: AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
let aVideoAsset: AVAsset = AVAsset(url: videoUrl)
let aAudioAsset: AVAsset = AVAsset(url: audioUrl)
mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid))
mutableCompositionAudioTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid))
mutableCompositionAudioOfVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid))
let aAudioOfVideoTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
let aVideoAssetTrack: AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
let aAudioAssetTrack: AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
do {
try mutableCompositionAudioOfVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aAudioOfVideoTrack, at: kCMTimeZero)
try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: kCMTimeZero)
try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: kCMTimeZero)
} catch {
}
totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration)
let mutableVideoComposition: AVMutableVideoComposition = AVMutableVideoComposition()
mutableVideoComposition.frameDuration = CMTimeMake(1, 30)
mutableVideoComposition.renderSize = CGSize(width: 1280, height: 720)//CGSize(1280,720)
//find your video on this URl
let savePathUrl: NSURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/newVideo.mp4")
do { // delete old video
try FileManager.default.removeItem(at: savePathUrl as URL)
} catch {
print(error.localizedDescription)
}
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.outputFileType = AVFileTypeMPEG4
assetExport.outputURL = savePathUrl as URL
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously {
switch assetExport.status {
case AVAssetExportSessionStatus.completed:
print("success")
case AVAssetExportSessionStatus.failed:
print("failed \(assetExport.error)")
case AVAssetExportSessionStatus.cancelled:
print("cancelled \(assetExport.error)")
default:
print("complete")
}
}
}
关于ios - Swift 合并音视频 Swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929686/
我是 Arduino 的新手(因为这个周末我必须学习它来完成一个学校项目)并且我无法让我的压电扬声器为闹钟发出声音。当草图运行时,一切正常。 LED 闪烁,但蜂鸣器不发声,这很奇怪,因为 LED 和蜂
我想知道是否有人遇到过在 iPhone SDK 中生成铃声的方法。我正在尝试生成 DTMF 音调,但似乎找不到任何实质性内容。我还希望能够指定播放音调的时间(即模拟按住按钮而不是简单地按下按钮....
我正在尝试制作一个可以在 Octave 音阶中水平滚动的钢琴应用程序。下面是另一个我尝试做的钢琴应用程序的示例。箭头在钢琴上滚动 Octave 。我已经了解了scrollview,但是似乎只允许通过滑
我正在我的 Android 应用程序中使用文本转语音 - TTS。我已经粘贴了下面的代码。TTS 工作正常,但是它说的语音/文本不是很清楚。它说的真的快速,以至于它不是完全可以理解的.. 我尝试设置
我正在调用一个带有号码的通话应用程序,然后通过 发送 dtmf 音 String number = "tel:+1234567,890#"; Intent c1 = new Intent(androi
我正在开展一个项目,该项目要求我的应用能够在通话期间以语音的上行链路频率发送 DTMF 音。 我的两个条件是: 我们不使用定制的 Android 平台 我们不需要root手机 我花了几天时间做功课,并
我需要制作一个IVR -auto dialer应用程序,它 => 调用免费电话 => 然后选择一个选项继续 => 然后输入一些代码 => 然后几秒钟后(一些广告/说 30 秒)需要输入另一个选项 前三
在我的 Twilio基于应用程序我通过 Twilio REST 创建拨出电话API。 当目标方响应时,我将他/她放到最近创建的 session 中。 在让他/她参加 session 之前,如何播放特定
Twilio 未从 Skype 检测到我的 DTMF 音。 我们用 twilio 建立了一个 IVR 系统,客户必须输入一个 7位客户编号。 我们的一些客户使用 voip 电话, 因此我们需要确保 t
我想在我的应用程序中以编程方式发送DTMF 铃声。在搜索时我找到了一个方法 public void playDtmfTone(字符数字) 这是 google 在 android 中发送 DTMF 的新
我读过一些有关 Android 中 DTMF 的文章。我想不可能通过流式传输 .wav 文件(或其他格式)进行自动电话调用并检测其他人(调用接收者)的输入。 我想制作一个应用程序来调用我/或其他人,并
我是一名优秀的程序员,十分优秀!