Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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
使用gstreamer1.0和Python播放.avi视频_Python_Windows_Python Gstreamer - Fatal编程技术网

使用gstreamer1.0和Python播放.avi视频

使用gstreamer1.0和Python播放.avi视频,python,windows,python-gstreamer,Python,Windows,Python Gstreamer,我已经搜索了很多在Windows上使用GStreamer的python库的例子,但只有这样的例子工作得很好: #!/usr/bin/env python3 # -*- coding:utf-8 -*- # GStreamer SDK Tutorials in Python # # basic-tutorial-2 # """ basic-tutorial-2: GStreamer concepts http://docs.gstreamer.com/display/GstSDK/Basi

我已经搜索了很多在Windows上使用GStreamer的python库的例子,但只有这样的例子工作得很好:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# GStreamer SDK Tutorials in Python
#
#     basic-tutorial-2
#
"""
basic-tutorial-2: GStreamer concepts
http://docs.gstreamer.com/display/GstSDK/Basic+tutorial+2%3A+GStreamer+concepts
"""

import sys
from gi.repository import Gst
Gst.init(None)

# Create the elements
source = Gst.ElementFactory.make("videotestsrc", "source")
sink = Gst.ElementFactory.make("autovideosink", "sink")

# Create the empty pipeline
pipeline = Gst.Pipeline.new("test-pipeline")


if not source or not sink or not pipeline:
    print("Not all elements could be created.", file=sys.stderr)
    exit(-1)

# Build the pipeline
pipeline.add(source)
pipeline.add(sink)
if not Gst.Element.link(source, sink):
    print("Elements could not be linked.", file=sys.stderr)
    exit(-1)

# Modify the source's properties
source.set_property("pattern", 0)

# Start playing
ret = pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
    print("Unable to set the pipeline to the playing state.", file=sys.stderr)
    exit(-1)

# Wait until error or EOS
bus = pipeline.get_bus()
msg = bus.timed_pop_filtered(
    Gst.CLOCK_TIME_NONE, Gst.MessageType.ERROR | Gst.MessageType.EOS)

# Parse message
if (msg):
    if msg.type == Gst.MessageType.ERROR:
        err, debug = msg.parse_error()
        print("Error received from element %s: %s" % (
            msg.src.get_name(), err), file=sys.stderr)
        print("Debugging information: %s" % debug, file=sys.stderr)
    elif msg.type == Gst.MessageType.EOS:
        print("End-Of-Stream reached.")
    else:
        print("Unexpected message received.", file=sys.stderr)

# Free resources
pipeline.set_state(Gst.State.NULL)
这是一个简单的测试,只使用一个“sink对象”,或者是流式传输GStreamer示例文件(Sintel电影预告片)的对象,如下所示:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# GStreamer SDK Tutorials in Python
#
#     basic-tutorial-4
#
"""
basic-tutorial-4: Time management
http://docs.gstreamer.com/display/GstSDK/Basic+tutorial+4%3A+Time+management
"""

import sys
from gi.repository import Gst
Gst.init(None)

# Python version of GST_TIME_ARGS


def convert_ns(t):
    s, ns = divmod(t, 1000000000)
    m, s = divmod(s, 60)

    if m < 60:
        return "0:%02i:%02i.%i" % (m, s, ns)
    else:
        h, m = divmod(m, 60)
        return "%i:%02i:%02i.%i" % (h, m, s, ns)


def handle_message(data, msg):
    if message.type == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        print("Error received from element %s: %s" % (message.src.get_name(), err), file=sys.stderr)
        print("Debugging information: %s" % debug, file=sys.stderr)
        data["terminate"] = True
    elif message.type == Gst.MessageType.EOS:
        print("End-Of-Stream reached.")
        data["terminate"] = True
    elif message.type == Gst.MessageType.DURATION_CHANGED:
        # The duration has changed, mark the current one as invalid
        data["duration"] = Gst.CLOCK_TIME_NONE
    elif message.type == Gst.MessageType.STATE_CHANGED:
        if message.src == data["playbin"]:
            old_state, new_state, pending_state = message.parse_state_changed()
            print("Pipeline state changed from %s to %s." % (old_state.value_nick, new_state.value_nick))
            data["playing"] = (new_state == Gst.State.PLAYING)
            if data["playing"]:
                query = Gst.Query.new_seeking(Gst.Format.TIME)
                if data["playbin"].query(query):
                    (aux, data["seek_enabled"], start, end) = query.parse_seeking()
                    if data["seek_enabled"]:
                        print("Seeking is ENABLED from %s to %s" % (convert_ns(start), convert_ns(end)))
                    else:
                        print("Seeking is DISABLED for this stream.")
                else:
                    print("Seeking query failed.", file=sys.stderr)
    else:
        print("Unexpected message received.", file=sys.stderr)


