'How do you get the subtitle index number in gstreamer-1.0

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)    


Solution 1:[1]

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) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kpaxit