fix upload video
This commit is contained in:
parent
9d767897b3
commit
207f1aa8e3
2 changed files with 75 additions and 5 deletions
45
app.py
45
app.py
|
|
@ -33,6 +33,47 @@ jobs: Dict[str, Dict] = {}
|
||||||
# Semester to limit concurrency to MAX_CONCURRENT_JOBS
|
# Semester to limit concurrency to MAX_CONCURRENT_JOBS
|
||||||
concurrency_semaphore = asyncio.Semaphore(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/<jobid>_...),
|
||||||
|
move them into output/<job_id>/ 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():
|
async def cleanup_jobs():
|
||||||
"""Background task to remove old jobs and files."""
|
"""Background task to remove old jobs and files."""
|
||||||
import time
|
import time
|
||||||
|
|
@ -208,6 +249,10 @@ async def run_job(job_id, job_data):
|
||||||
|
|
||||||
# Find result JSON
|
# Find result JSON
|
||||||
json_files = glob.glob(os.path.join(output_dir, "*_metadata.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:
|
if json_files:
|
||||||
target_json = json_files[0]
|
target_json = json_files[0]
|
||||||
with open(target_json, 'r') as f:
|
with open(target_json, 'r') as f:
|
||||||
|
|
|
||||||
33
main.py
33
main.py
|
|
@ -892,18 +892,43 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
script_start_time = time.time()
|
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
|
# 1. Get Input Video
|
||||||
if args.url:
|
if args.url:
|
||||||
output_dir = args.output if args.output and os.path.isdir(args.output) else "."
|
# For multi-clip runs, treat --output as an OUTPUT DIRECTORY (create it if needed).
|
||||||
if args.output and not os.path.isdir(args.output) and not args.skip_analysis:
|
# For whole-video runs (--skip-analysis), --output can be a file path.
|
||||||
# If output is a filename but we expect multiple clips, use its dir
|
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 "."
|
output_dir = os.path.dirname(args.output) or "."
|
||||||
|
else:
|
||||||
|
output_dir = "."
|
||||||
|
|
||||||
input_video, video_title = download_youtube_video(args.url, output_dir)
|
input_video, video_title = download_youtube_video(args.url, output_dir)
|
||||||
else:
|
else:
|
||||||
input_video = args.input
|
input_video = args.input
|
||||||
video_title = os.path.splitext(os.path.basename(input_video))[0]
|
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):
|
if not os.path.exists(input_video):
|
||||||
print(f"❌ Input file not found: {input_video}")
|
print(f"❌ Input file not found: {input_video}")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue