-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
197 lines (166 loc) · 6.78 KB
/
Copy pathapp.py
File metadata and controls
197 lines (166 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import subprocess
import uuid
from datetime import datetime
from flask import Flask, render_template, request, jsonify, send_file, flash, redirect, url_for
from werkzeug.utils import secure_filename
import threading
import time
app = Flask(__name__)
app.secret_key = 'your-secret-key-here'
# Configuration
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'output'
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'wmv', 'flv', 'webm', 'm4v'}
# Create directories if they don't exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Global variable to store optimization progress
optimization_progress = {}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_file_size_mb(file_path):
"""Get file size in MB"""
return round(os.path.getsize(file_path) / (1024 * 1024), 2)
def optimize_video(input_path, output_path, task_id):
"""Optimize video using FFmpeg with lossless compression"""
global optimization_progress
try:
# Get original file size
original_size = get_file_size_mb(input_path)
optimization_progress[task_id] = {
'status': 'processing',
'progress': 0,
'original_size': original_size,
'current_size': original_size,
'message': 'Starting optimization...'
}
# FFmpeg command for lossless optimization
# Using H.264 with high quality settings and efficient encoding
cmd = [
'ffmpeg', '-i', input_path,
'-c:v', 'libx264', # H.264 codec
'-preset', 'veryslow', # Slowest preset for best compression
'-crf', '18', # Constant Rate Factor (18 is visually lossless)
'-c:a', 'aac', # AAC audio codec
'-b:a', '128k', # Audio bitrate
'-movflags', '+faststart', # Optimize for web streaming
'-y', # Overwrite output file
output_path
]
# Run FFmpeg process
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
# Monitor progress
while True:
output = process.stderr.readline()
if output == '' and process.poll() is not None:
break
if output:
# Update progress (simplified - in real app you'd parse FFmpeg output)
if 'time=' in output:
optimization_progress[task_id]['progress'] += 1
if optimization_progress[task_id]['progress'] > 100:
optimization_progress[task_id]['progress'] = 100
# Wait for process to complete
process.wait()
if process.returncode == 0:
# Optimization successful
optimized_size = get_file_size_mb(output_path)
compression_ratio = round((1 - optimized_size / original_size) * 100, 1)
optimization_progress[task_id] = {
'status': 'completed',
'progress': 100,
'original_size': original_size,
'current_size': optimized_size,
'compression_ratio': compression_ratio,
'message': f'Optimization completed! Reduced by {compression_ratio}%',
'output_file': os.path.basename(output_path)
}
else:
# Optimization failed
optimization_progress[task_id] = {
'status': 'error',
'progress': 0,
'message': 'Optimization failed. Please check your video file.'
}
except Exception as e:
optimization_progress[task_id] = {
'status': 'error',
'progress': 0,
'message': f'Error during optimization: {str(e)}'
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'video' not in request.files:
return jsonify({'error': 'No file selected'}), 400
file = request.files['video']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if file and allowed_file(file.filename):
# Generate unique filename
filename = secure_filename(file.filename)
name, ext = os.path.splitext(filename)
unique_filename = f"{name}_{uuid.uuid4().hex[:8]}{ext}"
# Save uploaded file
input_path = os.path.join(UPLOAD_FOLDER, unique_filename)
file.save(input_path)
# Generate output filename
output_filename = f"{name}_optimized{ext}"
output_path = os.path.join(OUTPUT_FOLDER, output_filename)
# Generate task ID
task_id = str(uuid.uuid4())
# Start optimization in background thread
thread = threading.Thread(
target=optimize_video,
args=(input_path, output_path, task_id)
)
thread.daemon = True
thread.start()
return jsonify({
'task_id': task_id,
'message': 'Upload successful! Optimization started...'
})
return jsonify({'error': 'Invalid file type'}), 400
@app.route('/progress/<task_id>')
def get_progress(task_id):
if task_id in optimization_progress:
return jsonify(optimization_progress[task_id])
return jsonify({'error': 'Task not found'}), 404
@app.route('/download/<filename>')
def download_file(filename):
try:
return send_file(
os.path.join(OUTPUT_FOLDER, filename),
as_attachment=True,
download_name=filename
)
except FileNotFoundError:
return jsonify({'error': 'File not found'}), 404
@app.route('/cleanup', methods=['POST'])
def cleanup_files():
"""Clean up old files"""
try:
# Clean uploads older than 1 hour
current_time = time.time()
for filename in os.listdir(UPLOAD_FOLDER):
file_path = os.path.join(UPLOAD_FOLDER, filename)
if os.path.getctime(file_path) < current_time - 3600: # 1 hour
os.remove(file_path)
# Clean outputs older than 24 hours
for filename in os.listdir(OUTPUT_FOLDER):
file_path = os.path.join(OUTPUT_FOLDER, filename)
if os.path.getctime(file_path) < current_time - 86400: # 24 hours
os.remove(file_path)
return jsonify({'message': 'Cleanup completed'})
except Exception as e:
return jsonify({'error': f'Cleanup failed: {str(e)}'}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)