title: How to convert .movpkg to mp4
body: <p>I am streaming a video (.m3u8) by using <code>AVPlayer</code></p>
  <pre><code>let url = URL(string: &quot;http:myUrl.m3u8&quot;)
  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()
  </code></pre>
  <p>I need to save the streaming video to gallery. I noticed that in the below delegate saves the caching video path.</p>
  <pre><code>func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
          // Do not move the asset from the download location
          UserDefaults.standard.set(location.relativePath, forKey: &quot;assetPath&quot;)
         
      }
  </code></pre>
  <p>When I trying to get the url path from the UserDefaults by using the below code,</p>
  <pre><code>let path = UserDefaults.standard.value(forKey: &quot;assetPath&quot;)
  </code></pre>
  <p>The result is :</p>
  <blockquote>
  <p>Library/com.apple.UserManagedAssets.s9Giec/video_streaming_title_3E90DD91830B8992.movpkg</p>
  </blockquote>
  <p>I find .movpkg folder structures at the answer for <a href="https://stackoverflow.com/questions/46097856/avplayer-stops-playing-aes-encrypted-offline-hls-video-in-online-mode?answertab=votes#tab-top">this question</a></p>
  <p><img src="https://i.stack.imgur.com/0akbg.png" alt="" /></p>
  <p>the extension is <code>'.movpkg'</code>  How can I convert the video to mp4 and save to Gallery.</p>
  <h2>Note</h2>
  <ul>
  <li><p>The <code>movpkg</code> file contains <code>.frag</code> files. It's acceptable if there is any answer provides a way to convert <code>.frag</code> files to a mp4 file.</p>
  </li>
  <li><p>I can create an <code>AVAsset</code> from <code>.movpkg</code> url so the answer for question &quot;How to convert AVAsset to mp4 file&quot; is maybe acceptable too.</p>
  </li>
  </ul>
  <p>For anyone wants to help, I created a repo here</p>
  <p><a href="https://github.com/trungducc/stackoverflow/tree/movpkg-to-mp4" rel="noreferrer">https://github.com/trungducc/stackoverflow/tree/movpkg-to-mp4</a></p>
  <p>You can try to convert <code>.movpkg</code> 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.</p>