首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在gstreamer-1.0中获得字幕索引号?

如何在gstreamer-1.0中获得字幕索引号?
EN

Stack Overflow用户
提问于 2015-07-05 11:37:56
回答 2查看 1K关注 0票数 1

使用gst-发现者,我可以得到一个在mkv文件中可用的字幕列表,但它们的出现似乎是随机的顺序。

有人知道如何使用python来获取每个子标题流的索引吗?

一旦索引被知道,一个简单的

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

将更改正在使用的子标题流。

下面是一个简单的模拟,播放一个mkv并列出可用的字幕:

代码语言:javascript
复制
#!/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)    
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-07 15:33:28

这里有一个工作解决方案,不依赖于gst-发现者-1.0

更新为python3

代码语言:javascript
复制
#!/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) 

输出

代码语言:javascript
复制
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

No subtitle tags listed的引用是对手动附加的字幕文件H.srt的引用。

票数 0
EN

Stack Overflow用户

发布于 2015-07-07 11:35:07

在python中,您可以通过使用索引获得每个字幕流,例如:

代码语言:javascript
复制
info.get_subtitle_streams()[0]
info.get_subtitle_streams()[1]
etc...

我用迭代扩展了您的示例,虽然列出了演示的字幕列表。您仍然需要决定使用什么索引。

代码语言:javascript
复制
#!/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) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31229939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档