def catch_video(duration): def one_frame(codecName, bytes, sec, usec, durUSec): print('frame for %s: %d bytes' % (codecName, len(bytes))) f_video.write(b'\0\0\0\1' + bytes) seconds = float(duration) file_video = '/home/relea/scripts/' + datetime.now().strftime("%Y-%m-%d %H:%M:%S\n") + '.mp4' url = 'rtsp://kgti.ru:7447/e8bd304e-56e9-32c6-aa70-d3fabecfcdb6_0' with open(file_video,'wb') as f_video: useTCP = True live555.startRTSP(url, one_frame, useTCP) t = multiprocessing.Process(target=live555.runEventLoop, args=()) t.start() endTime = time.time() + seconds while time.time() < endTime: time.sleep(0.1) live555.stopEventLoop() t.terminate() return file_video
def realtime_swift_stream(self, channel=1, typeno=0): stored_exception = None # Create a new buffer for streaming global q q = BufferQueue.BufferQueue(globalVars.bufferSize) # Counter to maintain upload size global bytecount bytecount = 0 # Start the producer thread live555.startRTSP(cameraURL, fill_queue, False) producer = threading.Thread(target=live555.runEventLoop) producer.setDaemon(True) producer.start() # Loop until the break signal has been received try: while True: if stored_exception: break # Setup the filename for the uploaded segment ts = time.time() fileName = datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') fileName = cameraName + " " + fileName # Retrieve authentication for swift and setup storage location my_token_id = chameleonAuth.auth(tenantName) url = globalVars.chameleonObjectStorageURL + "/" + containerName + "/" + path + "/" + fileName my_headers = { "Content-Type": 'binary/octet-stream', "Transfer-Encoding": "chunked", "X-Auth-Token": my_token_id } consumer = threading.Thread(target=send_data, args=( q, url, my_headers, )) consumer.start() #Wait for upload to complete, timeout in while loop allows keyboard interrupt to function while consumer.is_alive(): consumer.join(timeout=1.0) except KeyboardInterrupt: stored_exception = sys.exc_info() quitEvent.set() live555.stopEventLoop() producer.join() q.close() consumer.join() print("Camera " + cameraName + " finished.")
def shutdown(): global capturing # FIXME # Tell Live555's event loop to stop: if capturing: live555.stopEventLoop() capturing = False # Wait for the background thread to finish: t.join() if pidfile: os.unlink(pidfile) sys.exit(0)
def oneFrame(codecName, bytes, sec, usec, durUSec): global frame_i if len(bytes) > 0 and codecName == 'H264': print('frame %d for %s: %d bytes' % (frame_i, codecName, len(bytes))) # H264 start with 0x00 0x00 0x00 0x01 receiveBuf = b'\0\0\0\1' + bytes fOut.write(receiveBuf) decodeFrame(receiveBuf) frame_i = frame_i + 1 if frame_i == nframe: # Tell Live555's event loop to stop live555.stopEventLoop()
# NOTE: the username & password, and the URL path, will vary from one # camera to another! This URL path works with the Lorex LNB2153: url = 'rtsp://192.168.0.1/livePreviewStream@%s/PSIA/Streaming/channels/%s' # (cameraIP, channel) fOut = open(fileOut, 'wb') def oneFrame(codecName, bytes, sec, usec, durUSec): print('frame for %s: %d bytes' % (codecName, len(bytes))) fOut.write(b'\0\0\0\1' + bytes) # Starts pulling frames from the URL, with the provided callback: useTCP = False live555.startRTSP(url, oneFrame, useTCP) # Run Live555's event loop in a background thread: t = threading.Thread(target=live555.runEventLoop, args=()) t.setDaemon(True) t.start() endTime = time.time() + seconds while time.time() < endTime: time.sleep(0.1) # Tell Live555's event loop to stop: live555.stopEventLoop() # Wait for the background thread to finish: t.join()