Sivasagar Palakurthy
Sivasagar Palakurthy

Reputation: 481

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

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?

Upvotes: 6

Views: 1566

Answers (2)

Ravi
Ravi

Reputation: 93

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

Upvotes: 1

Raishul
Raishul

Reputation: 146

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.

Upvotes: 0

Related Questions