data = dict()

data["playing"] = False
data["terminate"] = False
data["seek_enabled"] = False
data["seek_done"] = False
data["duration"] = Gst.CLOCK_TIME_NONE

# Create the elements
data["playbin"] = Gst.ElementFactory.make("playbin", "playbin")

if not data["playbin"]:
    print("Not all elements could be created.", file=sys.stderr)
    exit(-1)

# Set the URI to play
data["playbin"].set_property(
    "uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.webm")

# Start playing
ret = data["playbin"].set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
    print("Unable to set the pipeline to the playing state.", file=sys.stderr)
    exit(-1)

# Listen to the bus
bus = data["playbin"].get_bus()
while not data["terminate"]:
    message = bus.timed_pop_filtered(100 * Gst.MSECOND,
                                     Gst.MessageType.STATE_CHANGED | 
                                     Gst.MessageType.ERROR | 
                                     Gst.MessageType.EOS | 
                                     Gst.MessageType.DURATION_CHANGED)

    # Parse message
    if message:
        handle_message(data, message)
    else:
        if data["playing"]:
            fmt = Gst.Format.TIME
            current = -1
            # Query the current position of the stream
            _, current = data['playbin'].query_position(fmt)
            if not current:
                print("Could not query current position", file=sys.stderr)

            # If we didn't know it yet, query the stream duration
            if data["duration"] == Gst.CLOCK_TIME_NONE:
                _, data["duration"] = data['playbin'].query_duration(fmt)
                if not data["duration"]:
                    print("Could not query current duration", file=sys.stderr)

            print("Position %s / %s\r" % (
                convert_ns(current), convert_ns(data["duration"])), end=' ')
            sys.stdout.flush()

            # If seeking is enabled, we have not done it yet, and the time is
            # right, seek
            if data["seek_enabled"] and not data["seek_done"] and current > 10 * Gst.SECOND:
                print("\nReached 10s, performing seek...")
                data['playbin'].seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 30 * Gst.SECOND)
                data["seek_done"] = True


