From 39ab8c23d2d1f70c452d642b92fad10e6a5bd8f0 Mon Sep 17 00:00:00 2001 From: deflax Date: Fri, 12 Jan 2024 22:40:37 +0200 Subject: [PATCH] check if stream_hls_url exists before we exec the recording --- src/scheduler/app.py | 25 ++++++++++++++++--------- src/scheduler/requirements.txt | 3 ++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/scheduler/app.py b/src/scheduler/app.py index 34e0d16..56b01cf 100644 --- a/src/scheduler/app.py +++ b/src/scheduler/app.py @@ -1,7 +1,8 @@ import os +import time import logging import json -import time +import requests from datetime import datetime from flask import Flask, render_template, jsonify, request from apscheduler.schedulers.background import BackgroundScheduler @@ -154,17 +155,23 @@ def exec_recorder(stream_id, stream_hls_url): output_file = current_datetime + ".mp4" if rechead != "": logger_job.error('Recorder is already started. Refusing to start another rec job.') - return False else: + # Check if the stream_hls_url returns 200 + req_counter = 0 + while True: + req_counter += 1 + if requests.get(stream_hls_url).status_code == 200: + logger_job.warning(f'{stream_hls_url} accessible after {req_counter} attempts.') + break logger_job.warning(f'Starting recording job for {output_file}') rechead = stream_id - ffmpeg = ( - FFmpeg() - .option("y") - .input(stream_hls_url) - .output(output_file, vcodec="copy") - ) - ffmpeg.execute() + ffmpeg = ( + FFmpeg() + .option("y") + .input(stream_hls_url) + .output(output_file, vcodec="copy") + ) + ffmpeg.execute() def core_api_sync(): global database diff --git a/src/scheduler/requirements.txt b/src/scheduler/requirements.txt index 9fd960c..3af9109 100644 --- a/src/scheduler/requirements.txt +++ b/src/scheduler/requirements.txt @@ -7,4 +7,5 @@ MarkupSafe==2.1.3 waitress==2.1.2 Werkzeug==3.0.1 APScheduler==3.10.4 -python-ffmpeg==2.0.10 \ No newline at end of file +python-ffmpeg==2.0.10 +requests==2.31.0 \ No newline at end of file