Swift-如何将已保存的音频文件对话转换为文本? [英] Swift - How can I convert Saved Audio file conversations to Text?

查看:229
本文介绍了Swift-如何将已保存的音频文件对话转换为文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我致力于语音识别.我使用IOS框架解决了文本到语音和语音到文本的问题.但是现在我想将保存的音频文件对话转换为文本.我该如何解决呢?谢谢您的所有答复.

I work on speech recognition. I solve the text-to-speech and speech-to-text with IOS frameworks. But now i want to convert saved audio file conversations to text. How can i solve this ? Thank you for all replies.

推荐答案

我从事过对我有用的相同工作.

I have worked on same things which are working for me.

我的项目包中有音频文件.因此,我编写了以下代码将音频转换为文本.

I have audio file in my project bundle which. So I have written following code to convert audio to text.

let audioURL = Bundle.main.url(forResource: "Song", withExtension: "mov")

let recognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))
let request = SFSpeechURLRecognitionRequest(url: audioURL!)

request.shouldReportPartialResults = true

if (recognizer?.isAvailable)! {

    recognizer?.recognitionTask(with: request) { result, error in
        guard error == nil else { print("Error: \(error!)"); return }
        guard let result = result else { print("No result!"); return }

        print(result.bestTranscription.formattedString)
    }
} else {
    print("Device doesn't support speech recognition")
}

首先从存储音频文件的位置获取音频URL. 然后使用所需的语言环境创建SFSpeechRecognizer的实例. 创建用于请求recognitionTaskSFSpeechURLRecognitionRequest实例.

First get audio url from where you have store audio file. Then create instance of SFSpeechRecognizer with locale that you have want. Create instance of SFSpeechURLRecognitionRequest which are used to requesting recognitionTask.

recognitionTask将给您结果和错误.结果包含bestTranscription.formattedString. formmatedString是音频文件的测试结果.

recognitionTask will give you result and error. Where result contains bestTranscription.formattedString. formmatedString is your test result of audio file.

如果设置为request.shouldReportPartialResults = true,则将给出每行音频通话的部分结果.

If set request.shouldReportPartialResults = true, this will give your partial result of every line speak in audio.

我希望这会对您有所帮助.

I hope this will help you.

这篇关于Swift-如何将已保存的音频文件对话转换为文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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