数字0.1你怎么得到副标题

2024-05-13 05:07:48 发布

您现在位置:Python中文网/ 问答频道 /正文

使用gst discoverer,我可以获得mkv文件中可用的子标题列表,但它们以随机顺序出现。
有人知道使用python如何获得每个子标题流的索引。
一旦知道了索引

{cd1}

将更改正在使用的子标题流。
下面是一个简单的模型,可以播放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)    

Tags: importinfogetpipelinetimepropertyurithere
2条回答

这是一个不依赖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)    

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

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

我通过迭代扩展了您的示例,通过子标题列表进行演示。您仍然需要决定使用什么索引。在

^{pr2}$

相关问题 更多 >