Skip to content

Commit

Permalink
Merge pull request #157 from qnga/fix/audio_tracks_order
Browse files Browse the repository at this point in the history
Fix the order of audio tracks in the menu
  • Loading branch information
otsaloma authored May 13, 2020
2 parents d807f3f + f50b4e5 commit 528c72e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions gaupol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"Gst": "1.0",
"GstPbutils": "1.0",
"GstVideo": "1.0",
"GstTag": "1.0",
}.items():
with aeidon.util.silent(Exception):
gi.require_version(module, version)
Expand Down
16 changes: 12 additions & 4 deletions gaupol/agents/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

with aeidon.util.silent(Exception):
from gi.repository import Gst
from gi.repository import GstTag



class VideoAgent(aeidon.Delegate):
Expand Down Expand Up @@ -348,11 +350,17 @@ def _update_languages_menu(self):
"""Update the audio language selection menu."""
menu = self.get_menubar_section("audio-languages-placeholder")
menu.remove_all()
languages = self.player.get_audio_languages()
for i, language in enumerate(languages):
language = language or _("Undefined")
tracks = self.player.get_audio_infos()
for i, track in enumerate(tracks):
title = track.title or _("Track {:d}").format(i + 1)
lang = None
if track.language_code is not None:
lang = GstTag.tag_get_language_name(track.language_code)
if lang is None:
lang = track.language_name
name = title if lang is None else "{} - [{}]".format(title, lang.title())
action = "win.set-audio-language::{:d}".format(i)
menu.append(language, action)
menu.append(name, action)
if i == self.player.audio_track:
action = self.get_action("set-audio-language")
action.set_state(str(i))
Expand Down
23 changes: 20 additions & 3 deletions gaupol/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import gaupol
import time

from collections import namedtuple
from aeidon.i18n import _
from gi.repository import GLib
from gi.repository import Gtk
Expand Down Expand Up @@ -56,6 +57,8 @@ class VideoPlayer(aeidon.Observable):

signals = ("state-changed",)

TrackInfo = namedtuple("TrackInfo", ["title", "language_code", "language_name"])

def __init__(self):
"""Initialize a :class:`VideoPlayer` instance."""
aeidon.Observable.__init__(self)
Expand Down Expand Up @@ -116,9 +119,19 @@ def _ensure_default_segment(self):
self._playbin.seek_simple(Gst.Format.TIME, seek_flags, pos)
self._in_default_segment = True

def get_audio_languages(self):
"""Return a sequence of audio languages or ``None``."""
return tuple(x.get_language() for x in self._info.get_audio_streams())
def get_audio_infos(self):
"""Return a sequence of audio track infos."""
return tuple(
self._make_track_infos(self._playbin.emit("get-audio-tags", i))
for i in range(self._playbin.props.n_audio)
)

def _make_track_infos(self, tags):
return self.TrackInfo(
tags.get_string("title")[1],
tags.get_string("language-code")[1],
tags.get_string("language-name")[1]
)

def get_duration(self, mode=None):
"""Return duration of video stream or ``None``."""
Expand Down Expand Up @@ -356,6 +369,10 @@ def set_uri(self, uri):
dialog.add_button(_("_OK"), Gtk.ResponseType.OK)
dialog.set_default_response(Gtk.ResponseType.OK)
gaupol.util.flash_dialog(dialog)
else:
# Make stream tags available from _playbin
self._playbin.set_state(Gst.State.PAUSED)
self._playbin.get_state(Gst.CLOCK_TIME_NONE)

def stop(self):
"""Stop."""
Expand Down

0 comments on commit 528c72e

Please sign in to comment.