如何从 iOS 设备的 iPod 库导出 mp3 文件?

发布于 2024-11-27 18:53:39 字数 390 浏览 2 评论 0原文

在我的 iOS 应用程序中,我尝试将 mp3 文件从 iPod 库导出到设备上应用程序的文档目录。目前我正在尝试使用 AVAssetExportSession 但它不适用于 mp3 文件。它适用于 m4a 文件。

  • 是否可以使用 AVAssetExportSession 导出 mp3 文件?

  • 适合 AVAssetExportSession 的 outputFileType 是什么? (AVFileTypeAppleM4A 适用于 m4a 文件)

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九公里浅绿 2024-12-04 18:53:39

我面临着同样的问题。不幸的是,没有一个 iOS 框架(AVFoundation、CoreMedia 等)支持 MP3 编码。

类似问题的答案建议使用Lame 编码器另一个问题提到某些用户能够成功编译对于 iOS(“我刚刚尝试为 LAME 构建静态库并确认它‘有效’......”)。

另一种选择是使用 FFMpeg。似乎有些用户已经成功地为 iOS 4.3 编译了它(请参阅此参考)。

请考虑到您可能需要支付 MP3 编码的版税。此外,FFMpeg/Lame 的许可证可能会阻止您在闭源应用程序中使用其代码。

祝你好运!

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!

梦年海沫深 2024-12-04 18:53:39

看来 AVAssetExportSession 仅支持使用 com.apple.quicktime-movie (.mov) 和 com.apple.coreaudio-format 进行 mp3 转码的文件类型(.caf) 使用 AVAssetExportPresetPassthrough 预设。您还必须确保在写入输出文件时使用这些文件扩展名之一,否则它将无法保存。

mp3 输入文件支持的输出文件类型和扩展名以粗体显示(在 OS X 10.11.6 上测试):

  • com.apple.quicktime-movie (.mov)
  • com.apple.m4a-audio (. m4a)
  • public.mpeg-4 (.mp4)
  • com.apple.m4v-视频 (.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)

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)
毁梦 2024-12-04 18:53:39

这里的代码将帮助您从音乐库导出 mp4

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
                }
            })
        }
    }

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
                }
            })
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文