-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AuYang261/caption
添加自动生成字幕功能
- Loading branch information
Showing
15 changed files
with
224 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ old/ | |
*.exe | ||
build/ | ||
dist/ | ||
*.spec | ||
*.spec | ||
whisper_models/ | ||
release_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import whisper | ||
import time | ||
from zhconv import convert # 简繁体转换 | ||
import sys | ||
import os | ||
|
||
|
||
def seconds_to_hmsm(seconds): | ||
""" | ||
输入一个秒数,输出为H:M:S:M时间格式 | ||
@params: | ||
seconds - Required : 秒 (float) | ||
""" | ||
hours = str(int(seconds // 3600)) | ||
minutes = str(int((seconds % 3600) // 60)) | ||
seconds = seconds % 60 | ||
milliseconds = str(int(int((seconds - int(seconds)) * 1000))) # 毫秒留三位 | ||
seconds = str(int(seconds)) | ||
# 补0 | ||
if len(hours) < 2: | ||
hours = "0" + hours | ||
if len(minutes) < 2: | ||
minutes = "0" + minutes | ||
if len(seconds) < 2: | ||
seconds = "0" + seconds | ||
if len(milliseconds) < 3: | ||
milliseconds = "0" * (3 - len(milliseconds)) + milliseconds | ||
return f"{hours}:{minutes}:{seconds},{milliseconds}" | ||
|
||
|
||
def main(): | ||
# 视频文件路径 | ||
video_paths = [] | ||
if len(sys.argv) >= 2: | ||
video_paths.append(sys.argv[1]) | ||
else: | ||
files = [] | ||
for dirpath, dirnames, filenames in os.walk("."): | ||
for filename in filenames: | ||
if filename.endswith(".mp4"): | ||
files.append(os.path.join(dirpath, filename).replace("\\", "/")) | ||
for i, f in enumerate(files): | ||
print(f"[{i}]: ", f) | ||
input_list = eval( | ||
"[" + input("select a video file by input a num(split with ','): ") + "]" | ||
) | ||
for i in input_list: | ||
video_paths.append(files[i]) | ||
print("selected video files:", video_paths) | ||
models = [] | ||
for model in whisper.available_models(): | ||
if ".en" in model: | ||
continue | ||
print(f"[{len(models)}]: ", model) | ||
models.append(model) | ||
model_index = input("select a model by input a num(default 'base'): ") | ||
try: | ||
model_name = models[eval(model_index)] | ||
except: | ||
model_name = "base" | ||
print("selected model:", model_name) | ||
|
||
for video_path in video_paths: | ||
audio_path = video_path.replace("mp4", "m4a") | ||
cmd = f'ffmpeg -i "{video_path}" -vn -ar {whisper.audio.SAMPLE_RATE} "{audio_path}"' | ||
os.system(cmd) | ||
|
||
model = whisper.load_model(model_name, download_root="whisper_models/") | ||
|
||
start = time.time() | ||
result = model.transcribe(audio_path, verbose=False, language="zh") | ||
print("Time cost: ", time.time() - start) | ||
|
||
# 写入字幕文件 | ||
with open(video_path.replace("mp4", "srt"), "w", encoding="utf-8") as f: | ||
i = 1 | ||
for r in result["segments"]: | ||
f.write(str(i) + "\n") | ||
f.write( | ||
seconds_to_hmsm(float(r["start"])) | ||
+ " --> " | ||
+ seconds_to_hmsm(float(r["end"])) | ||
+ "\n" | ||
) | ||
i += 1 | ||
f.write( | ||
convert(r["text"], "zh-cn") + "\n" | ||
) # 结果可能是繁体,转为简体zh-cn | ||
f.write("\n") | ||
|
||
# 删除音频文件 | ||
os.remove(audio_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from PyInstaller.utils.hooks import collect_data_files | ||
|
||
datas = collect_data_files("whisper") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from PyInstaller.utils.hooks import collect_data_files | ||
|
||
datas = collect_data_files("zhconv") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,55 +6,82 @@ | |
import json | ||
import os | ||
import cProfile | ||
headers={ | ||
'Origin': 'https://www.yanhekt.cn', | ||
|
||
headers = { | ||
"Origin": "https://www.yanhekt.cn", | ||
"xdomain-client": "web_user", | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26' | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26", | ||
} | ||
|
||
|
||
# courseID = 31425 | ||
def main(): | ||
if len(sys.argv) == 1: | ||
courseID = eval(input('Please input course ID: ')) | ||
courseID = eval(input("Please input course ID: ")) | ||
else: | ||
courseID = sys.argv[1] | ||
|
||
course = requests.get(f'https://cbiz.yanhekt.cn/v1/course?id={courseID}&with_professor_badges=true', headers=headers) | ||
req = requests.get(f'https://cbiz.yanhekt.cn/v2/course/session/list?course_id={courseID}', headers=headers) | ||
if course.json()['code'] != '0' and course.json()['code'] != 0: | ||
print(course.json()['code']) | ||
print(course.json()['message']) | ||
raise Exception("Please Check your course ID, note that it should be started with yanhekt.cn/course/***, not yanhekt.cn/session/***") | ||
print(course.json()['data']['name_zh']) | ||
videoList = req.json()['data'] | ||
course = requests.get( | ||
f"https://cbiz.yanhekt.cn/v1/course?id={courseID}&with_professor_badges=true", | ||
headers=headers, | ||
) | ||
req = requests.get( | ||
f"https://cbiz.yanhekt.cn/v2/course/session/list?course_id={courseID}", | ||
headers=headers, | ||
) | ||
if course.json()["code"] != "0" and course.json()["code"] != 0: | ||
print(course.json()["code"]) | ||
print(course.json()["message"]) | ||
raise Exception( | ||
"Please Check your course ID, note that it should be started with yanhekt.cn/course/***, not yanhekt.cn/session/***" | ||
) | ||
print(course.json()["data"]["name_zh"]) | ||
videoList = req.json()["data"] | ||
# print(json.dumps(videoList, indent=2)) | ||
for i, c in enumerate(videoList): | ||
print(i, ":", c['title']) | ||
print(f"[{i}]: ", c["title"]) | ||
|
||
index = eval('[' + input('select(split by \',\', such as: 0,2,4):') + ']') | ||
vga = input('video(1) or screen(2)?(input 1 or 2, default video):') | ||
if not os.path.exists('output/'): | ||
os.mkdir('output/') | ||
index = eval("[" + input("select(split by ',', such as: 0,2,4): ") + "]") | ||
vga = input("video(1) or screen(2)?(input 1 or 2, default video):") | ||
if not os.path.exists("output/"): | ||
os.mkdir("output/") | ||
for i in index: | ||
c = videoList[i] | ||
name = course.json()['data']['name_zh'].strip() + '-' + course.json()['data']['professors'][0]['name'] + '-' + c['title'] | ||
name = ( | ||
course.json()["data"]["name_zh"].strip() | ||
+ "-" | ||
+ course.json()["data"]["professors"][0]["name"] | ||
+ "-" | ||
+ c["title"] | ||
) | ||
print(name) | ||
if vga == "2": | ||
print("Downloading screen...") | ||
m3u8dl.M3u8Download(c['videos'][0]['vga'], 'output/' + course.json()['data']['name_zh'].strip() + '-screen', name) | ||
m3u8dl.M3u8Download( | ||
c["videos"][0]["vga"], | ||
"output/" + course.json()["data"]["name_zh"].strip() + "-screen", | ||
name, | ||
) | ||
else: | ||
print("Downloading video...") | ||
m3u8dl.M3u8Download(c['videos'][0]['main'], 'output/'+ course.json()['data']['name_zh'].strip() + '-video', name) | ||
m3u8dl.M3u8Download( | ||
c["videos"][0]["main"], | ||
"output/" + course.json()["data"]["name_zh"].strip() + "-video", | ||
name, | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
try: | ||
main() | ||
# cProfile.run('main()', 'output/profile.txt') | ||
except Exception as e: | ||
print(e) | ||
print("If the problem is still not solved, you can report an issue in https://github.com/AuYang261/BIT_yanhe_download/issues.") | ||
print( | ||
"If the problem is still not solved, you can report an issue in https://github.com/AuYang261/BIT_yanhe_download/issues." | ||
) | ||
print("Or contact with the author [email protected]. Thanks for your report!") | ||
print("如果问题仍未解决,您可以在https://github.com/AuYang261/BIT_yanhe_download/issues 中报告问题。") | ||
print( | ||
"如果问题仍未解决,您可以在https://github.com/AuYang261/BIT_yanhe_download/issues 中报告问题。" | ||
) | ||
print("或者联系作者[email protected]。感谢您的报告!") |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
openai-whisper | ||
zhconv |