首先在谷歌硬盘上传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
drive.mount('/content/drive')
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,会提速很多。
转录完成的文本会自动同步到本地硬盘上。