4

リモートで動画を再生するアート プロジェクトに参加しています。HTTP サーバーと gstreamer ビデオ プレーヤーを備えた単純な Python アプリケーションを実装しました。http リクエストをキャッチして、現在再生中のビデオを変更することはできますが、新しいビデオを同じウィンドウに追加して、2 つのビデオを同時に再生し続けたいと思います。

playbin2 を使用して動画を再生しましたが、一度に 1 つの uri しか再生できないと思います。同時に複数のビデオを再生できる他のソリューションを見つけようとしましたが、役に立ちません...

複数のストリームを同時に再生する簡単な例を投稿したり、ドキュメントやその他のリソースへのポインタを教えてください。

前もって感謝します!!

PS。私が書いたコードは次のとおりです。VideoPlayer クラスはストリームを初期化し、playCurrent 関数は現在再生中のビデオを切り替えます。新しいビデオをストリームに追加するためだけにその関数を使用したいと考えています。

#!/usr/bin/python

import threading
import time
import BaseHTTPServer
from BaseHTTPServer import HTTPServer
from urlparse import urlparse, parse_qs
from os import path
import gst
import gtk



HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 9000 # Maybe set this to 9000.

#################################################################
# VIDEO DICTIONARY
# Manages the video database
#################################################################

# VideoDictionary class
#################################################################
# This class allows to access the video database
# used by the video player - for best performance, it's a native
# python dictionary
class VideoDictionary():

    # declaring filenames
    filename = path.join(path.dirname(path.abspath(__file__)), 'large.mp4')
    filename_02 = path.join(path.dirname(path.abspath(__file__)), '01.avi')

    # declaring uris
    uri = 'file://' + filename
    uri_02 = 'file://' + filename_02

    # combining it all into a dictionary
    videoDict = {}
    videoDict["01"] = uri
    videoDict["02"] = uri_02

    # setting the current video
    currentVideo = "01"

#################################################################
# VIDEO DICTIONARY END
#################################################################



#################################################################
# VIDEO PLAYER
# Manages all the video playing
#################################################################

# VideoPlayer class
#################################################################
# This class initializes the GST pipe context and it
# handles different events related to video stream playing
class VideoPlayer(object, VideoDictionary):

    VideoDictionary = ""

    def __init__(self, VideoDictionary):
        self.VideoDictionary = VideoDictionary        
        self.window = gtk.Window()
        self.window.connect('destroy', self.quit)
        self.window.set_default_size(1024, 768)

        self.drawingarea = gtk.DrawingArea()
        self.window.add(self.drawingarea)

        # Create GStreamer pipeline
        self.pipeline = gst.Pipeline()

        # Create bus to get events from GStreamer pipeline
        self.bus = self.pipeline.get_bus()

        # This is needed to make the video output in our DrawingArea:
        self.bus.enable_sync_message_emission()
        self.bus.connect('sync-message::element', self.on_sync_message)

        # Create GStreamer elements
        self.playbin = gst.element_factory_make('playbin2')

        # Add playbin2 to the pipeline
        self.pipeline.add(self.playbin)
        self.window.show_all()
        self.xid = self.drawingarea.window.xid
        print('DEBUG INFO: player initialization finished')

    def playCurrent(self):
        print('DEBUG INFO: getting running video ')
        print(self.VideoDictionary.currentVideo)
        self.pipeline.set_state(gst.STATE_READY)
        self.playbin.set_property('uri', self.VideoDictionary.videoDict[self.VideoDictionary.currentVideo])
        self.pipeline.set_state(gst.STATE_PLAYING)

    def quit(self, window):
        print('DEBUG INFO: quitting player')
        self.pipeline.set_state(gst.STATE_NULL)
        gtk.main_quit()

    def on_sync_message(self, bus, msg):
        if msg.structure.get_name() == 'prepare-xwindow-id':
            msg.src.set_property('force-aspect-ratio', True)
            msg.src.set_xwindow_id(self.xid)

    def on_eos(self, bus, msg):
        print('DEBUG INFO: EOS detected')
        print('on_eos(): seeking to start of video')
        self.pipeline.seek_simple(
            gst.FORMAT_TIME,        
            gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_KEY_UNIT,
            0L
        )

    def on_error(self, bus, msg):
        print('DEBUG INFO: error detected')
        print('on_error():', msg.parse_error())

#################################################################
# VIDEO PLAYER END
#################################################################




#################################################################
# HTTP SERVER
# implements the http listener in a separate thread
# the listener plays the videos depending on the 
# received parameters in the GET request 
#################################################################

