为什么使用 python3 和 GStreamer 混合 mp4 和 webm 失败:原因未链接 (-1)

问题描述 投票:0回答:0

我以前从未使用过GStreamer,我也不是c++开发者。这让我非常困惑,但我真的需要实现这个功能,而 ffmpeg 的执行速度太慢,无法满足我的需求。我从多个来源了解到,使用 G Streamer 可以更快地将视频混合成其他格式和分辨率。最终,我打算将其编写为一个微服务,供我的 golang 应用程序与之互操作,但与此同时,我什至无法将一个完全有效的格式化视频转换为 4 种不同的比特率和分辨率。有人可以向我提供一些关于正在发生的事情的见解,请记住我几乎不知道或不了解 G Streamer 的任何功能吗?

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

Gst.init(None)

input_file = 'C:/msys64/home/User/share/BlueMeringue/media/input/upload_originalaETX.mp4'
output_path = 'C:/msys64/home/User/share/BlueMeringue/media/output/upload_dist'
resolutions = [240, 480, 720, 1080]
bitrates = [300, 600, 1200, 2400]

for i, res in enumerate(resolutions):
    # Create a pipeline for each resolution and output format
    pipeline = Gst.Pipeline()

    # Create elements for reading input file and writing output files
    filesrc = Gst.ElementFactory.make("filesrc", "filesrc")
    filesrc.set_property("location", input_file)

    decode = Gst.ElementFactory.make("decodebin", "decode")

    queue = Gst.ElementFactory.make("queue", "queue")

    filter = Gst.ElementFactory.make("capsfilter", "filter")
    filter.set_property("caps", Gst.Caps.from_string(f"video/x-raw,format=I420,width={res},height={res},framerate=30/1"))

    encoder = Gst.ElementFactory.make("x264enc", "encoder")
    encoder.set_property("speed-preset", 1)
    encoder.set_property("tune", 0x00000004)
    encoder.set_property("bitrate", bitrates[i])
    encoder.set_property("key-int-max", 15)

    mux = Gst.ElementFactory.make("mp4mux", "mux")

    filesink = Gst.ElementFactory.make("filesink", "filesink")
    filesink.set_property("location", f"{output_path}_{res}p.mp4")

    # Add elements to the pipeline
    pipeline.add(filesrc)
    pipeline.add(decode)
    pipeline.add(queue)
    pipeline.add(filter)
    pipeline.add(encoder)
    pipeline.add(mux)
    pipeline.add(filesink)

    # Link elements together
    filesrc.link(decode)
    decode.link(queue)
    queue.link(filter)
    filter.link(encoder)
    encoder.link(mux)
    mux.link(filesink)

    # Set pipeline to playing state
    pipeline.set_state(Gst.State.PLAYING)

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

    pipeline.send_event(Gst.Event.new_eos())

    if msg:
        if msg.type == Gst.MessageType.ERROR:
            err, debug = msg.parse_error()
            print(f"Error: {err}. Debug: {debug}")
        elif msg.type == Gst.MessageType.EOS:
            print(f"Pipeline for resolution {res}p and bitrate {bitrates[i]} completed successfully.")
    else:
        print("Unknown error occurred.")

    # Clean up
    pipeline.set_state(Gst.State.NULL)

这个运行时产生的错误是:

Error: gst-stream-error-quark: Internal data stream error. (1). Debug: ../gst-plugins-good-1.22.0/gs
t/isomp4/qtdemux.c(6937): gst_qtdemux_loop (): /GstPipeline:pipeline0/GstDecodeBin:decode/GstQTDemux
:qtdemux0:
streaming stopped, reason not-linked (-1)
Error: gst-stream-error-quark: Internal data stream error. (1). Debug: ../gst-plugins-good-1.22.0/gs
t/isomp4/qtdemux.c(6937): gst_qtdemux_loop (): /GstPipeline:pipeline1/GstDecodeBin:decode/GstQTDemux
:qtdemux1:
streaming stopped, reason not-linked (-1)
Error: gst-stream-error-quark: Internal data stream error. (1). Debug: ../gst-plugins-good-1.22.0/gs
t/isomp4/qtdemux.c(6937): gst_qtdemux_loop (): /GstPipeline:pipeline2/GstDecodeBin:decode/GstQTDemux
:qtdemux2:
streaming stopped, reason not-linked (-1)
Error: gst-stream-error-quark: Internal data stream error. (1). Debug: ../gst-plugins-good-1.22.0/gs
t/isomp4/qtdemux.c(6937): gst_qtdemux_loop (): /GstPipeline:pipeline3/GstDecodeBin:decode/GstQTDemux
:qtdemux3:
streaming stopped, reason not-linked (-1)

我期望它能根据输入视频以不同的比特率和分辨率输出 4 个视频。 我曾尝试清理文档,但我不理解其中的任何内容,而且每个人似乎都有完全不同的设置和环境,而且由于复杂的 GStreamer 术语对我来说没有意义。

python-3.x mingw media pygobject python-gstreamer
© www.soinside.com 2019 - 2024. All rights reserved.