0

When I am downloading .mp4 or .mov player then it will save into device gallery without any error but when I am trying to save .m3u8 video file it will always giving me this error in error local description:

"Optional(Error Domain=NSCocoaErrorDomain Code=-1 \"(null)\")"

Can anyone help me out from where I am getting wrong?

 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
                didFinishDownloadingTo location: URL) {
    // 1
    guard let sourceURL = downloadTask.originalRequest?.url else { return }
    let download = DownloadManager.sharedDownloadManager.activeDownloads[sourceURL]
    DownloadManager.sharedDownloadManager.activeDownloads[sourceURL] = nil
    // 2
    let destinationURL = localFilePath(for: sourceURL)
    print(destinationURL)

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)
    }) { saved, error in
        if saved {
            print("Video saved")
           download?.track.downloaded = true
        }else{
            print("Video not saved")
            download?.track.downloaded = false
        }
    }
}
khusboo suhasini
  • 293
  • 2
  • 16

1 Answers1

0

When you downloads the video you get the video saved to temporary path. When downloads is completes then move the video from temporary path to your app directory. check Here

It have used this code to download the video, this works perfectly fine for me

You just have to pass the video link in the function. I have used the date formatter for naming the video downloaded from server. Because after downloading the first video your old video will replaced if you did not use any naming convention.

func downloadVideoLinkAndCreateAsset(_ videoLink: String) {
    DispatchQueue.main.async {
        self.view.makeToast("Downloading Video", duration: 0.8, position: .center)
    }

    guard let videoURL = URL(string: videoLink) else { return }
    guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

    URLSession.shared.downloadTask(with: videoURL) { (location, response, error) -> Void in
        guard let location = location else { return }
        let date = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm:ss"
        dateFormatter.timeZone = TimeZone(identifier: "GMT")
        let destinationURL = documentsDirectoryURL.appendingPathComponent("\(dateFormatter.string(from: date)).mp4")
        do {
            try FileManager.default.moveItem(at: location, to: destinationURL)
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                    if completed {
                        //Downloaded
                    } else if let error = error {
                        //Error
                    }
            }
        } catch {
            print(error.localizedDescription)
        }
        }.resume()
}

Destination URL is the url of your project directory

  • I have tried this one,But it will give same error : Error Domain=NSCocoaErrorDomain Code=-1 "(null)" Please check with this url to download :https://video.twimg.com/amplify_video/1071077437549744129/pl/u5ZKD47brR_6X5HE.m3u8?tag=9 – khusboo suhasini Dec 26 '18 at 07:50
  • Sorry to replying so late , I have tested with your given url it's throwing the same which you got . But I have also tested on m3u38 type url of my own it works fine. There must be some issue with your url type – VIJAY SINGH RAGHAV Jan 02 '19 at 06:15
  • Thanks for your reply, Okay I am checking with my given URL if any solution is working then I will definitely post here – khusboo suhasini Jan 02 '19 at 06:24