# Free resources
data["playbin"].set_state(Gst.State.NULL)
#/usr/bin/env蟒蛇3
#-*-编码:utf-8-*-
#Python中的GStreamer SDK教程
#
#基础教程-4
#
"""
基础教程-4:时间管理
http://docs.gstreamer.com/display/GstSDK/Basic+教程+4%3A+时间+管理
"""
导入系统
从gi.repository导入Gst
Gst.init(无)
#GST\u TIME\u ARGS的Python版本
def转换(t):
s、 ns=divmod(t,100000000)
m、 s=divmod(s,60)
如果m<60:
返回“0:%02i:%02i.%i”%(m、s、ns)
其他:
h、 m=divmod(m,60)
返回“%i:%02i:%02i.%i”%(h、m、s、ns)
def handle_消息(数据、消息):
如果message.type==Gst.MessageType.ERROR:
err,debug=message.parse_error()
打印(“从元素%s接收到错误:%s”%(message.src.get_name(),err),file=sys.stderr)
打印(“调试信息:%s”%debug,file=sys.stderr)
数据[“终止”]=True
elif message.type==Gst.MessageType.EOS:
打印(“已到达流的末尾”)
数据[“终止”]=True
elif message.type==Gst.MessageType.DURATION\u已更改:
#持续时间已更改,请将当前持续时间标记为无效
数据[“持续时间”]=Gst.CLOCK\u TIME\u无
elif message.type==Gst.MessageType.STATE\u已更改:
如果message.src==data[“playbin”]:
旧状态、新状态、挂起状态=message.parse\u state\u changed()
打印(“管道状态从%s更改为%s”。%(旧的状态值、新的状态值)
数据[“播放”]=(新的州==Gst.state.playing)
如果数据[“播放”]:
query=Gst.query.new\u seeking(Gst.Format.TIME)
如果数据[“playbin”]。查询(查询):
(aux,data[“seek_enabled”],start,end)=query.parse_seek()
如果数据[“已启用搜索”]:
打印(“从%s到%s已启用查找%(转换(开始)、转换(结束)))
其他:
打印(“此流已禁用搜索。”)
其他:
打印(“查找查询失败”,file=sys.stderr)
其他:
打印(“收到意外消息”,file=sys.stderr)
数据=dict()
数据[“播放”]=错误
数据[“终止”]=False
数据[“已启用搜索”]=错误
数据[“搜索完成”]=错误
数据[“持续时间”]=Gst.CLOCK\u TIME\u无
#创建元素
数据[“playbin”]=Gst.ElementFactory.make(“playbin”,“playbin”)
如果不是数据[“playbin”]:
打印(“并非所有元素都可以创建。”,file=sys.stderr)
出口(-1)
#设置要播放的URI
数据[“playbin”]。设置_属性(
“uri”http://docs.gstreamer.com/media/sintel_trailer-480p.webm")
#开始玩
ret=data[“playbin”]。设置状态(Gst.state.PLAYING)
如果ret==Gst.StateChangeReturn.FAILURE:
打印(“无法将管道设置为播放状态。”,file=sys.stderr)
出口(-1)
#听公共汽车
总线=数据[“playbin”]。获取总线()
虽然不是数据[“终止”]:
消息=总线定时弹出过滤(100*Gst.MSECOND,
Gst.MessageType.STATE|u已更改
Gst.MessageType.ERROR|
Gst.MessageType.EOS|
Gst.MessageType.DURATION(已更改)
#解析消息
如果消息:
处理消息(数据、消息)
其他:
如果数据[“播放”]:
fmt=Gst.Format.TIME
电流=-1
#查询流的当前位置
_,当前=数据['playbin']。查询位置(fmt)
如果不是最新的:
打印(“无法查询当前位置”,file=sys.stderr)
#如果我们还不知道,查询流持续时间
如果数据[“持续时间”]==Gst.CLOCK\u TIME\u无:
_,数据[“持续时间”]=数据['playbin']。查询持续时间(fmt)
如果不是数据[“持续时间”]:
打印(“无法查询当前持续时间”,文件=sys.stderr)
打印(“位置%s/%s\r”%(
转换(当前),转换(数据[“持续时间”),结束=“”)
sys.stdout.flush()
#如果启用了查找,则我们尚未执行此操作,并且时间已确定
#对,寻求
如果数据[“seek_enabled”]而不是数据[“seek_done”]且当前>10*Gst.SECOND:
打印(“\n搜索10秒,执行搜索…”)
数据['playbin'].seek_simple(Gst.Format.TIME,Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_单位,30*Gst.SECOND)
数据[“搜索完成”]=真
#免费资源
数据[“playbin”]。设置_状态(Gst.state.NULL)
因此,在尝试更改代码后,我有一些问题:

  • 这是吗?”file:///C:/folder/video.avi“GStreamer的正确路径格式
  • 我知道我必须使用一个“playerbin”对象并设置它的uri属性,但我不知道应该把这些行放在哪里,以及如何正确使用它
  • 视频格式是否有问题
  • 非常感谢你的帮助

  • 看起来不错,但我无法测试它,因为我使用Linux
  • 更改行:
    data[“playbin”]设置属性(
    “uri”http://docs.gstreamer.com/media/sintel_trailer-480p.webm”


    data[“playbin”]。设置_属性(“uri”file:///C:/folder/video.avi“”
  • 不,应该没问题,playbin会带着你扔给它的任何东西跑

  • 查看可修改的playbin属性的步骤