Fix Loading Screen Bug

This commit is contained in:
TheErrorExe 2025-06-18 21:04:30 +02:00 committed by GitHub
parent cd95e8b57e
commit 56cbb13f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,6 +71,23 @@ os.makedirs(VIDEO_FOLDER, exist_ok=True)
MAX_VIDEO_SIZE = 1 * 1024 * 1024 * 1024 MAX_VIDEO_SIZE = 1 * 1024 * 1024 * 1024
MAX_FOLDER_SIZE = 5 * 1024 * 1024 * 1024 MAX_FOLDER_SIZE = 5 * 1024 * 1024 * 1024
async def run_subprocess(cmd, timeout=300):
loop = asyncio.get_event_loop()
def _run():
try:
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=timeout,
check=True
)
return result
except Exception as e:
return e
return await loop.run_in_executor(None, _run)
def get_folder_size(path): def get_folder_size(path):
total_size = 0 total_size = 0
for dirpath, dirnames, filenames in os.walk(path): for dirpath, dirnames, filenames in os.walk(path):
@ -217,7 +234,7 @@ async def watch():
comments = await get_video_comments(video_id) comments = await get_video_comments(video_id)
channel_logo_url = "" channel_logo_url = ""
subscriber_count = "Unbekannt" subscriber_count = "Unknown"
try: try:
channel_id = metadata['channelId'] channel_id = metadata['channelId']
@ -286,81 +303,67 @@ 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")
temp_dir = tempfile.mkdtemp()
try: try:
video_status[video_id] = {"status": "downloading"} video_status[video_id] = {"status": "downloading"}
temp_dir = tempfile.mkdtemp()
try: ytdlp_cmd = [
subprocess.run([ "yt-dlp",
"yt-dlp", "-o", os.path.join(temp_dir, f"{video_id}.%(ext)s"),
"-o", os.path.join(temp_dir, f"{video_id}.%(ext)s"), "--cookies", "cookies.txt",
"--cookies", "cookies.txt", "-f", "worstvideo+worstaudio",
"--proxy", "http://localhost:4000", f"https://youtube.com/watch?v={video_id}"
"-f", "worstvideo+worstaudio", ]
f"https://youtube.com/watch?v={video_id}" result = await run_subprocess(ytdlp_cmd)
], check=True) if isinstance(result, Exception):
raise result
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:
raise Exception("No video file downloaded") raise Exception("No video file downloaded")
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"}
try: ffmpeg_cmd = [
subprocess.run([ "ffmpeg", "-y",
"ffmpeg", "-i", downloaded_file,
"-y", "-c:v", "libx264",
"-i", downloaded_file, "-crf", "51",
"-c:v", "libx264", "-c:a", "aac",
"-crf", "51", "-preset", "ultrafast",
"-c:a", "aac", "-b:a", "64k",
"-strict", "experimental", "-movflags", "+faststart",
"-preset", "ultrafast", "-vf", "scale=854:480",
"-b:a", "64k", video_mp4_path
"-movflags", "+faststart", ]
"-vf", "scale=854:480", result = await run_subprocess(ffmpeg_cmd)
video_mp4_path if isinstance(result, Exception):
], check=True, timeout=300, stderr=subprocess.PIPE) raise result
except subprocess.TimeoutExpired: else:
raise Exception("MP4 conversion timed out") shutil.copy(downloaded_file, video_mp4_path)
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): if not os.path.exists(video_flv_path):
video_status[video_id] = {"status": "converting for Wii"} video_status[video_id] = {"status": "converting for Wii"}
try: ffmpeg_flv_cmd = [
subprocess.run([ "ffmpeg", "-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", ]
video_flv_path result = await run_subprocess(ffmpeg_flv_cmd)
], check=True, timeout=300, stderr=subprocess.PIPE) if isinstance(result, Exception):
except subprocess.TimeoutExpired: raise result
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:
error_msg = str(e) error_msg = str(e)
@ -372,6 +375,12 @@ async def process_video(video_id):
except: except:
pass pass
finally:
try:
shutil.rmtree(temp_dir)
except:
pass
@app.route("/status/<video_id>") @app.route("/status/<video_id>")
async def check_status(video_id): async def check_status(video_id):
return jsonify(video_status.get(video_id, {"status": "pending"})) return jsonify(video_status.get(video_id, {"status": "pending"}))