python实现音频转文本

网上下载了一堆视频,但是没时间看,想着把视频声音转换成文字,读文字来学习就快多了,

找了一圈没有免费的,还是自己鼓捣一个吧

工具

  • faster-whisper 音频转文本
  • ffmpeg 将视频提取音频保存为wav格式

实现

python 复制代码
from faster_whisper import WhisperModel
import os
import tool
import threading

threads = []
i=0

def parse_autdio(mdoel,audio_url,savePath,language='zh'):
    
    model = WhisperModel('large-v3', device="cuda", compute_type="int8",download_root= os.getcwd()+'./models')
    segments, info = model.transcribe(audio_url, beam_size=5, language=language)
    file_name = os.path.basename(audio_url)
    noextname, ext = os.path.splitext(file_name)
    textname = noextname+'.txt'
    saveName = os.path.join(savePath,textname)
    with open(saveName,'w', encoding="utf-8") as f:
        for segment in segments:
            print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
            f.write(segment.text+'\n')

        f.close()    


def parse_dir(dir_path,savePath):
    for file in  os.listdir(dir_path):
        full_path = os.path.join(dir_path,file)
        if(os.path.isdir(full_path)):
            parse_dir(full_path,savePath)
        else:
           for i in range(3):
                t = threading.Thread(target=parse_file,args=(full_path,savePath))
                threads.append(t)
        #    return
           parse_file(full_path,savePath)    

def parse_file(file_path,savePath):
        print('当前转换文件:',file_path)
        # audio_file = open(file_path)
        # 如果是mp4
        file_name = os.path.basename(file_path)
        noextname, ext = os.path.splitext(file_name)
        ext = ext.lower()
        # 如果是视频,先分离
        if(not os.path.exists(savePath)):
            os.makedirs(savePath)
        wav_file = os.path.join(savePath, f'{noextname}.wav')
        text_file = os.path.join(savePath, f'{noextname}.txt')
        if(os.path.exists(wav_file) and os.path.exists(text_file)):
            print('------------------------文件已经转换过------------------------------------------------------------------------------------------')
            return
        if ext in ['.mp4', '.mov', '.avi', '.mkv', '.mpeg', '.mp3', '.flac']:
            video_file = file_path
            
            params = [
                "-i",
                video_file,
            ]
            print(wav_file,'fff_____________')
            if ext not in ['.mp3', '.flac']:
                params.append('-vn')
            params.append(wav_file)
            rs = tool.runffmpeg(params)
            parse_autdio('base',wav_file,savePath)
       

path = 'G:/学习视频/手把手教你零基础写作,业余时间月入过万'
savePath = 'G:/学习视频/手把手教你零基础写作,业余时间月入过万/视频字幕'

parse_dir(path,savePath)

for t in threads:
    t.start()


for t in threads:
    t.join()    

使用GPU需要配置一下cuda,另外使用large-v3模型识别速度回慢一些,识别准确率回高一些

如果嫌慢可以使用base模型,识别速度会快不少,但是准确率可能会没那么高

相关推荐
XIE39240 分钟前
Browser-use使用教程
python
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼2 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
蹦蹦跳跳真可爱5892 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
喵先生!3 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ3 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics4 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
byte轻骑兵4 小时前
【HFP】蓝牙HFP协议中音频连接转移与拨号功能的深度解析
音视频·蓝牙技术·hfp
MinggeQingchun5 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
yuanManGan6 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++