Download backend/server.py from jerymode/XX: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/jerymode/XX/resolve/main/backend/server.py
- Command line
-
hf download hf://spaces/jerymode/XX/backend/server.py
-
curl -L -o server.py https://huggingface.co/spaces/jerymode/XX/resolve/main/backend/server.py
1.98 kB
| from flask import Flask, request, jsonify | |
| import os, subprocess, uuid, json | |
| from werkzeug.utils import secure_filename | |
| app = Flask(__name__) | |
| OUTPUT_DIR = "output" | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| def process_video(): | |
| data = request.get_json() | |
| url = data.get("url") | |
| clips = int(data.get("clips", 1)) | |
| options = data.get("options", {}) | |
| if not url: | |
| return jsonify({"error": "No URL provided"}), 400 | |
| # Generate unique filename | |
| video_id = str(uuid.uuid4()) | |
| input_file = f"{OUTPUT_DIR}/{video_id}.mp4" | |
| # ----------------------------------- | |
| # 1️⃣ تحميل الفيديو | |
| # ----------------------------------- | |
| download_cmd = f'yt-dlp -o "{input_file}" "{url}"' | |
| subprocess.run(download_cmd, shell=True) | |
| # تحقق الفيديو اتحمل | |
| if not os.path.exists(input_file): | |
| return jsonify({"error": "Download failed"}), 500 | |
| # ----------------------------------- | |
| # 2️⃣ تقسيم الفيديو | |
| # ----------------------------------- | |
| duration_cmd = f'ffprobe -i "{input_file}" -show_entries format=duration -v quiet -of csv="p=0"' | |
| duration = float(subprocess.check_output(duration_cmd, shell=True)) | |
| segment = duration / clips | |
| results = [] | |
| for i in range(clips): | |
| start = i * segment | |
| output_clip = f"{OUTPUT_DIR}/{video_id}_clip_{i}.mp4" | |
| # قص المقطع | |
| clip_cmd = f'ffmpeg -y -i "{input_file}" -ss {start} -t {segment} -vf "scale=1080:1920:force_original_aspect_ratio=decrease" "{output_clip}"' | |
| subprocess.run(clip_cmd, shell=True) | |
| results.append({ | |
| "id": i, | |
| "url": f"/output/{video_id}_clip_{i}.mp4", | |
| "duration": round(segment, 2) | |
| }) | |
| return jsonify({"clips": results}) | |
| def home(): | |
| return jsonify({"status": "Backend Running"}) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |