From 207f1aa8e3e4ab0bfdbad4785d473eaf2610893e Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Tue, 23 Dec 2025 09:55:39 +0100 Subject: [PATCH] fix upload video --- app.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.py | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 02b6359..db87219 100644 --- a/app.py +++ b/app.py @@ -33,6 +33,47 @@ jobs: Dict[str, Dict] = {} # Semester to limit concurrency to MAX_CONCURRENT_JOBS concurrency_semaphore = asyncio.Semaphore(MAX_CONCURRENT_JOBS) +def _relocate_root_job_artifacts(job_id: str, job_output_dir: str) -> bool: + """ + Backward-compat rescue: + If main.py accidentally wrote metadata/clips into OUTPUT_DIR root (e.g. output/_...), + move them into output// so the API can find and serve them. + """ + try: + os.makedirs(job_output_dir, exist_ok=True) + root = OUTPUT_DIR + pattern = os.path.join(root, f"{job_id}_*_metadata.json") + meta_candidates = sorted(glob.glob(pattern), key=lambda p: os.path.getmtime(p), reverse=True) + if not meta_candidates: + return False + + # Move the newest metadata and its associated clips. + metadata_path = meta_candidates[0] + base_name = os.path.basename(metadata_path).replace("_metadata.json", "") + + # Move metadata + dest_metadata = os.path.join(job_output_dir, os.path.basename(metadata_path)) + if os.path.abspath(metadata_path) != os.path.abspath(dest_metadata): + shutil.move(metadata_path, dest_metadata) + + # Move any clips that match the same base_name into the job folder + clip_pattern = os.path.join(root, f"{base_name}_clip_*.mp4") + for clip_path in glob.glob(clip_pattern): + dest_clip = os.path.join(job_output_dir, os.path.basename(clip_path)) + if os.path.abspath(clip_path) != os.path.abspath(dest_clip): + shutil.move(clip_path, dest_clip) + + # Also move any temp_ clips that might remain + temp_clip_pattern = os.path.join(root, f"temp_{base_name}_clip_*.mp4") + for clip_path in glob.glob(temp_clip_pattern): + dest_clip = os.path.join(job_output_dir, os.path.basename(clip_path)) + if os.path.abspath(clip_path) != os.path.abspath(dest_clip): + shutil.move(clip_path, dest_clip) + + return True + except Exception: + return False + async def cleanup_jobs(): """Background task to remove old jobs and files.""" import time @@ -208,6 +249,10 @@ async def run_job(job_id, job_data): # Find result JSON json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) + if not json_files: + # Backward-compat rescue if outputs were written to OUTPUT_DIR root + if _relocate_root_job_artifacts(job_id, output_dir): + json_files = glob.glob(os.path.join(output_dir, "*_metadata.json")) if json_files: target_json = json_files[0] with open(target_json, 'r') as f: diff --git a/main.py b/main.py index 9f6d33e..574b013 100644 --- a/main.py +++ b/main.py @@ -892,18 +892,43 @@ if __name__ == '__main__': script_start_time = time.time() + def _ensure_dir(path: str) -> str: + """Create directory if missing and return the same path.""" + if path: + os.makedirs(path, exist_ok=True) + return path + # 1. Get Input Video if args.url: - output_dir = args.output if args.output and os.path.isdir(args.output) else "." - if args.output and not os.path.isdir(args.output) and not args.skip_analysis: - # If output is a filename but we expect multiple clips, use its dir - output_dir = os.path.dirname(args.output) or "." + # For multi-clip runs, treat --output as an OUTPUT DIRECTORY (create it if needed). + # For whole-video runs (--skip-analysis), --output can be a file path. + if args.output and not args.skip_analysis: + output_dir = _ensure_dir(args.output) + else: + # If output is a directory, use it; if it's a filename, use its directory; else default "." + if args.output and os.path.isdir(args.output): + output_dir = args.output + elif args.output and not os.path.isdir(args.output): + output_dir = os.path.dirname(args.output) or "." + else: + output_dir = "." input_video, video_title = download_youtube_video(args.url, output_dir) else: input_video = args.input video_title = os.path.splitext(os.path.basename(input_video))[0] - output_dir = os.path.dirname(args.output) if args.output else os.path.dirname(input_video) + + if args.output and not args.skip_analysis: + # For multi-clip runs, treat --output as an OUTPUT DIRECTORY (create it if needed). + output_dir = _ensure_dir(args.output) + else: + # If output is a directory, use it; if it's a filename, use its directory; else default to input dir. + if args.output and os.path.isdir(args.output): + output_dir = args.output + elif args.output and not os.path.isdir(args.output): + output_dir = os.path.dirname(args.output) or os.path.dirname(input_video) + else: + output_dir = os.path.dirname(input_video) if not os.path.exists(input_video): print(f"❌ Input file not found: {input_video}")