1

Using gst-discoverer I can get a list of the sub-titles available in an mkv file but they come out in what appears to be a random order.
Does anyone know, using python, how to get the index for each sub-title stream.
Once the index is known a simple

self.pipeline.set_property("current-text",subno)

will change the sub-title stream being used.
Here is a simple mock up that plays an mkv and lists the subtitles available:

#!/usr/bin/env python
import time
from gi.repository import Gst
from gi.repository import GstPbutils

Gst.init(None)
discoverer = GstPbutils.Discoverer()
uri='file:///home/rolf/H.mkv'
info = discoverer.discover_uri(uri)
for x in info.get_subtitle_streams():
    print x.get_language()
pipeline=Gst.ElementFactory.make("playbin", "playbin")
pipeline.set_property('uri',uri)
pipeline.set_state(Gst.State.PLAYING)
time.sleep(2)
subs = pipeline.get_property('n-text')
print "there are ", subs, " Subtitles"
auds = pipeline.get_property('n-audio')
print "there are ", auds, " Audio streams"
vids = pipeline.get_property('n-video')
print "there are ", vids, " Video Streams"
subc = pipeline.get_property('current-text')
print "Currently using ", subc, " Subtitle set"
dur = int(info.get_duration())/Gst.SECOND
hh = int(dur/3600)
mm, ss = (divmod(int(divmod(dur,3600)[1]),60))
print("Duration : %02d:%02d:%02d" % (hh,mm,ss))
time.sleep(dur)    
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60

2 Answers2

0

In python you can get each subtitle-stream by using index e.g.:

info.get_subtitle_streams()[0]
info.get_subtitle_streams()[1]
etc...

I extended your example with iteration though list of subtitles for demonstation. You still need to decide what index to use.

#!/usr/bin/env python
import time
from gi.repository import Gst
from gi.repository import GstPbutils

Gst.init(None)
discoverer = GstPbutils.Discoverer()
uri='file:///home/linuxencoder/sintel.mkv'
info = discoverer.discover_uri(uri)
mysublist = info.get_subtitle_streams()
i=0
for x in mysublist:
    print (x.get_language(), i, info.get_subtitle_streams()[i].get_language())
    i+=1
pipeline=Gst.ElementFactory.make("playbin", "playbin")
pipeline.set_property('uri',uri)
pipeline.set_state(Gst.State.PLAYING)
time.sleep(2)
subs = pipeline.get_property('n-text')
print "there are ", subs, " Subtitles"
auds = pipeline.get_property('n-audio')
print "there are ", auds, " Audio streams"
vids = pipeline.get_property('n-video')
print "there are ", vids, " Video Streams"
pipeline.set_property("current-text", 3)
subc = pipeline.get_property('current-text')
print "Currently using ", subc, " subtitle set. Sub name:", mysublist[subc].get_language()
dur = int(info.get_duration())/Gst.SECOND
hh = int(dur/3600)
mm, ss = (divmod(int(divmod(dur,3600)[1]),60))
print("Duration : %02d:%02d:%02d" % (hh,mm,ss))
time.sleep(dur) 
kpaxit
  • 133
  • 2
  • 11
  • The problem is that the subtitle streams are not listed in order. So for example if the language en comes out as the first stream, that doesn't mean that if you choose to set index 0 that you will see English subtitles, so assigning them a number based on their position in the list doesn't work I'm afraid but thanks for the reply kpaxit. – Rolf of Saxony Jul 07 '15 at 15:24
0

Here is a working solution with no reliance on gst-discoverer-1.0
[updated for python3]

#!/usr/bin/env python
import time
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstTag', '1.0')
gi.require_version('GstVideo', '1.0')
from gi.repository import Gst, GstTag
from gi.repository import GstVideo
Gst.init(None)
uri='file:///home/rolf/H.mkv'
pipeline=Gst.ElementFactory.make("playbin", "playbin")
pipeline.set_property('uri',uri)

suburi = "file:///home/rolf/H.srt"
suburi = pipeline.set_property('suburi', suburi)

pipeline.set_state(Gst.State.PLAYING)
time.sleep(1.0)
subs = pipeline.get_property('n-text')
auds = pipeline.get_property('n-audio')
vids = pipeline.get_property('n-video')
print (vids, "Video Streams ", auds, "Audio Streams ", subs, "Subtitle Streams")
subc = pipeline.get_property('current-text')

dur = (pipeline.query_duration(Gst.Format.TIME)[1]) / Gst.SECOND  
hh = int(dur/3600)
mm, ss = (divmod(int(divmod(dur,3600)[1]),60))
print("Duration : %02d:%02d:%02d" % (hh,mm,ss))
for x in range(subs):
    tags = pipeline.emit("get-text-tags", x)
    if tags:
        for i in range(tags.n_tags()):
            if tags.nth_tag_name(i) == "language-code":
                name = tags.nth_tag_name(i)
        if name == "language-code":
            current_code = tags.get_string(name)[1]
            if current_code != "und":
                language = GstTag.tag_get_language_name(current_code)
            else:
                language = "??"
            print(current_code, language)
    else:
        print( "No subtitle tags listed")
pipeline.set_property("current-text", 9)
print( "Currently using Subtitle set ", pipeline.get_property('current-text'))
time.sleep(dur) 

[output]

1 Video Streams  1 Audio Streams  10 Subtitle Streams
Duration : 01:36:40
No subtitle tags listed
en English
ro Romanian; Moldavian; Moldovan
da Danish
cs Czech
hu Hungarian
bg Bulgarian
pl Polish
sl Slovenian
el Greek, Modern (1453-)
Currently using Subtitle set  9

The reference to No subtitle tags listed is to the manually attached sub-title file H.srt

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • P.S. You can use the same method for video and audio streams by issuing "get-video-tags" and "get-audio-tags" requests to pipeline.emit – Rolf of Saxony Jul 07 '15 at 15:37