python和gstreamer,尝试播放视频(后来添加textoverlay)

时间:2012-05-29 08:38:21

标签: python video gstreamer

我正在尝试编写一个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()

现在我看到人们使用动态pad将decodebin链接到某些音频内容,但我真的不明白它是如何工作的...... 那么,我想我不能直接连接decoderbin2和ffmpegcolorspace? 有人能解释我为什么吗?

另外,你是否在下一步中预见到了问题,我想在管道中添加textoverlay元素,以显示字幕?

1 个答案:

答案 0 :(得分:1)

在最近回答我自己的问题的习惯中,我也会这样做:)

所以,经过更多阅读和黑客攻击后,我确实意识到我并没有真正获得动态打击垫,以及只有当有东西进来时才需要连接它们。

所以基本上我用2个队列解决了上述问题,包括音频和视频。那些队列然后连接到解码器,并且它们需要被放置在解复用器和放大器之后。动态连接。 解码器和接收器似乎也需要动态连接焊盘。

此论坛上的一个问题非常清楚地解释了这个过程: gstreamer code for playing avi file is hanging

相关问题