【问题标题】:swift can not save .m3u8 file to galleryswift无法将.m3u8文件保存到图库
【发布时间】:2019-06-13 22:15:39
【问题描述】:

我正在使用下面的方法下载我的视频并将其保存到画廊,使用 .mp4 可以正常工作,但是当更改为 .m3u8 时总是失败。

func downloadVideoLinkAndCreateAsset(_ videoLink: String,_ fileName : String) {

        // use guard to make sure you have a valid url
        guard let videoURL = URL(string: videoLink) else { return }

        guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
        let fileNameToSave = "CiviX_HistoryVideo_\(fileName)"
        // check if the file already exist at the destination folder if you don't want to download it twice
        if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(fileNameToSave).path) {

            // set up your download task
            URLSession.shared.downloadTask(with: videoURL) { (location, response, error) -> Void in

                // use guard to unwrap your optional url
                guard let location = location else { return }

                // create a deatination url with the server response suggested file name
                let destinationURL = documentsDirectoryURL.appendingPathComponent(fileNameToSave)
                print("destinationURL: \(destinationURL)")
                do {

                    try FileManager.default.moveItem(at: location, to: destinationURL)

                    PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in

                        // check if user authorized access photos for your app
                        if authorizationStatus == .authorized {
                            PHPhotoLibrary.shared().performChanges({
                                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                                    if completed {
                                        print("Video asset created")
                                    } else {
                                        print("Video asset create failed: \(error?.localizedDescription)")
                                    }
                            }
                        }
                    })

                } catch { print("file manager error: \(error.localizedDescription)") }

                }.resume()

        } else {
            print("File already exists at destination url")
        }

    }

然后这里是调用方法

let urlString = response.replacingOccurrences(of: "\"", with: "") -> my m3u8 URL

let videoImageUrl = "https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4" -> always success

//TEST MP4 file -> ALWAYS SUCCESS
self.downloadVideoLinkAndCreateAsset(videoImageUrl, "big_buck_bunny_720p_1mb.mp4")

//TEST M3U8 FIlE -> FAIL
self.downloadVideoLinkAndCreateAsset(urlString, history.fileName!) -> fileName format is 'abc.mp4'

MP4 的日志结果


destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/90994674-6C07-47F9-A880-D1A80CDA0C27/文件/CiviX_HistoryVideo_big_buck_bunny_720p_1mb.mp4

-> 已创建视频资产


M3U8 的日志结果


self.downloadVideoLinkAndCreateAsset(urlString, history.fileName!)

destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/DA6ABC38-4E0A-44C7-9C56-8B65F1DC0D4D/文件/CiviX_HistoryVideo_20-1-2019_3h18m32s.mp4

->视频资产创建失败:可选(“操作无法完成。(Cocoa错误-1。)”)


我也尝试使用 .m3u8 扩展名进行保存,但仍然无法正常工作


self.downloadVideoLinkAndCreateAsset(urlString, "TEST_M3U8_FILE.m3u8")

destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/9B42A55B-4E3E-4A20-A0DC-6E1ED22471A2/文件/CiviX_HistoryVideo_TEST_M3U8_FILE.m3u8

->视频资产创建失败:可选(“操作无法完成。(Cocoa错误-1。)”)


【问题讨论】:

    标签: swift gallery hlsl m3u8


    【解决方案1】:

    M3U8 是音频/视频播放列表文件,无法保存到图库。

    【讨论】:

    • 好的,我现在知道问题所在了,但是您知道一些转换 m3u8 -> mp4,...的方法吗?谢谢
    • 同样,m3u8 是一个不包含音频的音频播放列表文件。 MP4 是一个视频文件。我无法将一个转换为另一个。
    • 嗨。我知道,但我的服务器决定使用 m3u8 来减少数据文件的数量。所以我正在尝试使用 m3u8 格式。这对我来说很难:(,即使我有很多搜索。你能给一些建议吗?谢谢
    • @TalCohen 如何将外部本地音轨与 HLS 视频混合?由于视频资产包含 0 个轨道,因此无法混合/创建作品。我的问题是我的 m3u8 仅支持英语音频,所有其他替代语言都是从服务器提供的,并且在访问服务器时可以在本地使用。现在我的播放器必须支持具有相同 HLS 视频的多种语言。我该怎么做?
    • m3u8 可用于列出视频文件,它用于支持 HLS,我们用于在我们的应用中播放视频
    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多