Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 260
Sivasagar Palakurthy Avatar asked Sep 30 '16 05:09

Sivasagar Palakurthy


1 Answers

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

like image 144
Ravi Avatar answered Oct 11 '22 08:10

Ravi