How to encode audio along with video to h264 format using VideoToolbox?

I can compress the video shot from the camera device to h264 using the frame of the video objects, but when I tried to play this h264 file in the VLC player, I could not hear the sound of the video. I think that sound compression should also be done in code.

But how did I not find any resource?

+5
source share
2 answers

Responding to my own generosity. Please refer to this wonderful answer and comments:

Calculate PTS and DTS correctly for ffmpeg C ++ audio and video synchronization .

Despite the fact that the solution is implemented in C ++, but the logic can be ported to IOS, as I also implemented in my iOS project.

0
source

You can decode the recorded video as follows:

func encodeReocrdedVideoToH264(url:URL) { let anAsset = AVAsset.init(url: url)// Your source AVAsset movie in HEVC format // let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let outputURL = documentsURL?.appendingPathComponent("recording.mp4") // These settings will encode using H.264. let preset = AVAssetExportPresetHighestQuality let outFileType = AVFileType.mov AVAssetExportSession.determineCompatibility(ofExportPreset: preset, with: anAsset, outputFileType: outFileType) { (isCompatible) in if !isCompatible { print("AVAssetExportSession Not Compatible") return } } guard let export = AVAssetExportSession(asset: anAsset, presetName: preset) else { return } export.outputFileType = outFileType export.outputURL = outputURL export.exportAsynchronously { () -> Void in // Handle export results. print("exportAsynchronously Succesuffully") } } 

You can see your encoded video on outputURL.

For more information, please check out Apple Doc.

0
source

Source: https://habr.com/ru/post/1257490/


All Articles