上一篇对Whisper原理和实战进行了讲解,第7次拿到了热榜第一🏆。今天,我们在Whisper的基础上,引入ffmpeg工具对视频的音频进行抽取,再使用Whisper将音频转为文本,通过二阶段法实现视频内容的理解。
FFmpeg是一个开源的跨平台多媒体处理工具,它可以处理音频/视频数据,包括转码、转换格式、分割、合并等操作。
ffmpeg -i input.mp4 -vn -ar 44100 -ac 2 -ab 192k -f mp3 output.mp3
由于FFmpeg不支持pip安装,所以需要使用apt-get
sudo apt-get update && apt-get install ffmpeg
这里与上一篇一样,还是采用transformers的pipeline,首先创建conda环境以及安装transformers
创建并激活conda环境:
conda create -n video2text python=3.11
conda activate video2text
安装transformers库:
pip install transformers -i https://mirrors.cloud.tencent.com/pypi/simple
基于transformers的pipeline会自动进行模型下载,当然,如果您的网速不行,请替换HF_ENDPOINT为国内镜像。
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-medium")
不同尺寸模型参数量、多语言支持情况、需要现存大小以及推理速度如下
首先将ffmpeg命令放入ffmpeg_command,之后采用subprocess库的run方法执行ffmpeg_command内的命令。
输入的视频文件为input_file,输出的音频文件为output_file。
import subprocess
def extract_audio(input_file, output_file):
"""
使用FFmpeg从MP4文件中提取音频并保存为MP3格式。
:param input_file: 输入的MP4文件路径
:param output_file: 输出的MP3文件路径
"""
# 构建FFmpeg命令
ffmpeg_command = [
'ffmpeg', '-i', input_file, '-vn', '-acodec', 'libmp3lame', output_file
]
try:
# 执行命令
subprocess.run(ffmpeg_command, check=True)
print(f"音频已成功从 {input_file} 提取到 {output_file}")
except subprocess.CalledProcessError as e:
print(f"处理错误: {e}")
首先安装ffmpeg-python:
pip install ffmpeg-python -i https://mirrors.cloud.tencent.com/pypi/simple
引入ffmpeg库,一行代码完成音频转文本
import ffmpeg
def extract_audio(input_file, output_file):
"""
使用FFmpeg从MP4文件中提取音频并保存为MP3格式。
:param input_file: 输入的MP4文件路径
:param output_file: 输出的MP3文件路径
"""
try:
# 执行命令
ffmpeg.input(input_file).output(output_file, acodec="libmp3lame", ac=2, ar="44100").run()
print(f"音频已成功从 {input_file} 提取到 {output_file}")
except subprocess.CalledProcessError as e:
print(f"处理错误: {e}")
from transformers import pipeline
def speech2text(speech_file):
transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-medium")
text_dict = transcriber(speech_file)
return text_dict
这里采用pipeline完成openai/whisper-medium的模型下载以及实例化,将音频文件输入实例化的transcriber对象即刻得到文本。
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
from transformers import pipeline
import subprocess
def speech2text(speech_file):
transcriber = pipeline(task="automatic-speech-recognition", model="openai/whisper-medium")
text_dict = transcriber(speech_file)
return text_dict
def extract_audio(input_file, output_file):
"""
使用FFmpeg从MP4文件中提取音频并保存为MP3格式。
:param input_file: 输入的MP4文件路径
:param output_file: 输出的MP3文件路径
"""
# 构建FFmpeg命令
ffmpeg_command = [
'ffmpeg', '-i', input_file, '-vn', '-acodec', 'libmp3lame', output_file
]
try:
# 执行命令
subprocess.run(ffmpeg_command, check=True)
print(f"音频已成功从 {input_file} 提取到 {output_file}")
except subprocess.CalledProcessError as e:
print(f"处理错误: {e}")
import argparse
import json
def main():
parser = argparse.ArgumentParser(description="视频转文本")
parser.add_argument("--video","-v", type=str, help="输入视频文件路径")
parser.add_argument("--audio","-a", type=str, help="输出音频文件路径")
args = parser.parse_args()
print(args)
extract_audio(args.video, args.audio)
text_dict = speech2text(args.audio)
print("视频内的文本是:\n" + text_dict["text"])
#print("视频内的文本是:\n"+ json.dumps(text_dict,indent=4))
if __name__=="__main__":
main()
输出为:
如果想将该服务部署成语音识别API服务,可以参考之前的FastAPI相关文章。
本文在上一篇音频转文本的基础上,引入了视频转音频,这样可以采用二阶段法:先提取音频,再音频转文字的方法完成视频内容理解。之后可以配上LLM对视频内提取的文本进行一系列应用。