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)

29
dockerfile Normal file
View File

@@ -0,0 +1,29 @@
# Using Debian 12 as the base image
FROM debian:12
# Setting the working directory inside the container
WORKDIR /videocompressor
# Setting the timezone environment variable
ENV TZ=America/New_York
ENV NVIDIA_DRIVER_CAPABILITIES=all
# Updating and upgrading system packages, and installing dependencies
RUN apt update -y && \
apt full-upgrade -y && \
apt install -y python3 python3-pip python3-venv ffmpeg && \
apt clean && \
python3 -m venv venv
# Copying application source code to the container
COPY . .
# Installing Python dependencies inside the virtual environment
RUN . venv/bin/activate && \
pip3 install flask
# Exposing the port the app runs on
EXPOSE 5123
# Command to run the application using the virtual environment
CMD ["/bin/bash", "-c", "source venv/bin/activate && python3 app.py"]

12
templates/index.html Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Video Compressor</title>
</head>
<body>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file" required>
<input type="submit" value="Upload and Compress">
</form>
</body>
</html>