|
from flask import Flask, request, send_file, jsonify |
|
import os |
|
import uuid |
|
from process_video import process_video |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def index(): |
|
return ''' |
|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Video Crop MVP</title> |
|
</head> |
|
<body> |
|
<h1>Upload horizontal sports video</h1> |
|
<form action="/process_video" method="post" enctype="multipart/form-data"> |
|
<input type="file" name="video" accept="video/*"><br><br> |
|
<input type="submit" value="Upload and Process"> |
|
</form> |
|
</body> |
|
</html> |
|
''' |
|
|
|
@app.route('/process_video', methods=['POST']) |
|
def handle_video(): |
|
if 'video' not in request.files: |
|
return "No video file uploaded", 400 |
|
|
|
video_file = request.files['video'] |
|
if video_file.filename == '': |
|
return "No selected file", 400 |
|
|
|
|
|
input_path = f"input_{uuid.uuid4().hex}.mp4" |
|
video_file.save(input_path) |
|
|
|
|
|
output_dir = f"output_{uuid.uuid4().hex}" |
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
try: |
|
process_video(input_path, output_dir) |
|
except Exception as e: |
|
return f"Error processing video: {str(e)}", 500 |
|
finally: |
|
|
|
os.remove(input_path) |
|
|
|
|
|
keyframes_json_path = os.path.join(output_dir, "keyframes.json") |
|
if os.path.exists(keyframes_json_path): |
|
with open(keyframes_json_path) as f: |
|
keyframes_data = json.load(f) |
|
return jsonify(keyframes_data) |
|
else: |
|
return "Keyframes not generated.", 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |