From 4502501501f72afa89ee6daeb0377ee1ef3ff9f4 Mon Sep 17 00:00:00 2001 From: Leonard Excoffier Date: Wed, 1 May 2024 12:28:41 -0400 Subject: [PATCH] Initial commit --- app.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ dockerfile | 29 +++++++++++++++++++++ templates/index.html | 12 +++++++++ 3 files changed, 101 insertions(+) create mode 100644 app.py create mode 100644 dockerfile create mode 100644 templates/index.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..e4e102e --- /dev/null +++ b/app.py @@ -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) \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..afb3c86 --- /dev/null +++ b/dockerfile @@ -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"] \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..a079ba5 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ + + + + Video Compressor + + +
+ + +
+ + \ No newline at end of file