2
2
3
3
import os
4
4
import subprocess
5
- from src .config import DanmakuFactory_PATH
6
5
from src .log .logger import scan_log
7
6
from .adjust_price import update_danmaku_prices
8
- from .remove_emojis import remove_emojis
7
+ from .DanmakuConvert . dmconvert import convert_xml_to_ass
9
8
10
9
11
10
def get_resolution (in_video_path ):
@@ -19,69 +18,60 @@ def get_resolution(in_video_path):
19
18
# Use ffprobe to acquire the video resolution
20
19
result = subprocess .run (
21
20
['ffprobe' , '-v' , 'error' , '-select_streams' , 'v:0' , '-show_entries' , 'stream=width,height' , '-of' , 'csv=s=x:p=0' , in_video_path ],
22
- stdout = subprocess .PIPE ,
23
- stderr = subprocess .PIPE ,
21
+ check = True ,
24
22
text = True ,
25
- check = True
23
+ capture_output = True
26
24
)
25
+ scan_log .debug (f"get_resolution FFmpeg output: { result .stdout } " )
26
+ if result .stderr :
27
+ scan_log .debug (f"get_resolution FFmpeg debug: { result .stderr } " )
27
28
resolution = result .stdout .strip ()
28
- scan_log .info ("The video resolution is " + resolution )
29
- return resolution
29
+ resolution_x , resolution_y = map (int , resolution .split ('x' ))
30
+ scan_log .info (f"The video resolution x is { resolution_x } and y is { resolution_y } " )
31
+ return resolution_x , resolution_y
30
32
except subprocess .CalledProcessError as e :
31
- scan_log .error (f"Error: { e .stderr } " )
32
- return '1920x1080'
33
+ scan_log .error (f"get_resolution Error: { e .stderr } " )
34
+ return 1920 , 1080
33
35
34
- def process_danmakus (in_xml_path , resolution ):
36
+ def process_danmakus (in_xml_path , resolution_x , resolution_y ):
35
37
"""Generate and process the danmakus according to different resolution.
36
38
Args:
37
39
in_xml_path: str, the xml path to generate ass file
38
- resolution: str, the resolution of the video
40
+ resolution_x: int, the x resolution of the video
41
+ resolution_y: int, the y resolution of the video
39
42
Return:
40
43
subtitle_font_size: str, the font size of subtitles
44
+ subtitle_margin_v: str, the margin v of subtitles
41
45
"""
42
46
if os .path .isfile (in_xml_path ):
43
47
# Adjust the price of sc and guard
44
48
update_danmaku_prices (in_xml_path )
45
- in_ass_path = in_xml_path [:- 4 ] + '.ass'
46
- if resolution == '1280x720' :
47
- boxsize = '500x720'
48
- boxfont = '23'
49
- danmakufont = '38'
49
+ out_ass_path = in_xml_path [:- 4 ] + '.ass'
50
+ if resolution_x == 1280 and resolution_y == 720 :
51
+ boxfont = 30
52
+ danmakufont = 38
50
53
subtitle_font_size = '15'
51
54
subtitle_margin_v = '20'
52
- elif resolution == '1920x1080' :
53
- boxsize = '500x1080'
54
- boxfont = '50'
55
- danmakufont = '55'
56
- subtitle_font_size = '16'
57
- subtitle_margin_v = '60'
58
- elif resolution == '1080x1920' :
59
- boxsize = '500x1920'
60
- boxfont = '55'
61
- danmakufont = '60'
55
+ elif resolution_x == 720 and resolution_y == 1280 :
56
+ boxfont = 30
57
+ danmakufont = 38
62
58
subtitle_font_size = '8'
63
59
subtitle_margin_v = '60'
64
- elif resolution == '720x1280' :
65
- boxsize = '500x1280'
66
- boxfont = '28'
67
- danmakufont = '38'
60
+ elif resolution_x == 1920 and resolution_y == 1080 :
61
+ boxfont = 42
62
+ danmakufont = 42
63
+ subtitle_font_size = '16'
64
+ subtitle_margin_v = '60'
65
+ elif resolution_x == 1080 and resolution_y == 1920 :
66
+ boxfont = 42
67
+ danmakufont = 42
68
68
subtitle_font_size = '8'
69
69
subtitle_margin_v = '60'
70
70
else :
71
- boxsize = '500x1080'
72
- boxfont = '38'
73
- danmakufont = '38'
71
+ boxfont = 38
72
+ danmakufont = 38
74
73
subtitle_font_size = '16'
75
74
subtitle_margin_v = '60'
76
75
# Convert danmakus to ass file
77
- try :
78
- result = subprocess .run (
79
- [DanmakuFactory_PATH , "-o" , in_ass_path , "-i" , in_xml_path , "--resolution" , resolution , "--msgboxsize" , boxsize , "--msgboxfontsize" , boxfont , "-S" , danmakufont , "--ignore-warnings" ],
80
- check = True , capture_output = True , text = True )
81
- scan_log .debug (f"DanmakuFactory output: { result .stdout } " )
82
- except subprocess .CalledProcessError as e :
83
- scan_log .error (f"Error: { e .stderr } " )
84
- # Remove emojis from ass danmakus (the ffmpeg does not support emojis)
85
- remove_emojis (in_ass_path )
86
- scan_log .info (f"The emojis of { in_ass_path } has been removed." )
76
+ convert_xml_to_ass (danmakufont , boxfont , resolution_x , resolution_y , in_xml_path , out_ass_path )
87
77
return subtitle_font_size , subtitle_margin_v
0 commit comments