Skip to content

Instantly share code, notes, and snippets.

@michaelgruner
Created November 6, 2021 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelgruner/18999d8e212b6ce2b4a80113ceaafe6f to your computer and use it in GitHub Desktop.
Save michaelgruner/18999d8e212b6ce2b4a80113ceaafe6f to your computer and use it in GitHub Desktop.
Create a video with frame number and timestamp overlayed.
#!/usr/bin/env python3
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GLib', '2.0')
from gi.repository import Gst, GLib
FILE="/tmp/test.mp4"
def main():
Gst.init ()
pipe = Gst.parse_launch ("videotestsrc ! identity name=i ! video/x-raw,width=1920,height=1080 ! textoverlay halignment=center name=overlay ! queue ! x264enc ! qtmux ! filesink location=%s"%FILE)
overlay = pipe.get_by_name ("overlay")
identity = pipe.get_by_name ("i")
# Change overlay with each buffer's info
identity.connect("handoff",
lambda elem, buf:
overlay.set_property("text", "offset: %d, offset-end: %d, dts: %d, pts: %d"%(buf.offset, buf.offset_end, buf.pts, buf.dts)))
pipe.set_state (Gst.State.PLAYING);
loop = GLib.MainLoop()
try:
print("Running, ctrl+c to stop...")
loop.run()
except:
print("Closing...")
loop.quit()
# And EOS is needed to write the MP4 headers and have a playable files
pipe.send_event(Gst.Event.new_eos())
pipe.get_bus().timed_pop_filtered(15000000000, Gst.MessageType.EOS)
pipe.set_state (Gst.State.NULL);
Gst.deinit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment