Spaces:
Running
Running
Force update backend\app.py - 1757355548
Browse files- backend/app.py +159 -159
backend/app.py
CHANGED
|
@@ -1,159 +1,159 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, send_file
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import os
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import moviepy.editor as mp
|
| 8 |
-
from werkzeug.utils import secure_filename
|
| 9 |
-
import tempfile
|
| 10 |
-
import uuid
|
| 11 |
-
import json
|
| 12 |
-
from video_processor import VideoProcessor
|
| 13 |
-
|
| 14 |
-
def process_video(video_path, progress_callback=None):
|
| 15 |
-
"""
|
| 16 |
-
Process a video file and return the result.
|
| 17 |
-
This function is used by both Flask API and Gradio interface.
|
| 18 |
-
"""
|
| 19 |
-
try:
|
| 20 |
-
# Initialize video processor
|
| 21 |
-
processor = VideoProcessor()
|
| 22 |
-
|
| 23 |
-
# Generate output filename
|
| 24 |
-
input_filename = os.path.basename(video_path)
|
| 25 |
-
output_filename = f"vr180_{input_filename}"
|
| 26 |
-
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
|
| 27 |
-
|
| 28 |
-
# Process video
|
| 29 |
-
if progress_callback:
|
| 30 |
-
progress_callback(0.2, desc="Initializing video processing...")
|
| 31 |
-
|
| 32 |
-
result = processor.process_video(video_path, output_path, progress_callback)
|
| 33 |
-
|
| 34 |
-
if result['success']:
|
| 35 |
-
if progress_callback:
|
| 36 |
-
progress_callback(1.0, desc="Processing complete!")
|
| 37 |
-
return {
|
| 38 |
-
'success': True,
|
| 39 |
-
'output_path': output_path,
|
| 40 |
-
'output_filename': output_filename,
|
| 41 |
-
'message': 'Video processed successfully',
|
| 42 |
-
'processing_time': result.get('processing_time', 0)
|
| 43 |
-
}
|
| 44 |
-
else:
|
| 45 |
-
return {
|
| 46 |
-
'success': False,
|
| 47 |
-
'error': result.get('error', 'Processing failed')
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
except Exception as e:
|
| 51 |
-
return {
|
| 52 |
-
'success': False,
|
| 53 |
-
'error': f'Error processing video: {str(e)}'
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
app = Flask(__name__)
|
| 57 |
-
CORS(app)
|
| 58 |
-
|
| 59 |
-
# Configuration
|
| 60 |
-
UPLOAD_FOLDER = 'uploads'
|
| 61 |
-
OUTPUT_FOLDER = 'outputs'
|
| 62 |
-
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'webm'}
|
| 63 |
-
|
| 64 |
-
# Create directories
|
| 65 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 66 |
-
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 67 |
-
|
| 68 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 69 |
-
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
| 70 |
-
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500MB max file size
|
| 71 |
-
|
| 72 |
-
def allowed_file(filename):
|
| 73 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 74 |
-
|
| 75 |
-
@app.route('/api/health', methods=['GET'])
|
| 76 |
-
def health_check():
|
| 77 |
-
return jsonify({'status': 'healthy', 'message': 'VR180 Converter API is running'})
|
| 78 |
-
|
| 79 |
-
@app.route('/api/upload', methods=['POST'])
|
| 80 |
-
def upload_video():
|
| 81 |
-
if 'video' not in request.files:
|
| 82 |
-
return jsonify({'error': 'No video file provided'}), 400
|
| 83 |
-
|
| 84 |
-
file = request.files['video']
|
| 85 |
-
if file.filename == '':
|
| 86 |
-
return jsonify({'error': 'No file selected'}), 400
|
| 87 |
-
|
| 88 |
-
if not allowed_file(file.filename):
|
| 89 |
-
return jsonify({'error': 'Invalid file type. Please upload MP4, AVI, MOV, MKV, or WebM'}), 400
|
| 90 |
-
|
| 91 |
-
# Generate unique filename
|
| 92 |
-
filename = secure_filename(file.filename)
|
| 93 |
-
unique_filename = f"{uuid.uuid4()}_{filename}"
|
| 94 |
-
filepath = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
| 95 |
-
|
| 96 |
-
try:
|
| 97 |
-
file.save(filepath)
|
| 98 |
-
|
| 99 |
-
# Get video info
|
| 100 |
-
video = mp.VideoFileClip(filepath)
|
| 101 |
-
duration = video.duration
|
| 102 |
-
fps = video.fps
|
| 103 |
-
size = video.size
|
| 104 |
-
|
| 105 |
-
video.close()
|
| 106 |
-
|
| 107 |
-
return jsonify({
|
| 108 |
-
'success': True,
|
| 109 |
-
'filename': unique_filename,
|
| 110 |
-
'original_name': filename,
|
| 111 |
-
'duration': duration,
|
| 112 |
-
'fps': fps,
|
| 113 |
-
'size': size
|
| 114 |
-
})
|
| 115 |
-
|
| 116 |
-
except Exception as e:
|
| 117 |
-
return jsonify({'error': f'Error processing file: {str(e)}'}), 500
|
| 118 |
-
|
| 119 |
-
@app.route('/api/process', methods=['POST'])
|
| 120 |
-
def process_video_api():
|
| 121 |
-
data = request.get_json()
|
| 122 |
-
filename = data.get('filename')
|
| 123 |
-
|
| 124 |
-
if not filename:
|
| 125 |
-
return jsonify({'error': 'No filename provided'}), 400
|
| 126 |
-
|
| 127 |
-
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 128 |
-
|
| 129 |
-
if not os.path.exists(filepath):
|
| 130 |
-
return jsonify({'error': 'File not found'}), 404
|
| 131 |
-
|
| 132 |
-
# Use the shared process_video function
|
| 133 |
-
result = process_video(filepath)
|
| 134 |
-
|
| 135 |
-
if result['success']:
|
| 136 |
-
return jsonify(result)
|
| 137 |
-
else:
|
| 138 |
-
return jsonify({'error': result.get('error', 'Processing failed')}), 500
|
| 139 |
-
|
| 140 |
-
@app.route('/api/download/<filename>')
|
| 141 |
-
def download_video(filename):
|
| 142 |
-
filepath = os.path.join(app.config['OUTPUT_FOLDER'], filename)
|
| 143 |
-
|
| 144 |
-
if not os.path.exists(filepath):
|
| 145 |
-
return jsonify({'error': 'File not found'}), 404
|
| 146 |
-
|
| 147 |
-
return send_file(filepath, as_attachment=True)
|
| 148 |
-
|
| 149 |
-
@app.route('/api/status/<filename>')
|
| 150 |
-
def get_processing_status(filename):
|
| 151 |
-
# This would be implemented with a proper job queue in production
|
| 152 |
-
# For now, we'll return a simple status
|
| 153 |
-
return jsonify({
|
| 154 |
-
'status': 'completed',
|
| 155 |
-
'progress': 100
|
| 156 |
-
})
|
| 157 |
-
|
| 158 |
-
if __name__ == '__main__':
|
| 159 |
-
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import moviepy.editor as mp
|
| 8 |
+
from werkzeug.utils import secure_filename
|
| 9 |
+
import tempfile
|
| 10 |
+
import uuid
|
| 11 |
+
import json
|
| 12 |
+
from .video_processor import VideoProcessor # Fixed relative import
|
| 13 |
+
|
| 14 |
+
def process_video(video_path, progress_callback=None):
|
| 15 |
+
"""
|
| 16 |
+
Process a video file and return the result.
|
| 17 |
+
This function is used by both Flask API and Gradio interface.
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
# Initialize video processor
|
| 21 |
+
processor = VideoProcessor()
|
| 22 |
+
|
| 23 |
+
# Generate output filename
|
| 24 |
+
input_filename = os.path.basename(video_path)
|
| 25 |
+
output_filename = f"vr180_{input_filename}"
|
| 26 |
+
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
|
| 27 |
+
|
| 28 |
+
# Process video
|
| 29 |
+
if progress_callback:
|
| 30 |
+
progress_callback(0.2, desc="Initializing video processing...")
|
| 31 |
+
|
| 32 |
+
result = processor.process_video(video_path, output_path, progress_callback)
|
| 33 |
+
|
| 34 |
+
if result['success']:
|
| 35 |
+
if progress_callback:
|
| 36 |
+
progress_callback(1.0, desc="Processing complete!")
|
| 37 |
+
return {
|
| 38 |
+
'success': True,
|
| 39 |
+
'output_path': output_path,
|
| 40 |
+
'output_filename': output_filename,
|
| 41 |
+
'message': 'Video processed successfully',
|
| 42 |
+
'processing_time': result.get('processing_time', 0)
|
| 43 |
+
}
|
| 44 |
+
else:
|
| 45 |
+
return {
|
| 46 |
+
'success': False,
|
| 47 |
+
'error': result.get('error', 'Processing failed')
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return {
|
| 52 |
+
'success': False,
|
| 53 |
+
'error': f'Error processing video: {str(e)}'
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
app = Flask(__name__)
|
| 57 |
+
CORS(app)
|
| 58 |
+
|
| 59 |
+
# Configuration
|
| 60 |
+
UPLOAD_FOLDER = 'uploads'
|
| 61 |
+
OUTPUT_FOLDER = 'outputs'
|
| 62 |
+
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'webm'}
|
| 63 |
+
|
| 64 |
+
# Create directories
|
| 65 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 66 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 67 |
+
|
| 68 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 69 |
+
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
| 70 |
+
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500MB max file size
|
| 71 |
+
|
| 72 |
+
def allowed_file(filename):
|
| 73 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 74 |
+
|
| 75 |
+
@app.route('/api/health', methods=['GET'])
|
| 76 |
+
def health_check():
|
| 77 |
+
return jsonify({'status': 'healthy', 'message': 'VR180 Converter API is running'})
|
| 78 |
+
|
| 79 |
+
@app.route('/api/upload', methods=['POST'])
|
| 80 |
+
def upload_video():
|
| 81 |
+
if 'video' not in request.files:
|
| 82 |
+
return jsonify({'error': 'No video file provided'}), 400
|
| 83 |
+
|
| 84 |
+
file = request.files['video']
|
| 85 |
+
if file.filename == '':
|
| 86 |
+
return jsonify({'error': 'No file selected'}), 400
|
| 87 |
+
|
| 88 |
+
if not allowed_file(file.filename):
|
| 89 |
+
return jsonify({'error': 'Invalid file type. Please upload MP4, AVI, MOV, MKV, or WebM'}), 400
|
| 90 |
+
|
| 91 |
+
# Generate unique filename
|
| 92 |
+
filename = secure_filename(file.filename)
|
| 93 |
+
unique_filename = f"{uuid.uuid4()}_{filename}"
|
| 94 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
file.save(filepath)
|
| 98 |
+
|
| 99 |
+
# Get video info
|
| 100 |
+
video = mp.VideoFileClip(filepath)
|
| 101 |
+
duration = video.duration
|
| 102 |
+
fps = video.fps
|
| 103 |
+
size = video.size
|
| 104 |
+
|
| 105 |
+
video.close()
|
| 106 |
+
|
| 107 |
+
return jsonify({
|
| 108 |
+
'success': True,
|
| 109 |
+
'filename': unique_filename,
|
| 110 |
+
'original_name': filename,
|
| 111 |
+
'duration': duration,
|
| 112 |
+
'fps': fps,
|
| 113 |
+
'size': size
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
return jsonify({'error': f'Error processing file: {str(e)}'}), 500
|
| 118 |
+
|
| 119 |
+
@app.route('/api/process', methods=['POST'])
|
| 120 |
+
def process_video_api():
|
| 121 |
+
data = request.get_json()
|
| 122 |
+
filename = data.get('filename')
|
| 123 |
+
|
| 124 |
+
if not filename:
|
| 125 |
+
return jsonify({'error': 'No filename provided'}), 400
|
| 126 |
+
|
| 127 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 128 |
+
|
| 129 |
+
if not os.path.exists(filepath):
|
| 130 |
+
return jsonify({'error': 'File not found'}), 404
|
| 131 |
+
|
| 132 |
+
# Use the shared process_video function
|
| 133 |
+
result = process_video(filepath)
|
| 134 |
+
|
| 135 |
+
if result['success']:
|
| 136 |
+
return jsonify(result)
|
| 137 |
+
else:
|
| 138 |
+
return jsonify({'error': result.get('error', 'Processing failed')}), 500
|
| 139 |
+
|
| 140 |
+
@app.route('/api/download/<filename>')
|
| 141 |
+
def download_video(filename):
|
| 142 |
+
filepath = os.path.join(app.config['OUTPUT_FOLDER'], filename)
|
| 143 |
+
|
| 144 |
+
if not os.path.exists(filepath):
|
| 145 |
+
return jsonify({'error': 'File not found'}), 404
|
| 146 |
+
|
| 147 |
+
return send_file(filepath, as_attachment=True)
|
| 148 |
+
|
| 149 |
+
@app.route('/api/status/<filename>')
|
| 150 |
+
def get_processing_status(filename):
|
| 151 |
+
# This would be implemented with a proper job queue in production
|
| 152 |
+
# For now, we'll return a simple status
|
| 153 |
+
return jsonify({
|
| 154 |
+
'status': 'completed',
|
| 155 |
+
'progress': 100
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
+
if __name__ == '__main__':
|
| 159 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|