forked from viking-man/subtitles_translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg_utils.py
51 lines (40 loc) · 1.5 KB
/
ffmpeg_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
import logging
import ffmpeg
import re
import sys
from pathlib import Path
# windows电脑ffmpeg命令行兼容
def escape_windows_path(path):
# 将单个反斜杠替换为双反斜杠
sub = re.sub(r'\\', r'\\\\', path)
sub = "\'" + sub + "\'"
split = sub.split(":")
if len(split) == 2:
return split[0] + "\\" + ":" + split[1]
return sub
def is_windows():
return sys.platform.startswith('win')
# 通过ffmpeg给视频添加字幕信息
def add_subtitles(video_file, subtitle_file, output_file):
# 使用 ffmpeg.input() 来指定输入文件和字幕文件
input_video = ffmpeg.input(Path(video_file))
input_subtitle = ffmpeg.input(Path(subtitle_file))
# 区分系统
if is_windows():
subtitle_file = escape_windows_path(subtitle_file)
logging.info(f'subtitle_file after transfer->{subtitle_file}')
# 使用 filter() 添加字幕
output = ffmpeg.output(
input_video, # 输入视频文件
input_subtitle, # 输入字幕文件
output_file,
# vcodec='copy', # 视频编解码器,此处保持原样
acodec='copy', # 音频编解码器,此处保持原样
scodec='mov_text', # 字幕编解码器
f='mp4', # 输出文件格式
vf=f'subtitles={subtitle_file}', # 添加字幕滤镜
strict='experimental', # 使用实验性字幕编解码器
)
# 运行 ffmpeg 命令以创建输出文件
ffmpeg.run(output)
logging.info(f'字幕已添加到 {output_file}')