Closed dinghaol closed 5 months ago
这是我的代码希望对你有所帮助
import json
import requests
from moviepy.video.io.VideoFileClip import VideoFileClip
from tool.file.fileBasic import listFileSort
import os
import shutil
from tool.file.txt.TxtWriter import BasicWriter
def videoToAudio(file):
# 请求地址
url = "http://127.0.0.1:9977/api"
# 请求参数 file:音视频文件,language:语言代码,model:模型,response_format:text|json|srt
# 返回 code==0 成功,其他失败,msg==成功为ok,其他失败原因,data=识别后返回文字
files = {"file": open(file, "rb")}
data = {"language": "zh", "model": "medium", "response_format": "text"}
response = requests.request("POST", url, timeout=600, data=data, files=files)
text = json.loads(response.text)["data"]
return bytes(text, 'unicode_escape').decode('unicode_escape')
def split_video(input_path, output_prefix, segment_duration):
# 打开视频文件
video_clip = VideoFileClip(input_path)
# 获取视频总时长
total_duration = video_clip.duration
# 计算分割点
split_points = [t for t in range(0, int(total_duration), segment_duration)]
# 分割视频
for i, start_time in enumerate(split_points):
end_time = min(start_time + segment_duration, total_duration)
subclip = video_clip.subclip(start_time, end_time)
subclip.write_videofile(f"{output_prefix}_part_{i + 1}.mp4")
# 关闭视频文件
video_clip.close()
def delete_all_files_in_folder(folder_path):
# 遍历文件夹下的所有文件和子文件夹
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
# 如果是文件,则删除
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted file: {file_path}")
# 如果是子文件夹,则递归调用删除
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
print(f"Deleted folder: {file_path}")
if __name__ == '__main__':
path = r"G:\temp"
files = listFileSort(path)
for file in files:
if os.path.isfile(file):
temp = file.replace("\\", "/")
split_video(file, path + r"\temp\temp", 300)
text = ''
for video in os.listdir(path + r"\temp"):
temp = videoToAudio(path + r"\temp\\" + video)
text = text + temp
print(temp)
BasicWriter(file.replace("mp4", "txt"), text, "w")
delete_all_files_in_folder(path + r"\temp")
delete_all_files_in_folder(r"F:\software\videoToText\sttv-0.91\static\tmp")
内部已使用VAD自动按静音分割,应不会存在这个问题 只是vad消耗资源较多,如果视频很大而资源不足,可能出问题
10分钟的效果会比5分钟,或者30分钟要好吗,还是其他的说法?
还想说将英文电影识别翻译字幕来着,能支持到多大时长的?
只要内存足够、显存足够,多大都可以。
无法读取是资源不足。尤其是使用 large大模型时,显示至少要8G以上
对于上面的问题,我说一下我的解决方法: 1、将一个视频分为10min的小段 2、将这些小段一一进行音频读取 3、将读取的文字进行结合后输出 ps:如果觉得进行截取的时候,会在截取处,将音频的文字错误读取。那么可以在截取的那个时间点再进行一次10秒左右的截取,并将读取到文字与附近的文字进行缝合防止错误