2017-07-25 154 views
0

我的问题是,我正在使用下面的功能来编写视频和音频。我想保持视频的原始声音,但它以某种方式消失,我没有任何线索。撰写视频和音频 - 视频音频不见了

我从this answer

这个功能我想追加AVMutableCompositionTrack年代后向右改变卷,但没有奏效

例如;

mutableVideoCompositionTrack.prefferedVolume = 1.0 
mutableAudioCompositionTrack.prefferedVolume = 0.05 

但是,你所能听到的只是音频文件。

功能;

private func mergeAudioAndVideo(audioUrl: URL, videoUrl: URL, completion: @escaping (Bool)->Void){ 

    let mixComposition = AVMutableComposition() 
    var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = [] 
    var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = [] 
    let totalVideoCompositionInstruction = AVMutableVideoCompositionInstruction() 

    let videoAsset = AVAsset(url: videoUrl) 
    let audioAsset = AVAsset(url: audioUrl) 

    mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)) 
    mutableCompositionAudioTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)) 
    mutableCompositionAudioTrack[0].preferredVolume = 0.05 
    mutableCompositionVideoTrack[0].preferredVolume = 1.0   


    let videoAssetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] 
    let audioAssetTrack = audioAsset.tracks(withMediaType: AVMediaTypeAudio)[0] 

    do { 
     try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration), of: videoAssetTrack, at: kCMTimeZero) 
     try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration), of: audioAssetTrack, at: kCMTimeZero) 
    }catch{ 
     print("ERROR#1") 
    } 

    totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration) 

    let mutableVideoComposition = AVMutableVideoComposition() 
    mutableVideoComposition.frameDuration = CMTimeMake(1, 30) 
    mutableVideoComposition.renderSize = CGSize(width: 1280, height: 720) 

    //exporting 

    savePathUrl = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("merged").appendingPathExtension("mov") 

    let assetExport = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)! 
    assetExport.outputFileType = AVFileTypeMPEG4 
    assetExport.outputURL = savePathUrl 
    assetExport.shouldOptimizeForNetworkUse = true 

    do { 
     try FileManager.default.removeItem(at: savePathUrl) 
    }catch { 
     print(error) 
    } 

    assetExport.exportAsynchronously { 
     switch assetExport.status{ 
     case .completed: 
      print("completed") 
      completion(true) 
     default: 
      print("failed \(assetExport.error!)") 
      completion(false) 
     } 
    } 

} 

回答

1

我想通了。它似乎是一个加载视频的AVAsset,可分别保存音频和视频。所以你可以写他们'

videoAsset.tracks(withMediaType: AVMediaTypeAudio)[0] //audio of a video 
videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] //video of a video(without sound) 

因此,我将这些行添加到代码,它的工作!

​​

还有一个我不知道该怎么做的缺点,那就是设置这些音频资源的音量。我会尽快更新这个答案。

+0

我想保持视频的音频与我的自定义声音在一个文件中,以便它同时扮演音频播放,同时最终的视频。这个功能可能吗? – parth

+0

这正是我在这里所做的。但正如我所说,我不知道如何改变每个音频的音量。 @parth – Faruk

1

您可以分别调整视频和音频的音量@Faruk,这里是一点点的代码。

 //Extract audio from the video and the music 
let audioMix: AVMutableAudioMix = AVMutableAudioMix() 
var audioMixParam: [AVMutableAudioMixInputParameters] = [] 

let assetVideoTrack: AVAssetTrack = assetVideo.tracksWithMediaType(AVMediaTypeAudio)[0] 
let assetMusicTrack: AVAssetTrack = assetMusic.tracksWithMediaType(AVMediaTypeAudio)[0] 

let videoParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: assetVideoTrack) 
videoParam.trackID = compositionAudioVideo.trackID 

let musicParam: AVMutableAudioMixInputParameters = AVMutableAudioMixInputParameters(track: assetMusicTrack) 
musicParam.trackID = compositionAudioMusic.trackID 

//Set final volume of the audio record and the music 
videoParam.setVolume(volumeVideo, atTime: kCMTimeZero) 
musicParam.setVolume(volumeAudio, atTime: kCMTimeZero) 

//Add setting 
audioMixParam.append(musicParam) 
audioMixParam.append(videoParam) 

//Add audio on final record 
//First: the audio of the record and Second: the music 
do { 
try compositionAudioVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero, assetVideo.duration), ofTrack: assetVideoTrack, atTime: kCMTimeZero) 
} catch _ { 
assertionFailure() 
} 

do { 
try compositionAudioMusic.insertTimeRange(CMTimeRangeMake(CMTimeMake(Int64(startAudioTime * 10000), 10000), assetVideo.duration), ofTrack: assetMusicTrack, atTime: kCMTimeZero) 
} catch _ { 
assertionFailure() 
} 

//Add parameter 
audioMix.inputParameters = audioMixParam 

let completeMovie = "\(docsDir)/\(randomString(5)).mp4" 
let completeMovieUrl = NSURL(fileURLWithPath: completeMovie) 
let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)! 

exporter.outputURL = completeMovieUrl 
exporter.outputFileType = AVFileTypeMPEG4 
exporter.audioMix = audioMix 
exporter.exportAsynchronouslyWithCompletionHandler({ 

switch exporter.status { 

case AVAssetExportSessionStatus.Completed: 
    print("success with output url \(completeMovieUrl)") 
    case AVAssetExportSessionStatus.Failed: 
     print("failed \(String(exporter.error))") 
    case AVAssetExportSessionStatus.Cancelled: 
     print("cancelled \(String(exporter.error))") 
    default: 
     print("complete") 
    }    
}) 

}