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

2

6

I am able to compress video captured from camera device to h264 format using video toolbox framework, but when I tried to play that h264 file in VLC player I am not able to hear the audio of the video. I think audio compression should also be done in code.

But how I didn't found any resource?

Platelet answered 30/9, 2016 at 5:29 Comment(1)
Can you provide more information? The code you compress the video with, maybe a link to the result.Chukchi
C
1

You can decode your recorded video by this way :-

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 at outputURL.

For more information, please check Apple's doc

Colza answered 16/4, 2019 at 7:5 Comment(0)
T
0

Answering on my own bounty. Please refer to this great question answer and comments:

Compute PTS and DTS correctly to sync audio and video ffmpeg C++.

Even though the solution there implemented in C++ but the logic can be ported to IOS as I've also implemented in my own iOS project.

Tobacco answered 17/1, 2017 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.