Fix Random Crashes

This commit is contained in:
TheErrorExe 2025-04-20 18:08:47 +02:00 committed by GitHub
parent 8ccdd40095
commit 5910ee6a6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -286,13 +286,15 @@ async def watch():
async def process_video(video_id):
video_mp4_path = os.path.join(VIDEO_FOLDER, f"{video_id}.mp4")
video_flv_path = os.path.join(VIDEO_FOLDER, f"{video_id}.flv")
try:
video_status[video_id] = {"status": "downloading"}
with tempfile.TemporaryDirectory() as temp_dir:
temp_video_path = os.path.join(temp_dir, f"{video_id}.%(ext)s")
temp_dir = tempfile.mkdtemp()
try:
subprocess.run([
"yt-dlp",
"-o", temp_video_path,
"-o", os.path.join(temp_dir, f"{video_id}.%(ext)s"),
"--cookies", "cookies.txt",
"--proxy", "http://localhost:4000",
"-f", "worstvideo+worstaudio",
@ -301,12 +303,13 @@ async def process_video(video_id):
downloaded_files = [f for f in os.listdir(temp_dir) if video_id in f]
if not downloaded_files:
video_status[video_id] = {"status": "error", "message": "Error downloading."}
return
raise Exception("No video file downloaded")
downloaded_file = os.path.join(temp_dir, downloaded_files[0])
if not downloaded_file.endswith(".mp4"):
video_status[video_id] = {"status": "converting"}
try:
subprocess.run([
"ffmpeg",
"-y",
@ -320,12 +323,18 @@ async def process_video(video_id):
"-movflags", "+faststart",
"-vf", "scale=854:480",
video_mp4_path
], check=True)
], check=True, timeout=300, stderr=subprocess.PIPE)
except subprocess.TimeoutExpired:
raise Exception("MP4 conversion timed out")
except subprocess.CalledProcessError as e:
error_output = e.stderr.decode('utf-8') if e.stderr else str(e)
raise Exception(f"MP4 conversion failed: {error_output}")
else:
shutil.copy(downloaded_file, video_mp4_path)
if not os.path.exists(video_flv_path):
video_status[video_id] = {"status": "converting for Wii"}
try:
subprocess.run([
"ffmpeg",
"-y",
@ -338,11 +347,30 @@ async def process_video(video_id):
"-crf", "51",
"-filter:v", "fps=fps=15",
video_flv_path
], check=True)
], check=True, timeout=300, stderr=subprocess.PIPE)
except subprocess.TimeoutExpired:
raise Exception("FLV conversion timed out")
except subprocess.CalledProcessError as e:
error_output = e.stderr.decode('utf-8') if e.stderr else str(e)
raise Exception(f"FLV conversion failed: {error_output}")
video_status[video_id] = {"status": "complete", "url": f"/sigma/videos/{video_id}.mp4"}
finally:
try:
shutil.rmtree(temp_dir)
except:
pass
except Exception as e:
video_status[video_id] = {"status": "error", "message": str(e)}
error_msg = str(e)
video_status[video_id] = {"status": "error", "message": error_msg}
for path in [video_mp4_path, video_flv_path]:
try:
if os.path.exists(path):
os.remove(path)
except:
pass
@app.route("/status/<video_id>")
async def check_status(video_id):