我正在尝试编写一个项目,我至少有 20 个 rtsp CCTV URL 可以同时访问。
我尝试使用 ffmpeg 通过多种输入法达到我的目标。但是,有一个问题。
ffmpeg -i URL_1 -i URL_2 -
上面的命令是我试过的例子。我希望我可以通过 ffmpeg 访问两个 rtsps 并将它们输出到两个不同的队列中以供将来使用。如果我使用此命令并在此之后读取字节,我无法区分哪些字节属于哪个输入 rtsp。
有没有其他方法可以同时访问更多 rtsp?
编辑:添加代码
import ffmpeg
import numpy as np
import subprocess as sp
import threading
import queue
import time
class CCTVReader(threading.Thread):
def __init__(self, q, in_stream, name):
super().__init__()
self.name = name
self.q = q
self.command = ["ffmpeg",
"-c:v", "h264", # Tell ffmpeg that input stream codec is h264
"-i", in_stream, # Read stream from file vid.264
"-c:v", "copy", # Tell ffmpeg to copy the video stream as is (without decding and encoding)
"-an", "-sn", # No audio an no subtites
"-f", "h264", # Define pipe format to be h264
"-"] # Output is a pipe
def run(self):
pipe = sp.Popen(self.command, stdout=sp.PIPE, bufsize=1024**3) # Don't use shell=True (you don't need to execute the command through the shell).
# while True:
for i in range(1024*10): # Read up to 100KBytes for testing
data = pipe.stdout.read(1024) # Read data from pip in chunks of 1024 bytes
self.q.put(data)
# Break loop if less than 1024 bytes read (not going to work with CCTV, but works with input file)
if len(data) < 1024:
break
try:
pipe.wait(timeout=1) # Wait for subprocess to finish (with timeout of 1 second).
except sp.TimeoutExpired:
pipe.kill() # Kill subprocess in case of a timeout (there should be a timeout because input stream still lives).
if self.q.empty():
print("There is a problem (queue is empty)!!!")
else:
# Write data from queue to file vid_from_queue.264 (for testingg)
with open(self.name+".h264", "wb") as queue_save_file:
while not self.q.empty():
queue_save_file.write(self.q.get())
# Build synthetic video, for testing begins:
################################################
# width, height = 1280, 720
# in_stream = "vid.264"
# sp.Popen("ffmpeg -y -f lavfi -i testsrc=size=1280x720:duration=5:rate=1 -c:v libx264 -crf 23 -pix_fmt yuv420p " + in_stream).wait()
################################################
#Use public RTSP Streaming for testing
readers = {}
queues = {}
dict = {
"name1":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name2":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name3":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name4":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name5":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name6":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name7":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name8":{"ip":"rtsp://xxx.xxx.xxx.xxx/",
"name9":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name10":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name11":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name12":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name13":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name14":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
"name15":{"ip":"rtsp://xxx.xxx.xxx.xxx/"},
}
for key in dict:
ip = dict[key]["ip"]
name = key
q = queue.Queue()
queues[name] = q
cctv_reader = CCTVReader(q, ip, name)
readers[name] = cctv_reader
cctv_reader.start()
cctv_reader.join()