I am streaming a video (.m3u8) by using AVPlayer
let url = URL(string: "http:myUrl.m3u8")
let player = AVPlayer(url: url!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = videoView.bounds
playerLayer.backgroundColor = UIColor.purple.cgColor
videoView.layer.addSublayer(playerLayer)
player.play()
I need to save the streaming video to gallery. I noticed that in the below delegate saves the caching video path.
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
// Do not move the asset from the download location
UserDefaults.standard.set(location.relativePath, forKey: "assetPath")
}
When I trying to get the url path from the UserDefaults by using the below code,
let path = UserDefaults.standard.value(forKey: "assetPath")
The result is :
Library/com.apple.UserManagedAssets.s9Giec/video_streaming_title_3E90DD91830B8992.movpkg
I find .movpkg folder structures at the answer for this question

the extension is '.movpkg' How can I convert the video to mp4 and save to Gallery.
Note
The
movpkgfile contains.fragfiles. It's acceptable if there is any answer provides a way to convert.fragfiles to a mp4 file.I can create an
AVAssetfrom.movpkgurl so the answer for question "How to convert AVAsset to mp4 file" is maybe acceptable too.
For anyone wants to help, I created a repo here
https://github.com/trungducc/stackoverflow/tree/movpkg-to-mp4
You can try to convert .movpkg file to mp4 after download is finished. Note that downloading HLS streams is not supported in the simulator so please run the repo on real device.