Видео, преобразованное из mov в mp4 на iOS, не воспроизводится в браузере и т. Д. - PullRequest
0 голосов
/ 14 января 2019

Я хочу конвертировать .mov в .mp4 на iOS.

Я мог бы преобразовать его в .mp4, но не могу воспроизвести преобразованный файл в браузере, таком как Chrome.

Воспроизводится на iOS и Mac Safari.

Вот код для конвертации.

private func encodeVideo(at avAsset: AVURLAsset, completionHandler: ((URL?, Error?) -> Void)?)  {
        let startDate = Date()

        //Create Export session
        guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
            completionHandler?(nil, nil)
            return
        }

        //Creating temp path to save the converted video
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
        let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4")

        //Check if the file already exists then remove the previous file
        if FileManager.default.fileExists(atPath: filePath.path) {
            do {
                try FileManager.default.removeItem(at: filePath)
            } catch {
                completionHandler?(nil, error)
            }
        }

        exportSession.outputURL = filePath
        exportSession.outputFileType = .mp4
        exportSession.shouldOptimizeForNetworkUse = true
        let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
        let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
        exportSession.timeRange = range

        exportSession.exportAsynchronously(completionHandler: {() -> Void in
            switch exportSession.status {
            case .failed:
                print(exportSession.error ?? "NO ERROR")
                completionHandler?(nil, exportSession.error)
            case .cancelled:
                print("Export canceled")
                completionHandler?(nil, nil)
            case .completed:
                //Video conversion finished
                let endDate = Date()

                let time = endDate.timeIntervalSince(startDate)
                print(time)
                print("Successful!")
                print(exportSession.outputURL ?? "NO OUTPUT URL")
                completionHandler?(exportSession.outputURL, nil)

            default:
                break
            }

        })
    }

Как я могу преобразовать его в воспроизводимый mp4 в любом месте?

1 Ответ

0 голосов
/ 14 января 2019

Попробуйте использовать другой AVAssetExportPreset, в соответствии с этим потоком AVAssetExportSession с использованием AVAssetExportPresetPassthrough ломающий вывод имеет известные ошибки совместимости.

Подробнее здесь: https://developer.apple.com/documentation/avfoundation/avassetexportsession

...