Give player access to file info

This commit is contained in:
Jan Hamal Dvořák 2025-03-12 15:28:25 +01:00
parent f1e35214e2
commit 549f557008
3 changed files with 18 additions and 7 deletions

View file

@ -1,5 +1,6 @@
from __future__ import annotations
import errno
import os
import sys
from enum import Enum, auto
@ -97,7 +98,8 @@ class FileItem(GObject.Object, Watcher):
self.full_path, f"user.lazy_player.{name}", str(value).encode("utf8")
)
except OSError as err:
print(err, file=sys.stderr)
if err.errno != errno.ENOTSUP:
print(err, file=sys.stderr)
class FileListModel(GObject.Object, Gio.ListModel):

View file

@ -222,7 +222,7 @@ class MainWindow(Gtk.ApplicationWindow, Watcher):
# Start playing the video
self.player.play(
file_item.full_path,
file_item,
position,
file_item.saved_subtitle_track.value,
file_item.saved_audio_track.value,

View file

@ -1,10 +1,10 @@
from __future__ import annotations
from pathlib import Path
from time import time
from gi.repository import GObject, Gst, Gtk
from .file_model import FileItem
from .reactive import Ref
SEEK_FORWARD = Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT | Gst.SeekFlags.SNAP_AFTER
@ -19,6 +19,7 @@ class VideoPlayer(GObject.Object):
__gtype_name__ = "VideoPlayer"
file_item: Ref[FileItem | None]
is_playing: Ref[bool]
is_paused: Ref[bool]
@ -29,6 +30,7 @@ class VideoPlayer(GObject.Object):
self.picture = picture
self.file_item = Ref(None)
self.is_playing = Ref(False)
self.is_paused = Ref(True)
self.last_user_input = Ref(time())
@ -54,14 +56,17 @@ class VideoPlayer(GObject.Object):
def play(
self,
file_path: Path | str,
file_item: FileItem,
position: int = 0,
subtitle_track: int = -2,
audio_track: int = 0,
) -> None:
"""Start playing a video file"""
self.playbin.set_property("uri", Gst.filename_to_uri(str(file_path)))
uri = Gst.filename_to_uri(str(file_item.full_path))
assert uri is not None
self.playbin.set_property("uri", uri)
if subtitle_track >= 0:
flags = self.playbin.get_property("flags")
@ -93,6 +98,7 @@ class VideoPlayer(GObject.Object):
"""Stop playback and release resources"""
self.pipeline.set_state(Gst.State.NULL)
self.file_item.value = None
self.is_playing.value = True
self.is_paused.value = False
@ -160,11 +166,14 @@ class VideoPlayer(GObject.Object):
def cycle_subtitles(self) -> tuple[bool, int, str]:
"""Cycle through available subtitle tracks, including off state"""
if not self.file_item.value:
return False, 0, ""
flags = self.playbin.get_property("flags")
current = self.playbin.get_property("current-text")
n_text = self.playbin.get_property("n-text")
if n_text == 0:
if not n_text:
return False, 0, ""
if not (flags & 0x00000004): # TEXT flag
@ -198,7 +207,7 @@ class VideoPlayer(GObject.Object):
def _get_track_lang(self, track_type: str, track_index: int) -> str:
caps: Gst.TagList | None = self.playbin.emit(f"get-{track_type}-tags", track_index)
if not caps:
return str(track_index)
return str("???")
found, lang = caps.get_string("language-code")
return lang if found else "???"