1

I'm trying to encode a video so that it can be read on chrome in HTML5. Unfortunately I am not able to find a codec. I have tried MP4 and Apple .mov ( H.265 ). I can play the audio track, but there is no picture with the HTML5 player on Chrome. I tried to encode in MP4 ... but it doesn't work. The video is sent to Google Storage and displayed on Chrome with HTML5. Does anyone have a solution? Thanks in advance.

func encodeVideo(videoUrl: URL, outputUrl: URL? = nil, resultClosure: @escaping (URL?) -> Void ) {

var finalOutputUrl: URL? = outputUrl

if finalOutputUrl == nil {
    var url = videoUrl
    url.deletePathExtension()
    url.appendPathExtension(".mp4")
    finalOutputUrl = url
}

if FileManager.default.fileExists(atPath: finalOutputUrl!.path) {
    print("Converted file already exists \(finalOutputUrl!.path)")
    resultClosure(finalOutputUrl)
    return
}

let asset = AVURLAsset(url: videoUrl)
if let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) {
    exportSession.outputURL = finalOutputUrl!
    exportSession.outputFileType = AVFileTypeMPEG4
    let start = CMTimeMakeWithSeconds(0.0, 0)
    let range = CMTimeRangeMake(start, asset.duration)
    exportSession.timeRange = range
    exportSession.shouldOptimizeForNetworkUse = true
    exportSession.exportAsynchronously() {

        switch exportSession.status {
        case .failed:
            print("Export failed: \(exportSession.error != nil ? exportSession.error!.localizedDescription : "No Error Info")")
        case .cancelled:
            print("Export canceled")
        case .completed:
            resultClosure(finalOutputUrl!)
        default:
            break
        }
    }
} else {
    resultClosure(nil)
}

}

Fox5150
  • 2,180
  • 22
  • 24

1 Answers1

1

It works in Chrome when AVAssetExportPresetHighestQuality preset is used instead of AVAssetExportPresetPassthrough. See full example here

tadija
  • 2,981
  • 26
  • 37