import asyncio import signal import os from .consts import RTSP_URL, VIDEO_OUT_FULL, VIDEO_OUT_ANAL async def ffmpeg_start_recording(file): ffmpeg_args = [ "-rtsp_transport", "tcp", "-timeout", "5000000", "-i", RTSP_URL, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-preset", "fast", "-b:v", "1M", "-f", "mp4", "-an", file ] process = await asyncio.create_subprocess_exec('ffmpeg', *ffmpeg_args, stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.DEVNULL ) return process async def ffmpeg_stop_recording(process): # logger.debug("Inside ffmpeg_stop_recording") if process: # logger.debug(f"Sending SIGQUIT to recording_process: {process}") process.send_signal(signal.SIGQUIT) try: await asyncio.wait_for(process.wait(), timeout=2) except Exception as e: # logger.debug(f"Got an exception while waiting for the process to finish: {e}") process.kill() await process.wait() async def ffmpeg_create_anal_file(file, anal_file): print("Inside ffmpeg_create_anal_file") print(f"VIDEO_OUT_FULL: {VIDEO_OUT_FULL}, VIDEO_OUT_ANAL: {VIDEO_OUT_ANAL}, file:{file}, anal_file: {anal_file} ") ffmpeg_args = [ "-y", "-i", os.path.join(VIDEO_OUT_FULL, file), "-vf", "scale=456:256,setsar=1", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-an", "-movflags", "faststart", os.path.join(VIDEO_OUT_ANAL, anal_file) ] # logger.debug(f"writing to: {os.path.join(VIDEO_OUT_ANAL, anal_file)}") # logger.debug(f"calling ffmpeg with {' '.join(ffmpeg_args)}") process = await asyncio.create_subprocess_exec('ffmpeg', *ffmpeg_args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) # monitor_task = asyncio.create_task(monitor_process(process)) # monitor_task.add_done_callback( # lambda task: asyncio.create_task(cb_completed(row_id, anal_file)) # ) stdout, stderr = await process.communicate() return stdout, stderr # logger.debug(f"Process: {process} ended with return code: {process.returncode}") # if stderr: # logger.debug("stderr:") # logger.debug(stderr.decode()) # if stdout: # logger.debug("stdout:") # logger.debug(stdout.decode())