如何使用 Swift 在 iOS 中将音频格式从 mp3 转换为 wav? [英] How do I convert audio format from mp3 to wav in iOS using Swift?

查看:81
本文介绍了如何使用 Swift 在 iOS 中将音频格式从 mp3 转换为 wav?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iOS 中使用 Swift 使用 cmusphinx,cmusphinx 使用 wav 格式的音频文件,而我需要使用的文件是 mp3 格式.

I am using cmusphinx in iOS using Swift, and cmusphinx uses audio files in wav format, while the files I need to use are in mp3 format.

如何在 iOS 中使用 Swift 将 mp3 格式的音频文件转换为 wav 格式?

How do I convert audio files of mp3 format to wav format in iOS using Swift?

到目前为止,我有以下代码使用 Apple AVFoundation 框架中的 AVAssetExportSession,但我不断收到错误消息,指出我使用了无效的输出文件类型,无论我如何设置 AVAssetExportSession 对象的 outputFileType 属性.

So far I have the following code using AVAssetExportSession from the Apple AVFoundation framework, but I keep getting an error saying I used an invalid output file type, no matter what I set the outputFileType property of the AVAssetExportSession object.

我愿意使用 Apple 以外的其他框架.

I am open to using other frameworks other than from Apple.

错误发生在我用注释标记此处出错"的地方,其中设置了 AVAssetExportSession.outputFileType 实例属性.

The error occurs where I mark it with a comment that says "error here", where the AVAssetExportSession.outputFileType instance property is set.

override func viewDidLoad() {
    super.viewDidLoad()

    let assetURL: URL = Bundle.main.url(forResource: "audio16000", withExtension: "mp3")!

    let asset = AVAsset(url: assetURL)

    let presets: [String] = AVAssetExportSession.allExportPresets()

    for preset in presets {
        print(preset)
    }

    let localDocumentsURL: URL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

    let outputURL: URL = localDocumentsURL.appendingPathComponent("audio16000").appendingPathExtension("wav")

    if FileManager.default.fileExists(atPath: outputURL.path) {

        do {

            try FileManager.default.removeItem(at: outputURL)

        } catch {

            print(error)

        }

    }

    let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHEVC1920x1080)

    exporter?.outputURL = outputURL
    exporter?.outputFileType = AVFileType.wav // error here

    exporter?.exportAsynchronously {

        print("exporter status =", exporter?.status as Any)

        switch exporter!.status {
        case .unknown:
            print("status unknown")
        case .waiting:
            print("status waiting")
        case .exporting:
            print("status exporting")
        case .completed:
            print("status completed")
        case .failed:
            print("status failed")
        case .cancelled:
            print("status cancelled")
        @unknown default:
            print("@unknown default:")
        }

    }

}

以下是调试窗口的内容:

Here's what the debug window says:

2019-09-17 11:42:14.766703-0500 TrialAVAssetExportSession[5620:2016827] [Accessibility] ****************** Loading GAX Client Bundle ****************
AVAssetExportPreset1920x1080
AVAssetExportPresetLowQuality
AVAssetExportPresetAppleM4A
AVAssetExportPresetHEVCHighestQuality
AVAssetExportPreset640x480
AVAssetExportPreset3840x2160
AVAssetExportPresetHEVC3840x2160
AVAssetExportPresetHighestQuality
AVAssetExportPresetMediumQuality
AVAssetExportPreset1280x720
AVAssetExportPreset960x540
AVAssetExportPresetHEVC1920x1080
2019-09-17 11:42:25.574394-0500 TrialAVAssetExportSession[5620:2016827] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVAssetExportSession setOutputFileType:] Invalid output file type'
*** First throw call stack:
(0x233d4a98c 0x232f239f8 0x239d527f4 0x1004d2f58 0x1004d40f0 0x2601fe224 0x2601fe628 0x2607dce64 0x2607dd40c 0x253f82620 0x2607edce8 0x2607a0908 0x2607a5fe0 0x2600692a4 0x26007183c 0x260068f28 0x260069818 0x260067b64 0x26006782c 0x26006c36c 0x26006d150 0x26006c224 0x260070f24 0x2607a45e8 0x2603a0e04 0x2366c69fc 0x2366d040c 0x2366cfc14 0x100584c78 0x100588840 0x236701040 0x236700cdc 0x236701294 0x233cdc728 0x233cdc6a8 0x233cdbf90 0x233cd6ecc 0x233cd67c0 0x235ed779c 0x2607a7c38 0x1004d5594 0x23379a8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我已经尝试了从 AVAssetExportSession.exportPresets(compatibleWith:) 方法收到的所有预设.当我使用 AVAssetExportSession.allExportPresets() 时,我收到完全相同的字符串数组.

I've tried all the presets I received from the AVAssetExportSession.exportPresets(compatibleWith:) method. I receive the same exact array of strings when I use the AVAssetExportSession.allExportPresets().

推荐答案

WAV和包含PCM,看Core Audio,具体:

WAV and contain PCM, look at Core Audio, specifically:

  1. 音频文件服务(读取 MP3 格式和写入 AIFF 或 WAV)
  2. 音频文件转换服务(将 MP3 数据转换为 PCM,和/或从 PCM 编码到其他一些编解码器,如果你要写一个文件).请注意,给定的转换器无法在两种编码格式之间进行转换.您可以将 MP3 转 PCM 或 PCM 转 AAC,但要执行 MP3 转 AAC,您需要两个转换器.
  3. 扩展音频文件服务,结合了上述两种服务.

此外,请务必了解编解码器和文件格式之间的区别,以及哪些编解码器/格式组合是合法的.当我第一次发现 PCM 在 WAV 中必须是小端,而在 AIFF 中必须是大端时,我感到很惊讶.

Also, be sure to understand the difference between codecs and file formats, and what codec/format combinations are legal. I was surprised the first time I found out that PCM must be little-endian in a WAV, big-endian in an AIFF.

此处查看示例 取自 AudioKit

这篇关于如何使用 Swift 在 iOS 中将音频格式从 mp3 转换为 wav?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