如何从MKV中获取视频和音频? (迅速)

发布于 2025-01-14 17:37:31 字数 1478 浏览 1 评论 0原文

当我添加 MKV 格式的文件时,我想将 MKV (Matroska) 快速转换为 MP4,但

我的代码在 第 8 行 处中断,我应该怎么解决这个问题?

这是我的代码:

 let composition = AVMutableComposition()   

            do {
                let sourceUrl = Bundle.main.url(forResource: "sample", withExtension: "mov")! 
                let asset = AVURLAsset(url: sourceUrl)
8 ->here        guard let videoAssetTrack = asset.tracks(withMediaType: AVMediaType.video).first else { return } 
                guard let audioCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid) else { return }
                try audioCompositionTrack.insertTimeRange(videoAssetTrack.timeRange, of: videoAssetTrack, at: CMTime.zero)
            } catch {
                print(error)
            }

            // Create an export session
            let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)!
            exportSession.outputFileType = AVFileType.mp4
            exportSession.outputURL = browseURL

            // Export file
            exportSession.exportAsynchronously {
                guard case exportSession.status = AVAssetExportSession.Status.completed else { return }

                DispatchQueue.main.async {
                    // Present a UIActivityViewController to share audio file
                   print("completed")
                }
            }

i want to convert MKV (Matroska) to MP4 in swift

when i add a file with MKV format my code break in line 8 , what should i do to fix that?

this is my code:

 let composition = AVMutableComposition()   

            do {
                let sourceUrl = Bundle.main.url(forResource: "sample", withExtension: "mov")! 
                let asset = AVURLAsset(url: sourceUrl)
8 ->here        guard let videoAssetTrack = asset.tracks(withMediaType: AVMediaType.video).first else { return } 
                guard let audioCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid) else { return }
                try audioCompositionTrack.insertTimeRange(videoAssetTrack.timeRange, of: videoAssetTrack, at: CMTime.zero)
            } catch {
                print(error)
            }

            // Create an export session
            let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)!
            exportSession.outputFileType = AVFileType.mp4
            exportSession.outputURL = browseURL

            // Export file
            exportSession.exportAsynchronously {
                guard case exportSession.status = AVAssetExportSession.Status.completed else { return }

                DispatchQueue.main.async {
                    // Present a UIActivityViewController to share audio file
                   print("completed")
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文