-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
72 lines (63 loc) · 2.14 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import ffmpeg
import os
import json
def extract_audio(video_path, output_audio_path):
"""
从视频文件中提取音频并保存为wav。
参数:
video_path (str): 视频文件的路径。
output_audio_path (str): 输出音频文件的路径。
"""
if not os.path.exists(video_path):
raise "{} not find".format(video_path)
if os.path.exists(output_audio_path):
os.remove(output_audio_path)
try:
(
ffmpeg
.input(video_path)
.output(output_audio_path, acodec='mp3', audio_bitrate='320k')
.run(overwrite_output=True)
)
except ffmpeg.Error as e:
raise e
def merge_subtitles_to_video(video_path, subtitle_path, output_video_path):
"""
将字幕文件合并到视频文件中。
参数:
video_path (str): 视频文件的路径。
subtitle_path (str): 字幕文件的路径。
output_video_path (str): 合并字幕后的输出视频文件的路径。
"""
if not os.path.exists(video_path):
raise FileNotFoundError(f"{video_path} not found")
if not os.path.exists(subtitle_path):
raise FileNotFoundError(f"{subtitle_path} not found")
if os.path.exists(output_video_path):
os.remove(output_video_path)
subtitle_path = subtitle_path.replace("\\", "/")
print("subtitle_path = {}".format(subtitle_path))
try:
(
ffmpeg
.input(video_path)
.output(output_video_path, vf=f"subtitles={subtitle_path}")
.run(overwrite_output=True)
)
except ffmpeg.Error as e:
raise RuntimeError(f"Failed to merge subtitles into video: {e}")
def clear_folder(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
os.remove(file_path)
print("清空文件夹:{}".format(folder_path))
def import_config_file(file):
if file is not None:
content = file.read()
try:
json_data = json.loads(content)
return json_data
except Exception as e:
raise e
if __name__ == "__main__":
pass