GStreamer Python 录制并保存桌面视频

GStreamer Python record and save desktop video

我有以下命令行代码:

gst-launch-1.0 ximagesrc startx=0 use-damage=0 ! video/x-raw,framerate=30/1 ! videoscale method=0 ! video/x-raw,width=1280,height=1080  ! ximagesink

是否可以在 python 中使用 gstreamer 来记录其从桌面输出的视频并将其保存到文件中。该命令打开 window 并正确显示桌面 activity 但我想将此输出转换为 python 中的视频文件。

提前感谢您的帮助。

根据系统上可用的编解码器和 gstreamer 的其他元素,您可能会遇到问题,但这对我有用:

gst-launch-1.0 ximagesrc startx=0 use-damage=0 ! video/x-raw,framerate=30/1 ! videoscale method=0 ! video/x-raw,width=1280,height=1080  ! videoconvert ! x264enc ! avimux ! filesink location=output2.avi

因此需要 videoconvert,因为 x264enc 的输入类型与 ximagesink 不同。 x264enc本身就是编解码器,avimux将压缩后的视频放在一个avi容器中,filesink用于写出文件。

对于 Python API,一个简单的 MCVE 是:

#!/usr/bin/python

import os
import gi
import sys
import time

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

Gtk.init(sys.argv)

# initialize GStreamer
Gst.init(sys.argv)

pipeline = Gst.parse_launch ("ximagesrc startx=0 use-damage=0 ! video/x-raw,framerate=30/1 ! videoscale method=0 ! video/x-raw,width=1280,height=1080  ! videoconvert ! x264enc ! avimux ! filesink location=output4.avi")
pipeline.set_state(Gst.State.PLAYING)
time.sleep(15)
pipeline.set_state(Gst.State.NULL)