2
2
3
3
#===============================================================================
4
4
# 视频处理
5
+ # 视频转换成mp4格式,并将字幕烧制到视频中
5
6
#===============================================================================
6
7
#
7
8
10
11
import time
11
12
12
13
#单集动画(1~10分钟)
13
- src_dir = 'D:\\ tmp\\ video\\ source\\ '
14
- des_dir = 'D:\\ tmp\\ video\\ target\\ '
15
- log_file = r'D:/tmp/video/convert.csv'
16
- commad_line = 'ffmpeg.exe -ss 00:00:02 -i "{0}" -vf subtitles="{1}" "{2}"'
17
- commad_line2 = 'ffmpeg.exe -ss 00:00:02 -i "{0}" "{1}"'
14
+ src_dir = 'D:\\ tmp\\ video-source\\ '
15
+ des_dir = 'D:\\ tmp\\ video-target\\ '
16
+ subtitle_dir = 'D:\\ tmp\\ video-subtitle\\ '
17
+ log_file = r'D:/log.csv'
18
+ commad_line = 'ffmpeg.exe -i "{0}" -vf subtitles=\" {1}\" "{2}"'
19
+ commad_line2 = 'ffmpeg.exe -i "{0}" "{1}"'
20
+
18
21
#commad_line= commad_line.decode("utf-8").encode("gbk")
19
22
20
23
def convert_video (filename ):
21
- # for filename in os.listdir(src_dir):
22
-
23
24
froot , fext = os .path .splitext (filename )
24
25
fext = fext .lower ()
25
26
@@ -34,40 +35,64 @@ def convert_video(filename):
34
35
# 跳过已存在文件
35
36
des_file = des_dir + (froot + ".mp4" )
36
37
if os .path .exists (des_file ):
37
- print ("跳过已存在, " + des_file )
38
+ to_log ("跳过已存在, " + des_file )
38
39
return
39
40
40
41
# 字幕文件
41
- subtitle_file = src_dir + (froot + ".en.srt" )
42
+ subtitle_file = (froot + ".en.srt" )
42
43
43
44
src_file = src_dir + filename
44
45
45
- print (des_file )
46
- print (commad_line .format (src_file , subtitle_file , des_file ))
46
+ """
47
+ Note:
48
+ I think ffmpeg has some problems when it uses the parameter '-vf subtitles ='.
49
+ File directory, single quote, comma, etc. are not allowed in this parameter.
50
+ """
51
+ os .chdir (subtitle_dir ) # ffmpeg提取mkv内的字幕时,有bug!
52
+
47
53
if '.mp4' == fext :
48
54
if os .path .exists (subtitle_file ):
49
- os . system ( commad_line . format ( src_file , subtitle_file , des_file ) )
55
+ run_cmd ( src_file , subtitle_file , des_file )
50
56
to_log ("[mp4有字幕], " + filename )
51
57
else :
52
58
shutil .copy (src_file , des_file )
53
59
to_log ("[mp4无字幕], " + filename )
54
60
elif '.mkv' == fext :
55
- os .chdir (src_dir ) # ffmpeg提取mkv内的字幕时,有bug!
56
- subtitle_file = froot + ".en.srt"
57
61
if not os .path .exists (subtitle_file ):
62
+ # todo
58
63
subtitle_file = filename
64
+ run_cmd (src_file , subtitle_file , des_file )
59
65
to_log ("[mkv视频], " + filename )
60
- os .system (commad_line .format (src_file , subtitle_file , des_file ))
61
66
elif '.webm' == fext :
62
67
if os .path .exists (subtitle_file ):
63
- os . system ( commad_line . format ( src_file , subtitle_file , des_file ) )
68
+ run_cmd ( src_file , subtitle_file , des_file )
64
69
to_log ("[webm有字幕], " + filename )
65
70
else :
66
71
os .system (commad_line2 .format (src_file , des_file ))
67
72
to_log ("[webm无字幕], " + filename )
68
73
else :
69
74
to_log ('[未知文件], ' + filename )
70
75
76
+ #===============================================
77
+ # 执行系统指令
78
+ # Note: 为了规避ffmpeg缺陷,重命名字幕文件。最后再改回文件名
79
+ #===============================================
80
+ def run_cmd (srcpath , subtitlefile , despath ):
81
+ new_name = time .time ()
82
+ os .rename (subtitlefile , new_name )
83
+ os .system (commad_line .format (srcpath , new_name , despath ))
84
+ os .rename (new_name , subtitlefile )
85
+
86
+ #===============================================
87
+ # mkv视频是否含有字幕
88
+ #===============================================
89
+ def mkv_has_subtitle (mkv_file ):
90
+ cmd = "ffmpeg -i \" {0}\" -c copy -map 0:s -f null - -v 0 -hide_banner && echo $? || echo $?"
91
+ code = os .system (cmd .format (mkv_file ))
92
+ # TODO 为调通
93
+ if code == 0 :
94
+ return True
95
+ return False
71
96
72
97
def to_log (line ):
73
98
fname = log_file
@@ -76,7 +101,9 @@ def to_log(line):
76
101
fout .write (line )
77
102
fout .write ('\n ' )
78
103
79
-
104
+ #===============================================
105
+ # 并发任务
106
+ #===============================================
80
107
from multiprocessing import Process , Queue , Pool
81
108
def batch_tast (processes = 10 ):
82
109
""" 并发 """
0 commit comments