首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >用谷歌Colab免费批量将本地电脑上的Mp3语音文件转文字

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

作者头像
AIGC部落
发布2024-10-21 10:47:36
发布2024-10-21 10:47:36
9030
举报
文章被收录于专栏:Dance with GenAIDance with GenAI

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

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

音频很快自动同步上传。

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

在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/

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

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-10-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Dance with GenAI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档