I have H264 hex string data saved in a list. The data is in correct format as it is being received, I am trying to stream it to RTSP server.
I have stream the data in realtime as it is from a dashcam.
The RTSP server is deployed but when I stream frames to it, the connection is created and then ends in an instant (does not last for a second)
The code is mentioned below which performs this streaming task.
def h264_stream_to_rtsp(data_list, rtsp_url):
try:
ffmpeg_command = [
"ffmpeg",
"-f", "h264",
"-i", "-",
"-vcodec", "libx264",
"-preset", "fast",
"-f", "rtsp",
"-analyzeduration", "5000000",
"-probesize", "5000000",
rtsp_url # The RTSP URL to stream to
]
ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE)
for index, hex_data in enumerate(data_list):
# print(f"Processing hex data {index + 1}/{len(data_list)}...")
if len(hex_data) % 2 != 0:
hex_data = '0' + hex_data # Append a leading zero if length is odd
binary_data = binascii.unhexlify(hex_data)
ffmpeg_process.stdin.write(binary_data)
ffmpeg_process.stdin.close()
ffmpeg_process.wait()
print("Stream completed.")
except KeyboardInterrupt:
print("Stopping live stream.")
except Exception as e:
print(f"Error: {e}")
Logs from the RTSP server are mentioned below:
2025/01/01 10:56:04 INF [RTSP] [conn 20.174.9.78:35474] opened
2025/01/01 10:56:04 INF [RTSP] [session 1d2bb871] created by 20.174.9.78:35474
2025/01/01 10:56:04 INF [RTSP] [session 1d2bb871] is publishing to path 'live', 1 track (H264)
2025/01/01 10:56:04 INF [RTSP] [session 1d2bb871] destroyed: torn down by 20.174.9.78:35474
2025/01/01 10:56:04 INF [RTSP] [conn 20.174.9.78:35474] closed: EOF
2025/01/01 10:56:48 INF [RTSP] [conn 20.174.9.78:54448] opened
2025/01/01 10:56:48 INF [RTSP] [session b6a95e71] created by 20.174.9.78:54448
2025/01/01 10:56:48 INF [RTSP] [session b6a95e71] is publishing to path 'live', 1 track (H264)
2025/01/01 10:56:48 INF [RTSP] [session b6a95e71] destroyed: torn down by 20.174.9.78:54448
How can I stream continuously?