Skip to content

Instantly share code, notes, and snippets.

@shonenada
Created December 15, 2022 06:27
Show Gist options
  • Save shonenada/23691c0a25a12bdbd36e5445557073db to your computer and use it in GitHub Desktop.
Save shonenada/23691c0a25a12bdbd36e5445557073db to your computer and use it in GitHub Desktop.
Push random image to RTSP stream
import time
import subprocess
import sys
import numpy as np
FPS = 25
WIDTH = 1280
HEIGHT = 720
PUSH_URL = "rstp://127.0.0.1:554/demo"
def create_random_image(width=WIDTH, height=HEIGHT):
img_color = np.random.rand(3, height, width, 3) * 255
return img_color.astype(np.uint8)
def create_ffmpeg_pipe(push_url):
command = [
'ffmpeg',
'-loglevel', 'error',
'-c',
'-y',
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', f"{WIDTH}x{HEIGHT}",
'-r', str(FPS),
'-i', '-',
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-rtsp_transport', 'tcp',
'-f', 'rtsp',
push_url]
p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=sys.stdout)
return p
def main():
print(f"Connect to {PUSH_URL}")
p = create_ffmpeg_pipe(PUSH_URL)
while True:
frame = create_random_image()
print(frame[0][0][0])
p.stdin.write(frame.tobytes())
time.sleep(1/FPS)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment