Impoved style

This commit is contained in:
Heorhii Mykhailenko 2023-06-15 21:04:28 +03:00
parent 749a36b445
commit 4d9aecf18e
6 changed files with 58 additions and 23 deletions

9
.gitignore vendored
View file

@ -129,3 +129,12 @@ dmypy.json
.pyre/ .pyre/
config.ini config.ini
videos/
*.mp4
*.webm
*.mkv
*.avi
*.ogv
*.ogg
*.mp3
*.flac

View file

@ -1,4 +1,8 @@
import configparser, email, os, smtplib, ssl import configparser
import email
import os
import smtplib
import ssl
from email import encoders from email import encoders
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
@ -8,6 +12,7 @@ from email.mime.text import MIMEText
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read('config.ini') config.read('config.ini')
def send_email(destination, attachment): def send_email(destination, attachment):
subject = 'Your modified video' subject = 'Your modified video'
body = 'You have recently used our service to modify a video. The result is attached.' body = 'You have recently used our service to modify a video. The result is attached.'

30
main.py
View file

@ -10,12 +10,15 @@ import moviepy.editor as moviepy
app = Flask(__name__) app = Flask(__name__)
ALLOWED_UPLOAD_EXTENSIONS = {'mp4', 'webm', 'mkv', 'avi', 'ogv'} ALLOWED_UPLOAD_EXTENSIONS = {'mp4', 'webm', 'mkv', 'avi', 'ogv'}
ALLOWED_CONVERT_EXTENSIONS = {'mp4', 'webm', 'mkv', 'avi', 'ogv', 'ogg', 'mp3', 'flac'} ALLOWED_CONVERT_EXTENSIONS = {'mp4', 'webm',
'mkv', 'avi', 'ogv', 'ogg', 'mp3', 'flac'}
AUDIO_EXTENSIONS = {'ogg', 'mp3', 'flac'} AUDIO_EXTENSIONS = {'ogg', 'mp3', 'flac'}
ALLOWED_WATERMARK_EXTENSIONS = {'bmp', 'png', 'jpg', 'jpeg', 'tiff', 'tga', 'svg'} ALLOWED_WATERMARK_EXTENSIONS = {
'bmp', 'png', 'jpg', 'jpeg', 'tiff', 'tga', 'svg'}
percentages = {} percentages = {}
class BarLogger(ProgressBarLogger): class BarLogger(ProgressBarLogger):
global percentages global percentages
id = None id = None
@ -27,12 +30,14 @@ class BarLogger(ProgressBarLogger):
super(self.__class__, self).__init__() super(self.__class__, self).__init__()
self.id = id self.id = id
def is_integer(n): def is_integer(n):
try: try:
return float(n).is_integer() return float(n).is_integer()
except ValueError: except ValueError:
return False return False
def getExtension(filename, isWatermark=False): def getExtension(filename, isWatermark=False):
if not '.' in filename: if not '.' in filename:
return False return False
@ -44,6 +49,7 @@ def getExtension(filename, isWatermark=False):
else: else:
return False return False
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def index(): def index():
if request.method == 'POST' and 'video' in request.files: if request.method == 'POST' and 'video' in request.files:
@ -59,12 +65,15 @@ def index():
return render_template('index.html') return render_template('index.html')
@app.route('/video', methods=['GET', 'POST']) @app.route('/video', methods=['GET', 'POST'])
def video(): def video():
error = '' error = ''
changed = False changed = False
audio = None audio = None
id = request.args.get('id') id = request.args.get('id')
if id == None:
abort(400)
path = os.path.join('videos', id) path = os.path.join('videos', id)
if id != None and os.path.isfile(path): if id != None and os.path.isfile(path):
clip = moviepy.VideoFileClip(path) clip = moviepy.VideoFileClip(path)
@ -101,20 +110,23 @@ def video():
else: else:
watermarkExtension = getExtension(file.filename, True) watermarkExtension = getExtension(file.filename, True)
if watermarkExtension: if watermarkExtension:
watermarkPath = os.path.join('videos', id + '-watermark' + '.' + watermarkExtension) watermarkPath = os.path.join(
'videos', id + '-watermark' + '.' + watermarkExtension)
file.save(watermarkPath) file.save(watermarkPath)
formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'} formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'}
img = Image.open(watermarkPath) img = Image.open(watermarkPath)
rgbimg = Image.new(formatter.get(img.format, 'RGB'), img.size) rgbimg = Image.new(formatter.get(
img.format, 'RGB'), img.size)
rgbimg.paste(img) rgbimg.paste(img)
rgbimg.save(watermarkPath, format=img.format) rgbimg.save(watermarkPath, format=img.format)
watermark = (moviepy.ImageClip(watermarkPath) watermark = (moviepy.ImageClip(watermarkPath)
.set_duration(clip.duration) .set_duration(clip.duration)
.set_pos(('right', 'bottom'))) .set_pos(('right', 'bottom')))
clip = moviepy.CompositeVideoClip([clip, watermark]) clip = moviepy.CompositeVideoClip(
[clip, watermark])
os.remove(watermarkPath) os.remove(watermarkPath)
changed = True changed = True
else: else:
@ -133,10 +145,11 @@ def video():
os.remove(path) os.remove(path)
return render_template('success.html', error=error) return render_template('success.html', error=error)
return render_template('video.html', id=id, length=int(clip.duration), error=error) return render_template('video.html', length=int(clip.duration), error=error)
else: else:
abort(404) abort(404)
@app.route('/progress', methods=['GET']) @app.route('/progress', methods=['GET'])
def progress(): def progress():
id = request.args.get('id') id = request.args.get('id')
@ -146,6 +159,7 @@ def progress():
else: else:
abort(404) abort(404)
if __name__ == '__main__': if __name__ == '__main__':
if not os.path.isdir('videos'): if not os.path.isdir('videos'):
os.mkdir('videos') os.mkdir('videos')

