swift无法将.m3u8文件保存到图库 [英] swift can not save .m3u8 file to gallery
问题描述
我使用下面的方法将视频下载并保存到图库中,使用.mp4可以正常工作,但是更改为.m3u8总是失败.
Im using this below method to download and save my video to gallery, with .mp4 it's work normally, but when change to .m3u8 it's always fail.
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的日志结果
目标网址: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
destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/90994674-6C07-47F9-A880-D1A80CDA0C27/Documents/CiviX_HistoryVideo_big_buck_bunny_720p_1mb.mp4
->创建了视频资产
M3U8的日志结果
self.downloadVideoLinkAndCreateAsset(urlString,history.fileName!)
self.downloadVideoLinkAndCreateAsset(urlString, history.fileName!)
目标URL: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
destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/DA6ABC38-4E0A-44C7-9C56-8B65F1DC0D4D/Documents/CiviX_HistoryVideo_20-1-2019_3h18m32s.mp4
->视频资产创建失败:可选(操作无法完成.(可可错误-1.)")
-> Video asset create failed: Optional("The operation couldn’t be completed. (Cocoa error -1.)")
我也尝试使用.m3u8扩展名保存,但仍然无法正常工作
self.downloadVideoLinkAndCreateAsset(urlString,"TEST_M3U8_FILE.m3u8")
self.downloadVideoLinkAndCreateAsset(urlString, "TEST_M3U8_FILE.m3u8")
目标网址: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
destinationURL: file:///Users/thehe/Library/Developer/CoreSimulator/Devices/05C6DE76-6609-4E4A-B00D-2CE3622D2EF8/data/Containers/Data/Application/9B42A55B-4E3E-4A20-A0DC-6E1ED22471A2/Documents/CiviX_HistoryVideo_TEST_M3U8_FILE.m3u8
->视频资产创建失败:可选(操作无法完成.(可可错误-1.)")
-> Video asset create failed: Optional("The operation couldn’t be completed. (Cocoa error -1.)")
推荐答案
M3U8是音频/视频播放列表文件,不能保存到图库中.
M3U8 is an audio/video playlist file and it cannot be saved to the gallery.
这篇关于swift无法将.m3u8文件保存到图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!