Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得gstreamer-1.0中的字幕索引号_Python_Gstreamer_Python Gstreamer - Fatal编程技术网

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

Python 如何获得gstreamer-1.0中的字幕索引号,python,gstreamer,python-gstreamer,Python,Gstreamer,Python Gstreamer,使用gst discoverer,我可以获得mkv文件中可用的子标题列表,但它们似乎是以随机顺序出现的。 有人知道如何使用python为每个子标题流获取索引。 一旦知道索引,一个简单的 self.pipeline.set_属性(“当前文本”,subno) 将更改正在使用的子标题流。 下面是一个播放mkv的简单模型,并列出了可用的字幕: #!/usr/bin/env python import time from gi.repository import Gst from gi.repositor

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

self.pipeline.set_属性(“当前文本”,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)    

在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) 

在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) 

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

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

问题是字幕流没有按顺序列出。例如,如果语言en作为第一个流出现,这并不意味着如果您选择设置索引0,您将看到英文字幕,因此根据它们在列表中的位置为它们分配一个数字恐怕不起作用,但感谢您的回复kpaxit。问题是字幕流没有按顺序列出。例如,如果语言en作为第一个流出现,这并不意味着如果您选择设置索引0,您将看到英文字幕,因此根据它们在列表中的位置为它们分配一个数字恐怕不起作用,但感谢您的回复kpaxit.P.S。通过向pipeline.emitP.S发出“获取视频标签”和“获取音频标签”请求,可以对视频和音频流使用相同的方法。通过向pipeline.emit发出“获取视频标签”和“获取音频标签”请求,可以对视频和音频流使用相同的方法