

















前几天分享了使用Whisper批量转换视频中的文字分享了如何学ai的1和2,今天继续接下来的内容,今天来分享第三部分的内容。
3、Python实现(调用cli命令,最终版)
通过cli命令是因为它和客户端版是一起的,而客户端版使用了gpu,并且输出效果很好。
3.1 基本配置
下载地址就是上面的那个,cli文件,但是解压以后会发现,它的名字居然叫 main.exe,有点不能忍啊。
反正windows程序,我们简单理解为,在命令行能直接执行的,就在Path里面设置好就行了。
解压到一个地方,把它名字改了。

它的路径放在c盘下面,加到path里面就可以了

这个操作有点复杂,意思就是找到系统属性就行了,不同的操作系统,大同小异,基本都是这样
这样弄好了以后,就可以在命令行里面测试了

3.2 实现代码
# -*- coding: utf-8 -*-
import os
import subprocess
import time
from tqdm import tqdm
video_directory = ''
ffmpeg_command = 'ffmpeg -i "{}" -f wav -vn "{}"'
whisper_command = 'whispercli -gpu "NVIDIA GeForce GTX 1050 Ti" -nt -m "C:\\Program Files\\whispercli\\ggml-large.bin" -l zh -nt -otxt -f "{}"'
# 使用FFmpeg将视频转换为音频
def convert_video_to_audio(video_path, audio_path, video_name):
ffmpeg_output = subprocess.check_output(
ffmpeg_command.format(video_path, audio_path),
shell=True,
stderr=subprocess.DEVNULL, # 阻止FFmpeg输出显示在终端上
)
# 使用Whisper将音频转换为文字
def gen_audio_txt(audio_path, video_name):
# 不指定文件名,自动就是同名的txt
whisper_output = subprocess.check_output(
whisper_command.format( audio_path),
shell=True,
encoding='utf-8'
)
# 这里用来处理视频文件,生成文件
def process_video():
start_time = time.time()
# 遍历视频文件目录中的所有视频文件
n = 0
video_files = [f for f in os.listdir(video_directory) if f.endswith((".mp4", ".avi", ".mkv", ".flv", ".mov"))]
for video_file in tqdm(video_files, desc='正在处理视频文件 '):
# 获取视频文件路径和文件名
video_path = os.path.join(video_directory, video_file)
video_name = os.path.splitext(video_file)[0]
# 定义音频文件路径
audio_path = os.path.join(video_directory, video_name + '.wav')
# 定义txt文件路径
txt_path = os.path.join(video_directory, video_name + '.txt')
# 检查txt文件是否已存在,如果存在则跳过当前视频文件
if os.path.exists(txt_path):
print(f"跳过视频文件 【{video_file}】, 对应的文案txt文件已经存在.")
continue
# 使用FFmpeg将视频转换为音频
convert_video_to_audio(video_path, audio_path, video_name)
# 使用Whisper将音频转换为文字
gen_audio_txt(audio_path, video_name)
os.remove(audio_path)
n = n + 1
end_time = time.time()
print("一共 {:d}个视频,共耗时: {:.2f}秒".format(n, end_time - start_time))
if __name__ == '__main__':
path = ''
while True:
path = input("输入包含视频文件的目录: ")
if os.path.exists(path) :
break
else:
print(f'{path}文件不存在,可能是路径不对')
video_directory = path
# 开始处理文件
process_video()
运行效果如下:


3.3 命令说明
基本使用方法如下
whispercli.exe [options] file0.wav file1.wav …
我们使用命令行参数带 –help 的时候,比较特别的是,第3列代表着当前的值,也许是我们上次执行之后留下来的值,不知道它保存在哪里,有时候确实会轻松一点
简写 完整写法 当前值 说明
-h, –help [default] show this help message and exit
-la, –list-adapters 系统中当前的显卡名,给后面的参数用
-gpu, –use-gpu 使用gpu加速,这里后面跟的是显卡的名字,
-t N, –threads N [4 ] number of threads to use during computation
-p N, –processors N [1 ] number of processors to use during computation
-ot N, –offset-t N [0 ] time offset in milliseconds
-on N, –offset-n N [0 ] segment index offset
-d N, –duration N [0 ] duration of audio to process in milliseconds
-mc N, –max-context N [-1 ] maximum number of text context tokens to store
-ml N, –max-len N [0 ] maximum segment length in characters
-wt N, –word-thold N [0.01 ] word timestamp probability threshold
-su, –speed-up [false ] speed up audio by x2 (reduced accuracy)
-tr, –translate [false ] 从原始语音翻译成英文
-di, –diarize [false ] stereo audio diarization
-otxt, –output-txt [false ] 以txt的方式输出,说白了就是没有时间轴信息了,这个符合我的需求
-ovtt, –output-vtt [false ] output result in a vtt file
-osrt, –output-srt [false ] 输出格式是srt,就是时间轴的那个
-owts, –output-words [false ] output script for generating karaoke video
-ps, –print-special [false ] print special tokens
-nc, –no-colors [false ] do not print colors
-nt, –no-timestamps [false ] 不要输出时间轴信息,默认是关闭的,就是一行信息,最前面是时间
-l LANG, –language LANG [en ] 这里指的是输入的音频文件,讲的是啥语音,用的是zh
-m FNAME, –model FNAME [models/ggml-base.en.bin] model path
-f FNAME, –file FNAME [ ] 输入的文件名,这里大家可以看到,它是音频文件不是视频问题,所以需要转换
lang 有时候出来是繁体中文,但是都用的是zh
3.4 ffmpeg 一起
同理,ffmpeg也是这样实现的。它的命令更复杂更丰富,这里主要是考虑把mp4文件转换成音频文件


这里的 :
-i 表示输入文件名
-f 输出文件格式
-vn 输出文件名,这个说法不准确,不过好理解
更复杂的需求可以进一步去了解,东西还是挺多的
之所以选择命令行方式,一个很大的原因是,最开始选择直接用python的时候,无法使用gpu,尝试几个方案都不行,时不时还报错
今天AI文章就到这里,我们接下来的日子会继续把后面的内容分享给大家。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。