Skip to content

Commit 61aaeee

Browse files
committed
feat: Implement persistent subtitle style settings for text color, outline color, and font scale.
1 parent 5dbd313 commit 61aaeee

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ def __init__(self):
139139
self.video_player.video_finished.connect(self.on_video_finished)
140140
self.video_player.position_changed.connect(self.save_progress)
141141
self.video_player.pause_changed.connect(self.on_player_pause_changed)
142+
self.video_player.subtitle_style_changed.connect(self.save_subtitle_settings)
143+
144+
# Apply initial subtitle settings
145+
self.video_player.set_subtitle_styles(self.sub_color, self.sub_border_color, self.sub_scale)
142146

143147
self.splitter.addWidget(self.video_player)
144148

@@ -899,6 +903,11 @@ def create_default_settings(self):
899903
config['Video'] = {
900904
'extensions': '.mp4,.mkv,.avi,.mov,.wmv,.flv,.webm,.m4v,.mpg,.mpeg,.3gp,.ts'
901905
}
906+
config['Subtitles'] = {
907+
'text_color': '#FFFFFF',
908+
'outline_color': '#000000',
909+
'font_scale': '1.0'
910+
}
902911

903912
with open(self.config_file, 'w', encoding='utf-8') as f:
904913
config.write(f)
@@ -938,6 +947,30 @@ def load_settings(self):
938947
self.display_height = config.getint('Thumbnails', 'display_height', fallback=90)
939948
self.animation_interval = config.getint('Thumbnails', 'animation_interval', fallback=400)
940949

950+
# Subtitle settings
951+
self.sub_color = config.get('Subtitles', 'text_color', fallback='#FFFFFF')
952+
self.sub_border_color = config.get('Subtitles', 'outline_color', fallback='#000000')
953+
self.sub_scale = config.getfloat('Subtitles', 'font_scale', fallback=1.0)
954+
955+
def save_subtitle_settings(self, property_name, value):
956+
"""Save subtitle style settings to ini file."""
957+
config = configparser.ConfigParser()
958+
if self.config_file.exists():
959+
config.read(self.config_file, encoding='utf-8')
960+
961+
if 'Subtitles' not in config:
962+
config['Subtitles'] = {}
963+
964+
if property_name == "sub-color":
965+
config['Subtitles']['text_color'] = value
966+
elif property_name == "sub-border-color":
967+
config['Subtitles']['outline_color'] = value
968+
elif property_name == "sub-scale":
969+
config['Subtitles']['font_scale'] = f"{value:.2f}"
970+
971+
with open(self.config_file, 'w', encoding='utf-8') as f:
972+
config.write(f)
973+
941974
def format_time(self, seconds):
942975
if not seconds:
943976
return "00:00"

player.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class VideoPlayerWidget(QWidget):
3131
request_hide_main_window = pyqtSignal()
3232
request_show_main_window = pyqtSignal()
3333
pause_changed = pyqtSignal(bool)
34+
subtitle_style_changed = pyqtSignal(str, object)
3435

3536
def __init__(self, parent=None):
3637
super().__init__(parent)
@@ -44,6 +45,9 @@ def __init__(self, parent=None):
4445
self.is_loading = False
4546
self.auto_play_pending = False
4647
self.player = None
48+
self.sub_color = "#FFFFFF"
49+
self.sub_border_color = "#000000"
50+
self.sub_scale = 1.0
4751
self.setup_ui()
4852
self.setup_mpv()
4953

@@ -216,6 +220,10 @@ def setup_mpv(self):
216220
log_handler=print,
217221
loglevel='warn'
218222
)
223+
224+
# Apply initial subtitle styles
225+
self._apply_subtitle_styles()
226+
219227
@self.player.property_observer('time-pos')
220228
def time_observer(_name, value):
221229
if value is not None:
@@ -310,6 +318,7 @@ def load_video(self, file_path, saved_position=0, volume=100, auto_play=True):
310318

311319
self.player.sid = 'no'
312320
self.player.loadfile(file_path)
321+
self._apply_subtitle_styles()
313322
self.play_btn.setEnabled(True)
314323
self.progress_slider.setEnabled(True)
315324
self.frame_back_btn.setEnabled(True)
@@ -658,20 +667,61 @@ def change_subtitle_style(self, property_name, value):
658667
# Convert HEX to MPV format (ARGB)
659668
hex_color = value.lstrip('#')
660669
self.player.sub_color = f"#FF{hex_color.upper()}"
670+
self.sub_color = value
671+
self.subtitle_style_changed.emit("sub-color", value)
661672
print(f"📝 Subtitle color: {value}")
662673
elif property_name == "sub-border-color":
663674
hex_color = value.lstrip('#')
664675
self.player.sub_border_color = f"#FF{hex_color.upper()}"
676+
self.sub_border_color = value
677+
self.subtitle_style_changed.emit("sub-border-color", value)
665678
print(f"📝 Subtitle border color: {value}")
666679
elif property_name == "sub-scale":
667680
# value is delta (+5 or -5)
668681
current_scale = getattr(self.player, 'sub_scale', 1.0)
669682
new_scale = max(0.5, min(3.0, current_scale + value / 100.0))
670683
self.player.sub_scale = new_scale
684+
self.sub_scale = new_scale
685+
self.subtitle_style_changed.emit("sub-scale", new_scale)
671686
print(f"📝 Subtitle scale: {new_scale:.2f}")
672687
except Exception as e:
673688
print(f"Error changing subtitle style: {e}")
674689

690+
def set_subtitle_styles(self, color, border_color, scale):
691+
"""Set initial subtitle styles."""
692+
self.sub_color = color
693+
self.sub_border_color = border_color
694+
self.sub_scale = scale
695+
696+
# Also sync with popup
697+
if hasattr(self, 'subtitle_btn'):
698+
self.subtitle_btn.popup.text_color = color
699+
self.subtitle_btn.popup.outline_color = border_color
700+
self.subtitle_btn.popup._update_text_color_btn()
701+
self.subtitle_btn.popup._update_outline_color_btn()
702+
703+
if self.player:
704+
self._apply_subtitle_styles()
705+
706+
def _apply_subtitle_styles(self):
707+
"""Apply stored subtitle styles to MPV player."""
708+
if not self.player:
709+
return
710+
711+
try:
712+
# Color
713+
hex_color = self.sub_color.lstrip('#')
714+
self.player.sub_color = f"#FF{hex_color.upper()}"
715+
716+
# Border
717+
hex_border = self.sub_border_color.lstrip('#')
718+
self.player.sub_border_color = f"#FF{hex_border.upper()}"
719+
720+
# Scale
721+
self.player.sub_scale = self.sub_scale
722+
except Exception as e:
723+
print(f"Error applying subtitle styles: {e}")
724+
675725
def change_subtitle_track(self, index):
676726
"""Switch subtitles on selection."""
677727
if not self.player: return

0 commit comments

Comments
 (0)