[Script]
#!/usr/bin/env python3
# test-rtsp-publish-from-file.py
import cv2, numpy as np, sys
# ------------------- CONFIG -------------------
input_file = sys.argv[1] if len(sys.argv) > 1 else “sample.mp4”
out_url = sys.argv[2] if len(sys.argv) > 2 else “rtsp://127.0.0.1:8554/gst-pub-test”
codec = (sys.argv[3] if len(sys.argv) > 3 else “h265”).lower() # h264|h265
if codec not in (“h264”, “h265”):
raise SystemExit(“codec must be h264 or h265”)
enc = f"nvv4l2{codec}enc"
parse = f"{codec}parse"
# ------------------- OPEN INPUT VIDEO -------------------
cap = cv2.VideoCapture(input_file)
if not cap.isOpened():
raise SystemExit(f"Cannot open input file: {input_file}")
W = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
H = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
FPS = cap.get(cv2.CAP_PROP_FPS) or 25 # fallback if zero
print(f"Input: {input_file} → {W}x{H} @ {FPS:.2f} fps")
# ------------------- GStreamer pipeline (same as before) -------------------
PIPE = (
f"appsrc is-live=true block=true format=time do-timestamp=true "
f"caps=video/x-raw,format=BGR,width={W},height={H},framerate={FPS:.2f}/1 ! "
"videoconvert ! video/x-raw,format=NV12 ! "
"nvvidconv ! video/x-raw(memory:NVMM),format=NV12 ! "
f"{enc} insert-sps-pps=true maxperf-enable=true iframeinterval={int(FPS)} bitrate=4000000 ! "
f"{parse} ! "
f"rtspclientsink location={out_url} protocols=tcp do-rtsp-keep-alive=true latency=0"
)
out = cv2.VideoWriter(PIPE, cv2.CAP_GSTREAMER, 0, FPS, (W, H))
print(f"[{codec.upper()}] Publishing to → {out_url}")
if not out.isOpened():
raise SystemExit(“Failed to open RTSP publish pipeline”)
# ------------------- MAIN LOOP -------------------
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
print(“End of input file or error reading frame”)
break
out.write(frame)
frame_count += 1
if frame_count % 30 == 0:
print(f"Published {frame_count} frames…")
cap.release()
out.release()
print(“Finished publishing. Play with:”)
print(f" gst-play-1.0 {out_url}")
print(f" or ffplay {out_url}")
[Env]
=== Jetson model ===
NVIDIA Jetson Orin NX Engineering Reference Developer Kit=== L4T/JetPack ===
R36 (release), REVISION: 4.3, GCID: 38968081, BOARD: generic, EABI: aarch64, DATE: Wed Jan 8 01:49:37 UTC 2025
nvidia-l4t-core 36.4.3-20250107174145
=== Kernel ===
Linux yuan-6n0cnx 5.15.148-tegra #1 SMP PREEMPT Thu May 15 09:27:14 CST 2025 aarch64 aarch64 aarch64 GNU/Linux
=== GStreamer ===
gst-inspect-1.0 version 1.20.3
Factory Details:
Factory Details:
=== OpenCV ===
OpenCV: 4.5.4
GStreamer: YES
=== MediaMTX ===
v1.15.3