Initial commit

This commit is contained in:
2024-05-01 12:28:41 -04:00
commit 4502501501
3 changed files with 101 additions and 0 deletions

60
app.py Normal file
View File

@@ -0,0 +1,60 @@
from flask import Flask, request, render_template, Response
import subprocess
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'uploaded_videos'
OUTPUT_FOLDER = 'compressed_videos'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
def generate_file(output_filepath):
with open(output_filepath, 'rb') as f:
while True:
chunk = f.read(4096)
if not chunk:
break
yield chunk
def cleanup(filepath, output_filepath):
os.remove(filepath)
os.remove(output_filepath)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
file = request.files.get('file')
if file and file.filename:
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)
output_filepath = os.path.join(app.config['OUTPUT_FOLDER'], file.filename)
try:
subprocess.run(['ffmpeg', '-i', filepath, '-c:v', 'hevc_nvenc', output_filepath], check=True)
response = Response(generate_file(output_filepath), mimetype='video/mkv')
response.headers.set('Content-Disposition', 'attachment', filename=file.filename)
@response.call_on_close
def on_close():
cleanup(filepath, output_filepath)
return response
except subprocess.CalledProcessError as e:
cleanup(filepath, output_filepath)
return "Error processing video", 500
except Exception as e:
cleanup(filepath, output_filepath)
return "Unhandled Error", 500
else:
return "No file or empty filename", 400
else:
# Handle GET request by showing an upload form
return render_template('index.html') # Ensure you have an index.html template with the upload form.
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5123)