I’m using gstreamer in Python to read MP4 videos, when I press the record button and then stop recording it generates an empty file

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật