本文目的:
識(shí)別視頻中的音頻生成字幕并合成新的帶字幕的視頻秘豹,win11+anaconda+python3.9環(huán)境
步驟:
- 分離音頻:ffmpeg
- 語(yǔ)音識(shí)別:whisper
- 合成字幕:moviepy
資源連接:
- ffmpeg:https://ffmpeg.org/download.html
- whisper:https://github.com/openai/whisper.git
- ImageMagick:http://www.imagemagick.org/script/download.php#windows
操作步驟
一携御、安裝環(huán)境
提示:如果沒(méi)裝git需要先本地安裝git并添加環(huán)境變量
創(chuàng)建conda環(huán)境
conda create -n yu39 python=3.9
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install -c conda-forge moviepy
pip install git+https://github.com/openai/whisper.git
(重裝)pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git
(如需翻譯)conda install -c auto translate
安裝ffmpeg,ImageMagick
moviepy會(huì)使用到ffmpeg,ImageMagick,下載解壓安裝
兩種方式指定二選一:添加環(huán)境變既绕、修改配置文件啄刹,均精確到exe文件
moviepy配置文件anaconda3\envs\yu38\Lib\site-packages\moviepy\config_defaults.py:
FFMPEG_BINARY = os.getenv('FFMPEG_BINARY', 'ffmpeg-imageio')
IMAGEMAGICK_BINARY = os.getenv('IMAGEMAGICK_BINARY', 'auto-detect')
修改后:
FFMPEG_BINARY=D:\ProgramFiles\ffmpeg-5.1.2-essentials_build\bin\ffmpeg.exe
IMAGEMAGICK_BINARY=D:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe
若添加環(huán)境變量,其變量名為FFMPEG_BINARY凄贩,IMAGEMAGICK_BINARY
二誓军、實(shí)際操作
一、分離音頻
命令方式:
ffmpeg.exe -i E:\est\bb.mp4 E:\est\bb.mp3
ffmpeg.exe -i E:\est\bb.mp4 -vcodec copy -an E:\est\bb.avi
python代碼
# 將mp4文件轉(zhuǎn)為mp3音頻文件并返回其文件路徑,生成路徑仍在原路徑中(需要先下載moviepy庫(kù))
def mp4_to_mp3(path):
try:
video = VideoFileClip(path)
audio = video.audio
# 設(shè)置生成的mp3文件路徑
newPath = path.replace('mp4', 'mp3')
audio.write_audiofile(newPath)
return newPath
except Exception as e:
print(e)
return None
mp4_to_mp3(r'E:\est\bb.mp4')
二怎炊、語(yǔ)音識(shí)別
命令方式:
whisper.exe所在位置:anaconda3\envs\yu38\Scripts\whisper.exe
whisper E:\est\bb.mp3 --model small --language Chinese
--model:tiny谭企、base、small评肆、medium债查、large,準(zhǔn)確率耗時(shí)依次遞增瓜挽,首次執(zhí)行會(huì)自動(dòng)下載
效果:
[00:00.000 --> 00:01.000] 我說(shuō)一個(gè)事實(shí)
[00:01.000 --> 00:03.000] 就是一個(gè)人的思想境界越高
[00:03.000 --> 00:06.000] 那種以人際關(guān)系為目標(biāo)的欲望就會(huì)越低
[00:06.000 --> 00:10.000] 我發(fā)現(xiàn)如果一個(gè)人特別熱衷于社交盹廷、感情、關(guān)系這些
[00:10.000 --> 00:12.000] 而且搞得頭頭是道道的人
[00:12.000 --> 00:14.000] 往往缺乏深刻的認(rèn)知和知識(shí)
[00:14.000 --> 00:16.000] 因?yàn)樗恍枰羁痰囊?jiàn)識(shí)
[00:16.000 --> 00:18.000] 他只需要隨著大溜跟著群體走
[00:18.000 --> 00:20.000] 就可以保證一時(shí)唯有生活安危
[00:20.000 --> 00:23.000] 而事實(shí)上那些特別深刻的道理和見(jiàn)解
[00:23.000 --> 00:25.000] 一般都是需要經(jīng)歷很大的痛苦
[00:25.000 --> 00:28.000] 并且對(duì)其充分的思考之后才能得到的
[00:28.000 --> 00:29.000] 兩個(gè)條件少一個(gè)都不行
[00:29.000 --> 00:33.000] 而這些痛苦和思考基本上都有不合群這一特征
[00:33.000 --> 00:35.000] 不是那種不善良的不合群
[00:35.000 --> 00:37.000] 而是屬于人際關(guān)系技巧的那一種
[00:37.000 --> 00:38.000] 就比如說(shuō)他故意不合群
python代碼:
import whisper
# 語(yǔ)音識(shí)別
model = whisper.load_model("small")
result = model.transcribe(r'E:\est\bb.mp3', language='chinese')
print(result["text"])
# 翻譯
translator = Translator(from_lang="Chinese",to_lang="Japanese")
# 提取字幕[起始時(shí)間久橙,持續(xù)時(shí)間俄占,字幕]
segments = result['segments']
l_subtitle = []
for seg in segments:
start = seg['start']
end = seg['end']
text = seg['text']
# subtitle = [round(start,2), round(end-start, 2), translator.translate(text)]
subtitle = [round(start,2), round(end-start, 2), text]
print(subtitle)
l_subtitle.append(subtitle)
三、合成字幕
from moviepy.editor import *
def videocaption(src_mp4, dst_mp4, subtitle):
video = VideoFileClip(src_mp4)
position = 'bottom'
txts = []
for start, duration, text in subtitle:
txt = (TextClip(text, fontsize=40,font='SimHei', size=(1900, 40),
align='center', color='red')
.set_position(position)
.set_duration(duration).set_start(start))
txts.append(txt)
# 合成字幕
video = CompositeVideoClip([video, *txts])
# 合成音頻
# videos = video.set_audio(AudioFileClip('Python.mp3'))
# 保存視頻淆衷,注意加上參數(shù)audio_codec缸榄,否則音頻無(wú)聲音
video.write_videofile(dst_mp4, audio_codec='mp3')
if __name__ == '__main__':
src_mp4 = r'E:\est\bb_有聲無(wú)字幕.mp4'
dst_mp4 = r'E:\est\bb_有聲有字幕.mp4'
videocaption(src_mp4,dst_mp4,l_subtitle)
三、結(jié)語(yǔ)
本目標(biāo)核心點(diǎn)在用whisper語(yǔ)音轉(zhuǎn)文字