我想将录音从Facebook Messenger转换为文本. 这是使用Facebook的API发送的.mp4文件的示例: rel ="no ://cdn.fbsbx/v/t59.3654-21/15720510_10211855778255994_5430581267814940672_n.mp4/audioclip-1484407992000-3392.mp4?oh = a78286aa96c9dea29e5d07854194801c& oe = 587C3833
I want to convert a sound recording from Facebook Messenger to text. Here is an example of an .mp4 file send using Facebook's API: cdn.fbsbx/v/t59.3654-21/15720510_10211855778255994_5430581267814940672_n.mp4/audioclip-1484407992000-3392.mp4?oh=a78286aa96c9dea29e5d07854194801c&oe=587C3833
因此,此文件仅包含音频(不包括视频),我想将其转换为文本.
So this file includes only audio (not video) and I want to convert it to text.
此外,我想尽快执行此操作,因为我将在几乎实时的应用程序中使用生成的文本(即用户发送.mp4文件,脚本将其转换为文本并显示回去).
Moreover, I want to do it as fast as possible since I'll use the generated text in an almost real-time application (i.e. user sends the .mp4 file, the script translates it to text and shows it back).
我找到了这个示例 github/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py 这是我使用的代码:
I've found this example github/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py and here is the code I use:
import requests import speech_recognition as sr url = 'cdn.fbsbx/v/t59.3654-21/15720510_10211855778255994_5430581267814940672_n.mp4/audioclip-1484407992000-3392.mp4?oh=a78286aa96c9dea29e5d07854194801c&oe=587C3833' r = requests.get(url) with open("test.mp4", "wb") as handle: for data in r.iter_content(): handle.write(data) r = sr.Recognizer() with sr.AudioFile('test.mp4') as source: audio = r.record(source) command = r.recognize_google(audio) print command但是我遇到了这个错误:
But I'm getting this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\Asterios\Anaconda2\lib\site-packages\speech_recognition\__init__.py", line 200, in __enter__ self.audio_reader = aifc.open(aiff_file, "rb") File "C:\Users\Asterios\Anaconda2\lib\aifc.py", line 952, in open return Aifc_read(f) File "C:\Users\Asterios\Anaconda2\lib\aifc.py", line 347, in __init__ self.initfp(f) File "C:\Users\Asterios\Anaconda2\lib\aifc.py", line 298, in initfp chunk = Chunk(file) File "C:\Users\Asterios\Anaconda2\lib\chunk.py", line 63, in __init__ raise EOFError EOFError有什么想法吗?
我想在pythonanywhere的免费计划上运行脚本,所以我不确定如何在该处安装ffmpeg之类的工具.
I want to run the script on the free-plan of pythonanywhere, so I'm not sure how I can install tools like ffmpeg there.
如果您运行上述脚本,则用该URL替换该URL" www.wavsource/snds_2017-01-08_2348563217987237/people/men/about_time.wav ",然后将"mp4"更改为"wav",即可正常运行.因此,可以肯定的是文件格式.
EDIT 2: If you run the above script substituting the url with this one "www.wavsource/snds_2017-01-08_2348563217987237/people/men/about_time.wav" and change 'mp4' to 'wav', the it works fine. So it is for sure something with the file format.
推荐答案最后,我找到了解决方案.我将其张贴在这里,以防将来对某人有所帮助.
Finally I found an solution. I'm posting it here in case it helps someone in the future.
幸运的是,pythonanywhere预先安装了avconv(avconv与ffmpeg相似).
Fortunately, pythonanywhere comes with avconv pre-installed (avconv is similar to ffmpeg).
这是一些有效的代码:
import urllib2 import speech_recognition as sr import subprocess import os url = 'cdn.fbsbx/v/t59.3654-21/15720510_10211855778255994_5430581267814940672_n.mp4/audioclip-1484407992000-3392.mp4?oh=a78286aa96c9dea29e5d07854194801c&oe=587C3833' mp4file = urllib2.urlopen(url) with open("test.mp4", "wb") as handle: handle.write(mp4file.read()) cmdline = ['avconv', '-i', 'test.mp4', '-vn', '-f', 'wav', 'test.wav'] subprocess.call(cmdline) r = sr.Recognizer() with sr.AudioFile('test.wav') as source: audio = r.record(source) command = r.recognize_google(audio) print command os.remove("test.mp4") os.remove("test.wav")在免费计划中,cdn.fbsbx不在pythonanywhere上的站点白名单中,因此我无法使用urllib2下载内容.我联系了他们,他们在1-2小时内将域名添加到了白名单中!
In the free plan, cdn.fbsbx was not on the white list of sites on pythonanywhere so I could not download the content with urllib2. I contacted them and they added the domain to the white list within 1-2 hours!
即使我使用的是免费套餐,也非常感谢并祝贺他们的优质服务.
So a huge thanks and congrats to them for the excellent service even though I'm using the free tier.
更多推荐
将mp4声音转换为python中的文本
发布评论