使用Python和管道的AVI视频和音频播放器不工作

2024-10-02 00:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在寻找使用avidemux、管道和python来显示来自单个avi视频文件的视频和音频的方法。我可以使用gst-launch实现它,但我想用代码实现它。现在它说所有的元素都被创建了,但是它不能从avidemux或解码器访问任何pad的动态分配。在

下面是python的全部代码。在

谢谢

    #!/usr/bin/env python
    import gobject, pygst
    pygst.require("0.10")
    import gst


    def allocate_muxer_pad(dbin, pad, islast):
        print "allocate prog entered"   

        if pad.get_caps()[0].to_string().startswith("a"):
            pad.link(audioqueue.get_pad("sink"))
            print 'audio Mux connected' 
        elif pad.get_caps()[0].to_string().startswith("v"):  
            pad.link(videoqueue.get_pad("sink"))    
            print 'Video Mux connected'

    def new_Adecode_pad(dbin, pad, islast):
        pad.link(audioconvert.get_pad("sink"))  
        print 'audio decode connected'

    def new_Vdecode_pad(dbin, pad, islast):  
        pad.link(videoconvert.get_pad("sink"))
        print 'video decode connected'

    pipeline = gst.Pipeline("PIPELINE")
    Bin = gst.Bin("pipeline")

    src = gst.element_factory_make("filesrc", "source")
    src.set_property("location", "testav.avi")

    demux = gst.element_factory_make("avidemux","avi-demuxer")
    audioqueue = gst.element_factory_make('queue', 'Audioqueue')
    videoqueue = gst.element_factory_make('queue','videoqueue')
    audiodecoder = gst.element_factory_make("decodebin2","Adecoder")
    videodecoder = gst.element_factory_make("decodebin2","Vdecoder")
    audioconverter = gst.element_factory_make("audioconvert","Audio_Converter") 
    videoconverter = gst.element_factory_make("ffmpegcolorspace","Video_Converter")
    audioSink = gst.element_factory_make("autoaudiosink","Sink_Audio")
    videoSink = gst.element_factory_make("ximagesink","Sink_Video")

    Bin.add(src)
    Bin.add_many(demux,audioqueue,videoqueue,audiodecoder,audioconverter,videodecoder,videoconverter,audioSink,videoSink)

    src.link(demux)
    gst.element_link_many(audioqueue,audiodecoder)
    gst.element_link_many(audioconverter,audioSink)
    gst.element_link_many(videoqueue,videodecoder)
    gst.element_link_many(videoconverter,videoSink)

    if (not(src) or not(demux) or not(audioqueue) or not(videoqueue) or not(videodecoder) or not(audiodecoder) or not(audioconverter) or not(videoconverter) or not(audioSink) or not(videoSink) ):
        print "Elements not Created"
    else:
        print "Elements Created"


    demux.connect("pad-added", allocate_muxer_pad)
    demux.connect("pad-added", allocate_muxer_pad)

    audiodecoder.connect("new-decoded-pad", new_Adecode_pad)
    videodecoder.connect("new-decoded-pad", new_Vdecode_pad)


    pipeline.set_state(gst.STATE_PLAYING)
    print "Pipeline Playing" 
    # enter into a mainloop
    loop = gobject.MainLoop()

    loop.run()

Tags: orsrcnewgetmakefactorylinknot

热门问题