I’m working on reading MP4 video files using Gstreamer in Python. However, I’m encountering an issue where pressing the record button followed by the stop recording button results in an empty output file. Can someone assist me with troubleshooting this problem, please? thanks in advance
import gi
from gi.repository import GObject, Gst, Gtk
Gst.init(None)
Gtk.init(None)
video_file = "/host_dir/people.mp4"
filesrc = Gst.ElementFactory.make("filesrc", "source")
filesrc.set_property("location", video_file)
qtdemux = Gst.ElementFactory.make("qtdemux", "demux")
h264parse = Gst.ElementFactory.make("h264parse", "parser")
nvdecoder = Gst.ElementFactory.make("nvv4l2decoder", "decoder")
nvconvert = Gst.ElementFactory.make("nvvideoconvert", "converter")
videoflip = Gst.ElementFactory.make("videoflip", "flip")
nvsink = Gst.ElementFactory.make("nveglglessink", "sink")
tee = Gst.ElementFactory.make("tee", "tee")
pipeline = Gst.Pipeline.new("mypipeline")
for element in [filesrc, qtdemux, h264parse, nvdecoder, nvconvert, videoflip, nvsink, tee]:
pipeline.add(element)
filesrc.link(qtdemux)
def on_pad_added(demux, pad):
pad.link(h264parse.get_static_pad("sink"))
qtdemux.connect("pad-added", on_pad_added)
h264parse.link(nvdecoder)
nvdecoder.link(nvconvert)
nvconvert.link(tee)
tee.link(videoflip)
videoflip.link(nvsink)
pipeline.set_state(Gst.State.PLAYING)
def record_button_clicked(button):
if button.get_label() == "Record":
button.set_label("Stop Recording")
start_recording()
else:
button.set_label("Record")
stop_recording()
def start_recording():
print("Started recording")
videoconvert_record = Gst.ElementFactory.make("nvvideoconvert", "videoconvert_record")
filesink = Gst.ElementFactory.make("filesink", "filesink")
filesink.set_property("location", "/host_dir/recorded_video_1.mp4")
pipeline_record = Gst.Pipeline.new("recording_pipeline")
# Add elements to the recording pipeline
for element in [videoconvert_record, filesink]:
pipeline_record.add(element)
videoconvert_record_sink_pad = videoconvert_record.get_static_pad("sink")
filesink_sink_pad = filesink.get_static_pad("sink")
videoconvert_record_sink_pad.set_active(False)
ghost_pad = Gst.GhostPad.new("sink", videoconvert_record_sink_pad)
filesink.add_pad(ghost_pad)
ghost_pad.link(filesink_sink_pad)
videoconvert_record_sink_pad.set_active(True)
pipeline_record.set_state(Gst.State.PLAYING)
def stop_recording():
print("Stopped recording")
fsink = pipeline.get_by_name("filesink")
vconvert = pipeline.get_by_name("videoconvert_record")
if fsink is not None:
pipeline.remove(fsink)
if vconvert is not None:
pipeline.remove(vconvert)
tee_src_pad = tee.get_static_pad("src_1")
if tee_src_pad is not None:
tee.release_request_pad(tee_src_pad)
window = Gtk.Window()
window.set_default_size(200, 100)
window.connect("destroy", Gtk.main_quit)
recording_button = Gtk.Button.new_with_label("Record")
recording_button.connect("clicked", record_button_clicked)
window.add(recording_button)
recording_button.show()
window.show_all()
Gtk.main()
pipeline.set_state(Gst.State.NULL)
I tried everything but still didn’t work
New contributor
Ammar Abu Zainah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.