22
33import os
44import subprocess
5- from src .config import DanmakuFactory_PATH
65from src .log .logger import scan_log
76from .adjust_price import update_danmaku_prices
8- from .remove_emojis import remove_emojis
7+ from .DanmakuConvert . dmconvert import convert_xml_to_ass
98
109
1110def get_resolution (in_video_path ):
@@ -19,69 +18,60 @@ def get_resolution(in_video_path):
1918 # Use ffprobe to acquire the video resolution
2019 result = subprocess .run (
2120 ['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 ,
2422 text = True ,
25- check = True
23+ capture_output = True
2624 )
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 } " )
2728 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
3032 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
3335
34- def process_danmakus (in_xml_path , resolution ):
36+ def process_danmakus (in_xml_path , resolution_x , resolution_y ):
3537 """Generate and process the danmakus according to different resolution.
3638 Args:
3739 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
3942 Return:
4043 subtitle_font_size: str, the font size of subtitles
44+ subtitle_margin_v: str, the margin v of subtitles
4145 """
4246 if os .path .isfile (in_xml_path ):
4347 # Adjust the price of sc and guard
4448 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
5053 subtitle_font_size = '15'
5154 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
6258 subtitle_font_size = '8'
6359 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
6868 subtitle_font_size = '8'
6969 subtitle_margin_v = '60'
7070 else :
71- boxsize = '500x1080'
72- boxfont = '38'
73- danmakufont = '38'
71+ boxfont = 38
72+ danmakufont = 38
7473 subtitle_font_size = '16'
7574 subtitle_margin_v = '60'
7675 # 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 )
8777 return subtitle_font_size , subtitle_margin_v
0 commit comments