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,63 +286,91 @@ async def watch():
async def process_video(video_id): async def process_video(video_id):
video_mp4_path = os.path.join(VIDEO_FOLDER, f"{video_id}.mp4") video_mp4_path = os.path.join(VIDEO_FOLDER, f"{video_id}.mp4")
video_flv_path = os.path.join(VIDEO_FOLDER, f"{video_id}.flv") video_flv_path = os.path.join(VIDEO_FOLDER, f"{video_id}.flv")
try: try:
video_status[video_id] = {"status": "downloading"} video_status[video_id] = {"status": "downloading"}
with tempfile.TemporaryDirectory() as temp_dir: temp_dir = tempfile.mkdtemp()
temp_video_path = os.path.join(temp_dir, f"{video_id}.%(ext)s")
try:
subprocess.run([ subprocess.run([
"yt-dlp", "yt-dlp",
"-o", temp_video_path, "-o", os.path.join(temp_dir, f"{video_id}.%(ext)s"),
"--cookies", "cookies.txt", "--cookies", "cookies.txt",
"--proxy", "http://localhost:4000", "--proxy", "http://localhost:4000",
"-f", "worstvideo+worstaudio", "-f", "worstvideo+worstaudio",
f"https://youtube.com/watch?v={video_id}" f"https://youtube.com/watch?v={video_id}"
], check=True) ], check=True)
downloaded_files = [f for f in os.listdir(temp_dir) if video_id in f] downloaded_files = [f for f in os.listdir(temp_dir) if video_id in f]
if not downloaded_files: if not downloaded_files:
video_status[video_id] = {"status": "error", "message": "Error downloading."} raise Exception("No video file downloaded")
return
downloaded_file = os.path.join(temp_dir, downloaded_files[0]) downloaded_file = os.path.join(temp_dir, downloaded_files[0])
if not downloaded_file.endswith(".mp4"): if not downloaded_file.endswith(".mp4"):
video_status[video_id] = {"status": "converting"} video_status[video_id] = {"status": "converting"}
subprocess.run([ try:
"ffmpeg", subprocess.run([
"-y", "ffmpeg",
"-i", downloaded_file, "-y",
"-c:v", "libx264", "-i", downloaded_file,
"-crf", "51", "-c:v", "libx264",
"-c:a", "aac", "-crf", "51",
"-strict", "experimental", "-c:a", "aac",
"-preset", "ultrafast", "-strict", "experimental",
"-b:a", "64k", "-preset", "ultrafast",
"-movflags", "+faststart", "-b:a", "64k",
"-vf", "scale=854:480", "-movflags", "+faststart",
video_mp4_path "-vf", "scale=854:480",
], check=True) video_mp4_path
], 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: else:
shutil.copy(downloaded_file, video_mp4_path) shutil.copy(downloaded_file, video_mp4_path)
if not os.path.exists(video_flv_path): if not os.path.exists(video_flv_path):
video_status[video_id] = {"status": "converting for Wii"} video_status[video_id] = {"status": "converting for Wii"}
subprocess.run([ try:
"ffmpeg", subprocess.run([
"-y", "ffmpeg",
"-i", video_mp4_path, "-y",
"-ar", "22050", "-i", video_mp4_path,
"-f", "flv", "-ar", "22050",
"-s", "320x240", "-f", "flv",
"-ab", "32k", "-s", "320x240",
"-preset", "ultrafast", "-ab", "32k",
"-crf", "51", "-preset", "ultrafast",
"-filter:v", "fps=fps=15", "-crf", "51",
video_flv_path "-filter:v", "fps=fps=15",
], check=True) video_flv_path
], 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"} 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: 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>") @app.route("/status/<video_id>")
async def check_status(video_id): async def check_status(video_id):