# HttpHandler class
#################################################################
# uses global variables to operate videos
class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):
        # initialize the currently played video
        global VideoDictionary
        print('DEBUG INFO: GET running playCurrent')
        if VideoDictionary.currentVideo == "01":
            VideoDictionary.currentVideo = "02"
        else:
            VideoDictionary.currentVideo = "01"

        # play the video we have just set        
        global player
        player.playCurrent()        

# HttpThread class
#################################################################
# initializes the http listener in a separate thread
class HttpThread (threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        gtk.gdk.threads_enter()
        server_class = BaseHTTPServer.HTTPServer
        httpd = server_class((HOST_NAME, PORT_NUMBER), HttpHandler)
        print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
        gtk.gdk.threads_leave()
        return

#################################################################
# HTTP SERVER END
#################################################################

if __name__ == '__main__':
    VideoDictionary = VideoDictionary()
    player = VideoPlayer(VideoDictionary)
    gtk.gdk.threads_init()
    thread2 = HttpThread()
    thread2.run()
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()
4

1 に答える 1

1

複数のビデオ ストリームを同時に再生するコードの簡単な例を次に示します。

Python 2 および 3 で動作し、標準の Python GUI (Tk) と Gstreamer 1.0 を使用します。したがって、移植可能である必要がありますが、Ubuntu 16.04 でのみテストしました。

(ffmpeg フォーク libav は Ubuntu 14.04 で問題を引き起こしましたが、これは 16.04 で解決されるようです。gstreamer1.0-plugins-* に加えてパッケージ gstreamer1.0-libav が必要であることに注意してください。)

このコードは、1 列に 8 つのフレームを作成し、Gstreamer プレーヤーを各フレームに関連付けるように構成されています。次のように、有効なローカル ビデオ ファイル名のリスト (最大 8 つ) を、それを保存したファイル (multivid.py など) への引数として指定する必要があります。

$ python3 multivid.py video1.webm video2.mp4

サウンドチャンネルは単純にミックスされています。おそらくこれを変更したいと思うでしょう。

私の解決策はリモート プレイには対応していませんが、その部分は既に解決しています。

以前、 tkinter のビデオ ファイルに関する別の質問への回答に同じコードを投稿しましたが、この質問では同時ストリームが求められませんでした。したがって、ここではより適切です。

import sys
import os

if sys.version_info[0] < 3:
    import Tkinter as tkinter
else:
    import tkinter

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

# Needed for set_window_handle():
gi.require_version('GstVideo', '1.0')
from gi.repository import GstVideo

def set_frame_handle(bus, message, frame_id):
    if not message.get_structure() is None:
        if message.get_structure().get_name() == 'prepare-window-handle':
            display_frame = message.src
            display_frame.set_property('force-aspect-ratio', True)
            display_frame.set_window_handle(frame_id)

NUMBER_OF_FRAMES = 8 # with more frames than arguments, videos are repeated
relative_height = 1 / float(NUMBER_OF_FRAMES)

# Only argument number checked, not validity.
number_of_file_names_given = len(sys.argv) - 1
if number_of_file_names_given < 1:
    print('Give at least one video file name.')
    sys.exit()
if number_of_file_names_given < NUMBER_OF_FRAMES:
    print('Up to', NUMBER_OF_FRAMES, 'video file names can be given.')
file_names = list()
for index in range(number_of_file_names_given):
    file_names.append(sys.argv[index + 1])

window = tkinter.Tk()
window.title("Multiple videos in a column using Tk and GStreamer 1.0")
window.geometry('480x960')

Gst.init(None)
GObject.threads_init()

for number in range(NUMBER_OF_FRAMES):
    display_frame = tkinter.Frame(window, bg='')
    relative_y = number * relative_height
    display_frame.place(relx = 0, rely = relative_y,
            anchor = tkinter.NW, relwidth = 1, relheight = relative_height)
    frame_id = display_frame.winfo_id()

    player = Gst.ElementFactory.make('playbin', None)
    fullname = os.path.abspath(file_names[number % len(file_names)])
    player.set_property('uri', 'file://%s' % fullname)
    player.set_state(Gst.State.PLAYING)

    bus = player.get_bus()
    bus.enable_sync_message_emission()
    bus.connect('sync-message::element', set_frame_handle, frame_id)

window.mainloop()

ハンドルをプレーヤーに保存する場合 (player_list など)、後で次のようにそのうちの 1 つで再生されている uri を変更できます。

player_list[index].set_state(Gst.State.NULL)
player_list[index].set_property('uri', 'file://%s' % fileName)
player_list[index].set_state(Gst.State.PLAYING)
于 2016-08-17T10:48:30.153 に答える