title: Export files from Music Library to Documents directory (Swift 5)
body: <p>In my app I want to export files from Music Library and save them to Documents directory. I already fetch file list from Music Library and I get url of each file. But I still have two problems:</p>
  <ol>
  <li><p>I can export files only in &quot;m4a&quot; format, but I'd like to export it in format what file was in Music Library (mp3, wav, aac or else)</p>
  </li>
  <li><p>How can I save files from Music Library to Documents directory with url?
  My code now looks like this:</p>
  <pre><code> private func exportFile(with assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -&gt; ()) {
       let asset = AVURLAsset(url: assetURL)
       guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
           completionHandler(nil, nil)
           return
       }
  
       let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
           .appendingPathComponent(NSUUID().uuidString)
           .appendingPathExtension(&quot;m4a&quot;)
  
       exporter.outputURL = fileURL
       exporter.outputFileType = AVFileType.m4a
  
       exporter.exportAsynchronously {
           if exporter.status == .completed {
               completionHandler(fileURL, nil)
           } else {
               completionHandler(nil, exporter.error)
           }
       }
  
  func downloadFile(file: MPMediaItem?, callback: @escaping () -&gt; ()) {
       guard let file = file,
             let url = file.url
       else { return }
       exportFile(with: url) { fileURL, error in
           guard let fileURL = fileURL, error == nil else {
               print(error?.localizedDescription)
               return
           }
  
           //Here I'd like to save file to Documents dir
           print(&quot;\(fileURL)&quot;)
       }
       callback()
     }
  </code></pre>
  </li>
  </ol>