from flask import Flask, request, send_file, jsonify
import os
import uuid
from process_video import process_video # 載入 process_video 函式
app = Flask(__name__)
@app.route('/')
def index():
return '''
Video Crop MVP
Upload horizontal sports video
'''
@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)