用谷歌Colab免费批量将本地电脑上的Mp3语音文件转文字

智能语音交互关系型数据库云安全

首先在谷歌硬盘上传Mp3语音文件

picture.image

可以下载电脑版谷歌drive软件:Google Drive for desktop,使用更方便:

picture.image

音频很快自动同步上传。

接下来使用Openai的whisper模型:https://github.com/openai/whisper

picture.image

在ChatGPT中输入提示词:

你是一个编程高手,写一个谷歌colab的ipynb脚本,实现任务如下:

从huggingface下载Whisper large-v3-turbo语音转录模型文件,然后保存到谷歌Drive中的myaudio文件夹中;

读取谷歌Drive中的myaudio文件目录中所有子文件夹中的音频文件;

从谷歌Drive中调用Whisper large-v3-turbo模型将所有音频文件转录成文字,保存为txt文本文件,txt文件名和音频文件名保持同一个名称,txt文件保存在和音频文件的同一个文件夹中;

注意:

安装pydub库;

安装ffmpeg;

在调用Whisper模型时显式指定语言为英文;

代码示例:

import whisper

model = whisper.load_model("turbo")

result = model.transcribe("audio.mp3")

print(result["text"])

ChatGPT生成的源代码:

安装必要的库

!pip install pydub

!apt-get install ffmpeg

!pip install -q git+https://github.com/openai/whisper.git

!pip install tqdm

import os

import whisper

from tqdm import tqdm

from google.colab import drive

from pydub import AudioSegment

挂载 Google Drive

drive.mount('/content/drive')

下载并加载 Whisper 模型

model = whisper.load_model("large-v3-turbo")

设置音频文件夹路径

audio_folder = '/content/drive/MyDrive/myaudio'

遍历所有子文件夹,找到音频文件

for root, dirs, files in os.walk(audio_folder):

for file in tqdm(files):

if file.endswith(('.mp3', '.wav', '.m4a')): # 支持的音频格式

audio_path = os.path.join(root, file)

print(f"正在转录: {audio_path}")

转录音频并指定语言为英文

result = model.transcribe(audio_path, language="en")

transcript = result['text']

保存转录文本

txt_filename = os.path.splitext(file)[0] + '.txt'

txt_path = os.path.join(root, txt_filename)

with open(txt_path, 'w') as txt_file:

txt_file.write(transcript)

print(f"已保存转录文本: {txt_path}")

打开谷歌colab

https://colab.research.google.com/

picture.image

新建笔记本,输入源代码。然后运行。注意:在免费版Colab 中,笔记本最长可以运行12 小时

picture.image

实测一个28分钟的mp3,在使用CPU的时候,耗时1小时,而如果改用T4 GPU,仅耗时3分钟。所以尽量使用GPU,会提速很多。

picture.image

picture.image

转录完成的文本会自动同步到本地硬盘上。

0
0
0
0
关于作者

文章

0

获赞

0

收藏

0

相关资源
抖音连麦音画质体验提升与进阶实践
随着互娱场景实时互动创新玩法层出不穷,业务伙伴对 RTC「体验」和「稳定」的要求越来越高。火山引擎 RTC 经历了抖音 6 亿 DAU 的严苛验证和打磨,在架构设计、音画质提升、高可靠服务等方面沉淀了丰富的经验,本次演讲将和大家分享火山引擎 RTC 在直播连麦等场景中的技术优化及其带来的新玩法。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论