View file

@ -1,8 +1,10 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Video upload</title> <title>Video upload</title>
</head> </head>
<body> <body>
{% if error %} {% if error %}
<p>{{ error | safe }}</p> <p>{{ error | safe }}</p>
@ -13,5 +15,5 @@
<button id="submit" type="submit">Submit</button> <button id="submit" type="submit">Submit</button>
</form> </form>
</body> </body>
</p>
</html> </html>

View file

@ -1,15 +1,17 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Video upload</title> <title>Video upload</title>
</head> </head>
<body> <body>
<p>The modified video was sent to your email address. You can upload another one if you want to.</p> <p>The modified video was sent to your email address. You can upload another one if you want to.</p>
{% if error %} {% if error %}
<p>The following errors occurred:<br> <p>The following errors occurred:<br>
{{ error | safe }}</p> {{ error | safe }}</p>
{% endif %} {% endif %}
<p><a href="index.html">Upload another video</a></p> <p><a href="index.html">Upload another video</a></p>
</body> </body>
</p>
</html> </html>

View file

@ -1,13 +1,15 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Video editing</title> <title>Video editing</title>
</head> </head>
<body> <body>
{% if error %} {% if error %}
<p>{{ error | safe }}</p> <p>{{ error | safe }}</p>
{% endif %} {% endif %}
<p>Video {{name}} ({{length}})</p> <p>Video length: {{length}} s</p>
<form method="POST" enctype="multipart/form-data" onsubmit="trackProgress()"> <form method="POST" enctype="multipart/form-data" onsubmit="trackProgress()">
<label for="start">Start (s): </label><br> <label for="start">Start (s): </label><br>
<input type="text" id="start" name="start"><br> <input type="text" id="start" name="start"><br>
@ -27,7 +29,7 @@
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id'); const id = urlParams.get('id');
const xhttp = new XMLHttpRequest(); const xhttp = new XMLHttpRequest();
xhttp.onload = function() { xhttp.onload = function () {
document.getElementById("bar").value = this.responseText; document.getElementById("bar").value = this.responseText;
} }
xhttp.open("GET", "/progress?id=" + id, true); xhttp.open("GET", "/progress?id=" + id, true);
@ -42,4 +44,5 @@
</script> </script>
</body> </body>
</html> </html>