Skip to content

Commit

Permalink
replace-omxplayer #comment Add subtitle selection capability to the p…
Browse files Browse the repository at this point in the history
…layer wrapper.

 - Set the subtitle with the prefered language when available.
  • Loading branch information
Tastyep committed Jun 5, 2020
1 parent 89f0355 commit 0aa593f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
6 changes: 6 additions & 0 deletions OpenCast/app/service/player.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import structlog
from OpenCast.app.command import player as player_cmds
from OpenCast.app.error import CommandFailure
from OpenCast.config import config
from OpenCast.domain.model.player_state import PlayerState

from .service import Service
Expand Down Expand Up @@ -104,6 +105,11 @@ def play_video(model):
model.play(video)

self._player.play(video.id, str(video.path))
player = self._player_model()
if player.subtitle_state is True:
sub_stream = video.stream("subtitle", config["subtitle.language"])
if sub_stream is not None:
self._player.select_subtitle_stream(sub_stream.index)
self._update(cmd_id, play_video)

def _queue_video_impl(self, cmd_id, video):
Expand Down
26 changes: 21 additions & 5 deletions OpenCast/infra/media/player_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from threading import Lock
from threading import Condition
from uuid import UUID

import OpenCast.infra.event.player as e
import structlog
import vlc
from vlc import EventType

from .error import PlayerError


class PlayerWrapper:
Expand All @@ -16,9 +18,6 @@ def __init__(self, vlc_instance, evt_dispatcher):
self._player = self._instance.media_player_new()
self._id_to_media = {}

self._lock = Lock()
self._stop_operation_id = None

player_events = self._player.event_manager()
player_events.event_attach(EventType.MediaPlayerEndReached, self._on_media_end)

Expand All @@ -39,6 +38,23 @@ def pause(self):
def unpause(self):
self._player.pause()

def select_subtitle_stream(self, index: int):
media = self._player.get_media()
if media is None:
raise PlayerError("the player is not started")

def is_playing(_, cv):
with cv:
cv.notify()

cv = Condition()
self._player.event_manager().event_attach(
EventType.MediaPlayerPlaying, is_playing, cv
)
with cv:
cv.wait_for(self._player.is_playing)
self._player.video_set_spu(index)

def toggle_subtitle(self):
self._player.toggle_teletext()

Expand Down

0 comments on commit 0aa593f

Please sign in to comment.