[英]python and gstreamer, trying to play video (and later add textoverlay)
我正在嘗試編寫python應用程序,並讓gstreamer播放我錄制的視頻文件(並在稍后使用textoverlay在視頻中添加一些字幕)。
但是看起來我仍然在理解填充墊的工作方式方面存在一些基本問題。.我似乎無法正確建立鏈接。
我在上面構建的基本示例是一個簡單的應用程序,顯示來自網絡攝像頭的視頻。 所以我知道代碼可以工作,並且只有我的管道使事情變得混亂。
另外,如果我在終端中運行執行以下管道,則它會起作用:
gst-launch-0.10 filesrc location=GOPR0042.MP4 ! decodebin2 ! ffmpegcolorspace ! videoflip method=2 ! xvimagesink
現在,我正在嘗試重新創建到python應用程序的管道,如下所示:
#!/usr/bin/env python
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
class GTK_Main:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Webcam-Viewer")
window.set_default_size(500, 400)
window.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox()
window.add(vbox)
self.movie_window = gtk.DrawingArea()
vbox.add(self.movie_window)
hbox = gtk.HBox()
vbox.pack_start(hbox, False)
hbox.set_border_width(10)
hbox.pack_start(gtk.Label())
self.button = gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
hbox.pack_start(self.button, False)
self.button2 = gtk.Button("Quit")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False)
hbox.add(gtk.Label())
window.show_all()
# Set up the gstreamer pipeline
self.pipeline = gst.Pipeline("player")
self.filesource = gst.element_factory_make("filesrc","filesource")
self.filesource.set_property("location","""/home/jlumme/video/GOPR0042.MP4""")
self.pipeline.add(self.filesource)
self.decoder = gst.element_factory_make("decodebin2","decoder")
self.pipeline.add(self.decoder)
self.colorspace = gst.element_factory_make("ffmpegcolorspace","colorspace")
self.pipeline.add(self.colorspace)
self.videosink = gst.element_factory_make("xvimagesink","videosink")
self.pipeline.add(self.videosink)
self.filesource.link(self.decoder)
self.decoder.link(self.colorspace) #This fails
self.colorspace.link(self.videosink)
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message", self.on_message)
bus.connect("sync-message::element", self.on_sync_message)
def start_stop(self, w):
if self.button.get_label() == "Start":
self.button.set_label("Stop")
self.pipeline.set_state(gst.STATE_PLAYING)
else:
self.pipeline.set_state(gst.STATE_NULL)
self.pipeline.set_label("Start")
def exit(self, widget, data=None):
gtk.main_quit()
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.pipeline.set_state(gst.STATE_NULL)
self.button.set_label("Start")
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.pipeline.set_state(gst.STATE_NULL)
self.button.set_label("Start")
def on_sync_message(self, bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
# Assign the viewport
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(self.movie_window.window.xid)
GTK_Main()
gtk.gdk.threads_init()
gtk.main()
現在,我已經看到人們使用動態墊將解碼器bin鏈接到一些音頻內容,但是我不太了解它是如何工作的……所以,我想我不能直接連接decoderbin2和ffmpegcolorspace嗎? 有人可以解釋一下為什么嗎?
此外,您是否預見了下一步要在其中向管道添加textoverlay元素以顯示字幕的問題?
按照最近回答我自己的問題的習慣,我也會在這里這樣做:)
因此,經過更多的閱讀和黑客攻擊后,我的確意識到我並沒有真正獲得動態鍵盤以及僅當有東西進入時才需要連接動態鍵盤。
因此,基本上,我針對音頻和視頻使用2個隊列解決了上述問題。 這些隊列隨后連接到解碼器,並且需要將它們放在解復用器之后並動態連接。 解碼器和接收器似乎也需要動態連接焊盤。
這個論壇上的一個問題很清楚地說明了這一過程,就是這個問題: 用於播放avi文件的gstreamer代碼已掛起
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.