Allow disabling subtitles

This commit is contained in:
Jan Hamal Dvořák 2025-03-08 22:15:32 +01:00
parent 8944fdc291
commit eef1ec6585

View file

@ -72,12 +72,12 @@ class MainWindow(Gtk.ApplicationWindow):
overlay_box.set_name("black-overlay")
overlay_box.set_vexpand(True)
overlay_box.set_hexpand(True)
# Create an overlay container
overlay = Gtk.Overlay()
overlay.set_child(self.video_widget)
overlay.add_overlay(self.overlay_label)
overlay_box.append(overlay)
# Setup GStreamer pipeline
@ -234,7 +234,7 @@ class MainWindow(Gtk.ApplicationWindow):
elif keyval == Gdk.keyval_from_name("Down"):
self._seek_relative(-60)
return True
elif keyval == Gdk.keyval_from_name("j"):
self._cycle_subtitles()
return True
@ -276,13 +276,28 @@ class MainWindow(Gtk.ApplicationWindow):
Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, new_pos
)
def _get_subtitle_info(self, track_index: int) -> str:
"""Get subtitle track info including language if available"""
playbin = self.pipeline.get_by_name("playbin")
if not playbin:
return str(track_index)
# Query the subtitle track's tags
caps: Gst.TagList | None = playbin.emit("get-text-tags", track_index)
if not caps:
return str(track_index)
found, lang = caps.get_string("language-code")
return f"{track_index} ({lang})" if found else str(track_index)
def _cycle_subtitles(self) -> None:
"""Cycle through available subtitle tracks"""
"""Cycle through available subtitle tracks, including off state"""
playbin = self.pipeline.get_by_name("playbin")
if not playbin:
return
# Get current subtitle track
# Get current flags and subtitle track
flags = playbin.get_property("flags")
current = playbin.get_property("current-text")
n_text = playbin.get_property("n-text")
@ -290,12 +305,27 @@ class MainWindow(Gtk.ApplicationWindow):
self.show_overlay_text("No subtitles available")
return
# Cycle to next track, wrapping around
next_track = (current + 1) % n_text
# If subtitles are disabled, enable them and set to first track
if not (flags & 0x00000004): # TEXT flag
flags |= 0x00000004
playbin.set_property("flags", flags)
playbin.set_property("current-text", 0)
track_info = self._get_subtitle_info(0)
self.show_overlay_text(f"Subtitle track: {track_info}")
return
# If we're on the last track, disable subtitles
if current >= n_text - 1:
flags &= ~0x00000004 # TEXT flag
playbin.set_property("flags", flags)
self.show_overlay_text("Subtitles: Off")
return
# Otherwise cycle to next track
next_track = current + 1
playbin.set_property("current-text", next_track)
# Show the new track number
self.show_overlay_text(f"Subtitle track: {next_track}")
track_info = self._get_subtitle_info(next_track)
self.show_overlay_text(f"Subtitle track: {track_info}")
def show_overlay_text(self, text: str, timeout_seconds: float = 1.0) -> None:
"""Show text in a centered overlay that disappears after timeout"""