Spaces:
Paused
Paused
from flask import Flask, render_template, request, send_file | |
import os | |
import tempfile | |
import subprocess | |
import zipfile | |
# Initialize the Flask app | |
app = Flask(__name__, template_folder='.') | |
def home(): | |
return render_template('html.html') # Loads the HTML interface from the same folder | |
def process_video(): | |
video_file = request.files['video'] | |
action = request.form['action'] | |
# Save the uploaded video to a temporary file | |
temp_file = tempfile.NamedTemporaryFile(delete=False) | |
video_file.save(temp_file.name) | |
# Determine the output path based on selected action | |
output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name # Default output format is mp4 | |
# Process video based on action | |
if action == 'Convert Format': | |
# Get the selected format from the form | |
selected_format = request.form['format'] | |
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=f'.{selected_format}').name | |
# Check if "Copy Streams" option is enabled | |
if 'copy_streams' in request.form: | |
# Attempt to copy streams | |
result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-c', 'copy', output_path]) | |
if result.returncode != 0: # If copying fails, re-encode without specifying a codec | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path]) | |
else: | |
# Re-encode without specifying a codec | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, output_path]) | |
elif action == 'Trim Video': | |
start_time = request.form['start_time'] | |
duration = request.form['duration'] | |
if 'copy_streams' in request.form: | |
result = subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, '-c', 'copy', output_path]) | |
if result.returncode != 0: | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path]) | |
else: | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-ss', start_time, '-t', duration, output_path]) | |
elif action == 'Resize Video': | |
width = request.form['width'] | |
height = request.form['height'] | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', f'scale={width}:{height}', output_path]) | |
elif action == 'Extract Audio': | |
output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3').name | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vn', '-acodec', 'mp3', output_path]) | |
elif action == 'Extract Frames': | |
frames_folder = tempfile.mkdtemp() | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-vf', 'fps=1', f'{frames_folder}/frame_%04d.png']) | |
# Create a zip file for the frames | |
output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name | |
with zipfile.ZipFile(output_path, 'w') as zipf: | |
for root, _, files in os.walk(frames_folder): | |
for file in files: | |
zipf.write(os.path.join(root, file), arcname=file) | |
elif action == 'Change Video Speed': | |
speed_factor = request.form['speed_factor'] | |
subprocess.run(['ffmpeg', '-y', '-i', temp_file.name, '-filter:v', f'setpts={1/float(speed_factor)}*PTS', output_path]) | |
# Send the processed file back if successful | |
if os.path.exists(output_path): | |
return send_file(output_path, as_attachment=True) | |
else: | |
return "Error: Processing failed", 500 | |
if __name__ == '__main__': | |
from waitress import serve | |
# Serving the app using waitress | |
serve(app, host='0.0.0.0', port=8080) |