import os import subprocess import re import time import eventlet # Eventlet monkey patch for SocketIO eventlet.monkey_patch() from flask import Flask, render_template, request, send_file, jsonify from flask_socketio import SocketIO app = Flask(__name__) app.config['SECRET_KEY'] = 'mls_compress_v2_secret' socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet') UPLOAD_FOLDER = 'uploads_compress' PROCESSED_FOLDER = 'processed_compress' os.makedirs(UPLOAD_FOLDER, exist_ok=True) os.makedirs(PROCESSED_FOLDER, exist_ok=True) def get_video_duration(file_path): try: cmd = ["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", file_path] duration = subprocess.check_output(cmd).decode().strip() return float(duration) except: return 1.0 @app.route('/') def index(): return render_template('index.html') @app.route('/compress', methods=['POST']) def compress_video(): if 'video' not in request.files: return jsonify({"status": "error", "message": "No video selected"}) file = request.files['video'] if file.filename == '': return jsonify({"status": "error", "message": "No file name"}) input_path = os.path.join(UPLOAD_FOLDER, file.filename) output_filename = "MLS_FIXED_" + os.path.splitext(file.filename)[0] + ".mp4" output_path = os.path.join(PROCESSED_FOLDER, output_filename) # File save ho rahi hai (Upload phase yahan khatam hota hai) file.save(input_path) # Compression shuru hone ka signal bhejein socketio.emit('status_update', {'msg': 'COMPRESSING VIDEO...'}) total_duration = get_video_duration(input_path) command = [ 'ffmpeg', '-y', '-i', input_path, '-vcodec', 'libx265', '-crf', '28', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-tag:v', 'hvc1', '-acodec', 'aac', '-b:a', '128k', output_path ] process = subprocess.Popen(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, universal_newlines=True) for line in process.stdout: time_match = re.search(r"time=(\d+):(\d+):(\d+.\d+)", line) if time_match: hours, mins, secs = map(float, time_match.groups()) current_time = hours * 3600 + mins * 60 + secs progress = min(int((current_time / total_duration) * 100), 99) socketio.emit('progress', {'percentage': progress}) eventlet.sleep(0) process.wait() if os.path.exists(input_path): os.remove(input_path) socketio.emit('progress', {'percentage': 100}) return jsonify({"status": "done", "file": output_filename}) @app.route('/download/') def download(filename): return send_file(os.path.join(PROCESSED_FOLDER, filename), as_attachment=True) if __name__ == '__main__': socketio.run(app, host='0.0.0.0', port=7860)