Implement seeking for vlc

This commit is contained in:
PapaTutuWawa 2025-01-12 13:56:33 +01:00
parent a90047d14c
commit f590e752be
4 changed files with 41 additions and 4 deletions

View File

@ -7,6 +7,9 @@ class KodiTime:
minutes: int minutes: int
seconds: int seconds: int
def to_seconds(self) -> int:
return self.hours * 3600 + self.minutes * 60 + self.seconds
def seconds_to_kodi_format(seconds: int) -> KodiTime: def seconds_to_kodi_format(seconds: int) -> KodiTime:
"""Convert seconds into hours, minutes, and seconds as Kodi wants that.""" """Convert seconds into hours, minutes, and seconds as Kodi wants that."""
hours = seconds // 3600 hours = seconds // 3600

View File

@ -9,7 +9,7 @@ from http.server import BaseHTTPRequestHandler
from microkodi.repository import I from microkodi.repository import I
from microkodi.programs import Program, PlayerInfo from microkodi.programs import Program, PlayerInfo
from microkodi.helpers import seconds_to_kodi_format from microkodi.helpers import KodiTime, seconds_to_kodi_format
def jsonrpc_response(id: int, payload: dict[str, Any]) -> dict[str, Any]: def jsonrpc_response(id: int, payload: dict[str, Any]) -> dict[str, Any]:
return { return {
@ -99,9 +99,25 @@ class PlayerRpcObject(JsonRpcObject):
return self.open(params) return self.open(params)
elif method == "PlayPause": elif method == "PlayPause":
return self.play_pause(params) return self.play_pause(params)
elif method == "Seek":
return self.seek(params)
else: else:
return "Unknown method" return "Unknown method"
def seek(self, params: dict[str, Any]) -> Any:
if not self.is_active:
self.logger.warn("Trying to seek player that is not active")
return "ERR"
time_raw = params["value"]["time"]
kodi_time = KodiTime(
hours=time_raw["hours"],
minutes=time_raw["minutes"],
seconds=time_raw["seconds"],
)
self._active_program.seek(kodi_time)
return "Ok"
def get_active_players(self, params: dict[str, Any]) -> Any: def get_active_players(self, params: dict[str, Any]) -> Any:
if not self.is_active: if not self.is_active:
return [] return []

View File

@ -2,6 +2,8 @@ from dataclasses import dataclass
from urllib.parse import ParseResult from urllib.parse import ParseResult
import logging import logging
from microkodi.helpers import KodiTime
@dataclass @dataclass
class PlayerInfo: class PlayerInfo:
runtime: int runtime: int
@ -38,3 +40,6 @@ class Program:
def get_player_info(self) -> PlayerInfo: def get_player_info(self) -> PlayerInfo:
raise NotImplementedError() raise NotImplementedError()
def seek(self, timestamp: KodiTime):
raise NotImplementedError()

View File

@ -1,5 +1,5 @@
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
from typing import Callable from typing import Any, Callable
from urllib.parse import ParseResult, urlunparse from urllib.parse import ParseResult, urlunparse
import subprocess import subprocess
import logging import logging
@ -11,6 +11,7 @@ from microkodi.process import nonblocking_run
from microkodi.config import Config from microkodi.config import Config
from microkodi.programs import PlayerInfo, Program from microkodi.programs import PlayerInfo, Program
from microkodi.repository import I from microkodi.repository import I
from microkodi.helpers import KodiTime
EVENT_PLAYER_EXIT = "post-player-exit" EVENT_PLAYER_EXIT = "post-player-exit"
@ -138,11 +139,15 @@ class VlcProgram(Program):
I.get("DataBridge").notification.emit("VLC exited with an error") I.get("DataBridge").notification.emit("VLC exited with an error")
self._process = None self._process = None
def __vlc_command(self, command: str) -> str | None: def __vlc_command(self, command: str, params: dict[str, Any] | None = None) -> str | None:
try: try:
req = requests.get( req = requests.get(
f"http://127.0.0.1:9090/requests/status.xml?command={command}", f"http://127.0.0.1:9090/requests/status.xml",
auth=("", self._vlc_password), auth=("", self._vlc_password),
params={
"command": command,
**(params or {}),
},
) )
return req.text return req.text
except Exception as ex: except Exception as ex:
@ -156,6 +161,14 @@ class VlcProgram(Program):
if self.is_active(): if self.is_active():
self._process.terminate() self._process.terminate()
def seek(self, timestamp: KodiTime):
self.__vlc_command(
"seek",
{
"val": timestamp.to_seconds(),
}
)
def exit(self): def exit(self):
self.stop("mpvplayer.exit") self.stop("mpvplayer.exit")