首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS上从mov转换为mp4的视频无法在浏览器等上播放

在iOS上从mov转换为mp4的视频无法在浏览器等上播放
EN

Stack Overflow用户
提问于 2019-01-14 17:43:17
回答 1查看 706关注 0票数 0

我想在iOS上将.mov转换为.mp4

我可以将其转换为.mp4格式,但无法在Chrome等浏览器上播放转换后的文件。

可在iOS和mac Safari上播放。

下面是转换的代码。

代码语言:javascript
复制
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 anywhere?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-14 18:13:34

尝试使用其他AVAssetExportPreset,因为根据此线程AVAssetExportSession using AVAssetExportPresetPassthrough breaking output具有已知的兼容性错误。

点击此处阅读更多信息:https://developer.apple.com/documentation/avfoundation/avassetexportsession

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54178940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档