8

In my iOS application I'm trying to export an mp3 file from the iPod library to the app's documents directory on the device. Currently I'm trying to use AVAssetExportSession but it's not working for mp3 files. It works well for m4a files.

  • Is exporting an mp3 file possible using AVAssetExportSession?

  • What is the appropriate outputFileType for AVAssetExportSession? (AVFileTypeAppleM4A works for m4a files)

Thanks!

SundayMonday
  • 19,147
  • 29
  • 100
  • 154

3 Answers3

1

I am facing the same problem. Unfortunately, non of the iOS frameworks (AVFoundation, CoreMedia, etc) support encoding to MP3.

An answer to a similar question suggest using the Lame Encoder, and another question mentions that some user was able to compile is successfully for iOS ("I have just attempted to build the static library for LAME and confirmed that it 'works'...").

Another alternative would be to go with FFMpeg. It seems like some users have successfully compiled it for iOS 4.3 (see this reference).

Take into account that you may have to pay royalties for encoding MP3. Also, the licenses for FFMpeg/Lame may prevent you from using their code in a closed-source application.

Good luck!

Community
  • 1
  • 1
urish
  • 8,943
  • 8
  • 54
  • 75
  • 3
    This answer is not correct. If you are exporting an MP3 file from the library, it is already encoded, you dont need to encode to mp3 again. I've used QuickTimeMovie as the file type to successfully export mp3 from the library to the file system. – Dermot Aug 18 '12 at 06:05
  • @Dermot can you please share the code . . i am doing the same in Swift but not luck please help – Usama Sadiq Jul 29 '15 at 08:20
  • November 1999: LAME switches from a GPL license to an LGPL license, which allows using it with closed-source applications. – Dalmazio Sep 17 '16 at 00:23
  • It can be done with AVAssetExportSession with QuickTime filetype and Core Audio filetype. You just need to make sure the output file has the proper extension (.mov or .caf) or it won't save. [More details can be found here.](http://stackoverflow.com/questions/37819867/how-can-i-export-a-avplayer-audio-mp3-file-using-avassetexportsession/39542273#39542273) – Dalmazio Sep 17 '16 at 06:21
0

It appears AVAssetExportSession only supports filetypes for mp3 transcoding with com.apple.quicktime-movie (.mov) and com.apple.coreaudio-format (.caf) using the AVAssetExportPresetPassthrough preset. You must also be sure to use one of these file extensions when writing your output file otherwise it won't save.

Supported output filetype and extensions for an mp3 input file are in bold (tested on OS X 10.11.6):

  • com.apple.quicktime-movie (.mov)
  • com.apple.m4a-audio (.m4a)
  • public.mpeg-4 (.mp4)
  • com.apple.m4v-video (.m4v)
  • org.3gpp.adaptive-multi-rate-audio (.amr)
  • com.microsoft.waveform-audio (.wav)
  • public.aiff-audio (.aiff)
  • public.aifc-audio (.aifc)
  • com.apple.coreaudio-format (.caf)
Dalmazio
  • 1,835
  • 2
  • 23
  • 40
0

here code that will help you to export an mp4 from music library

func displayMediaPicker() {
        let mediaPicker = MPMediaPickerController.init(mediaTypes: .anyAudio)
        mediaPicker.delegate = self
        mediaPicker.allowsPickingMultipleItems = false
        mediaPicker.loadView();
        self.present(mediaPicker, animated: true, completion: nil)
    }

func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        //
        self.dismiss(animated:true)

        if mediaItemCollection.count > 0 {

            let mediaItem = mediaItemCollection.items[0]
            let assetURL = mediaItem.value(forProperty: MPMediaItemPropertyAssetURL)
            let mediaAsset = AVURLAsset(url: assetURL as! URL, options: nil)

            let exporter = AVAssetExportSession.init(asset: mediaAsset, presetName: AVAssetExportPresetMediumQuality)
            exporter?.outputFileType = AVFileType.mp4

            let mediaPathToSave = //assign destination path here

            let exportURL = URL(fileURLWithPath: mediaPathToSave)
            exporter?.outputURL = exportURL

            // if incase you need first 30 seconds
            let startTime = CMTimeMake(0, 1)
            let stopTime = CMTimeMake(30, 1)
            let exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime)
            exporter?.timeRange = exportTimeRange

            exporter?.exportAsynchronously(completionHandler: { 
                //
                let status = exporter?.status

                if status == AVAssetExportSessionStatus.completed {

                    print("AVAssetExportSessionStatus successfull")
                    //do further code for exported file here
                }
            })
        }
    }
Usman Nisar
  • 3,031
  • 33
  • 41