你如何获得gstreamer-1.0中的字幕索引号

时间:2015-07-05 11:37:56

标签: python gstreamer python-gstreamer

使用gst-discoverer我可以获得一个mkv文件中可用的子标题列表,但它们看起来是一个随机顺序。
有谁知道,使用python,如何获取每个子标题流的索引 一旦知道索引简单

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

将更改正在使用的子标题流 这是一个简单的模拟,播放mkv并列出可用的字幕:

#!/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)    

2 个答案:

答案 0 :(得分:0)

在python中,您可以使用索引来获取每个字幕流:

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

我通过迭代扩展了你的例子,但是用于演示的字幕列表。您仍需要确定要使用的索引。

#!/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) 

答案 1 :(得分:0)

这是一个不依赖gst-discoverer-1.0的工作解决方案

#!/usr/bin/env python
import time
from gi.repository import Gst, GstTag
Gst.init(None)
uri='file:///home/rolf/H.mkv'
pipeline=Gst.ElementFactory.make("playbin", "playbin")
pipeline.set_property('uri',uri)
try:
    prefix = uti.rindex('.')
    suburi = uri[:prefix]
    suburi = suburi+".srt"
    suburi = pipeline.set_property('suburi', suburi)
except:
    pass
pipeline.set_state(Gst.State.PLAYING)
time.sleep(0.5)
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 xrange(subs):
    tags = pipeline.emit("get-text-tags", x)
    if (tags):
        name = tags.nth_tag_name(0)
        if name == "language-code":
            current_code = tags.get_string(name)[1]
            language = GstTag.tag_get_language_name(current_code)
            print "Subtitle stream ",current_code, "Index ", x, language

    else:
        print "No subtitle tags listed"
print "Currently using Subtitle set ", subc
time.sleep(dur)