diff --git a/mail.py b/mail.py old mode 100755 new mode 100644 index 646b9d0..388f7a6 --- a/mail.py +++ b/mail.py @@ -38,7 +38,7 @@ def send_email(destination, attachment): # Add header as key/value pair to attachment part part.add_header( 'Content-Disposition', - f'attachment; filename= {os.path.normpath(attachment)}', + f'attachment; filename= {os.path.basename(attachment)}', ) # Add attachment to message and convert message to string diff --git a/main.py b/main.py old mode 100755 new mode 100644 index cf877b1..763d07e --- a/main.py +++ b/main.py @@ -76,78 +76,83 @@ def video(): abort(400) path = os.path.join('videos', id) if os.path.isfile(path): - clip = moviepy.VideoFileClip(path) - if request.method == 'POST': - start = request.form.get('start') - end = request.form.get('end') - extension = request.form.get('extension') - email = request.form.get('email') - if not email: - abort(400) - if start and end: - if is_integer(start) and is_integer(end) and int(start) < int(end): - if int(start) < 0 or int(end) > clip.duration: - error += 'Selected subclip out of bounds.
' - else: - clip = clip.subclip(start, end) - changed = True - else: - error += 'Invalid start or end times.
' - if extension: - if extension in ALLOWED_CONVERT_EXTENSIONS: - if extension in AUDIO_EXTENSIONS: - audio = clip.audio - newId = id.rsplit('.', 1)[0] + '.' + extension - if id == newId: - error += 'A different extension than the currently used one is needed.
' - else: - changed = True - else: - error += 'Requested recode extension not allowed.
' - if 'watermark' in request.files: - file = request.files['watermark'] - if file.filename: - if audio: - error += 'Audio cannot be watermarked.
' - else: - watermarkExtension = getExtension(file.filename, True) - if watermarkExtension: - watermarkPath = os.path.join( - 'videos', id + '-watermark' + '.' + watermarkExtension) - file.save(watermarkPath) - - formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'} - img = Image.open(watermarkPath) - rgbimg = Image.new(formatter.get( - img.format, 'RGB'), img.size) - rgbimg.paste(img) - rgbimg.save(watermarkPath, format=img.format) - - watermark = (moviepy.ImageClip(watermarkPath) - .set_duration(clip.duration) - .set_pos(('right', 'bottom'))) - - clip = moviepy.CompositeVideoClip( - [clip, watermark]) - os.remove(watermarkPath) - changed = True + with moviepy.VideoFileClip(path) as clip: + length = int(clip.duration) + if request.method == 'POST': + start = request.form.get('start') + end = request.form.get('end') + extension = request.form.get('extension') + email = request.form.get('email') + if not email: + abort(400) + if start and end: + if is_integer(start) and is_integer(end) and int(start) < int(end): + if int(start) < 0 or int(end) > clip.duration: + error += 'Selected subclip out of bounds.
' else: - error += 'Non-allowed watermark extension.
' - if changed: - logger = BarLogger(id) - if 'newId' in locals(): - os.remove(path) - id = newId - path = os.path.join('videos', id) - if audio: - audio.write_audiofile(path, logger=logger) - else: - clip.write_videofile(path, logger=logger) - send_email(email, path) - os.remove(path) - return render_template('success.html', error=error) + clip = clip.subclip(start, end) + changed = True + else: + error += 'Invalid start or end times.
' + if extension: + if extension in ALLOWED_CONVERT_EXTENSIONS: + if extension in AUDIO_EXTENSIONS: + audio = clip.audio + newId = id.rsplit('.', 1)[0] + '.' + extension + if id == newId: + error += 'A different extension than the currently used one is needed.
' + else: + changed = True + else: + error += 'Requested recode extension not allowed.
' + if 'watermark' in request.files: + file = request.files['watermark'] + if file.filename: + if audio: + error += 'Audio cannot be watermarked.
' + else: + watermarkExtension = getExtension(file.filename, True) + if watermarkExtension: + watermarkPath = os.path.join( + 'videos', id + '-watermark' + '.' + watermarkExtension) + file.save(watermarkPath) - return render_template('video.html', length=int(clip.duration), error=error) + formatter = {'PNG': 'RGBA', 'JPEG': 'RGB'} + with Image.open(watermarkPath) as img: + rgbimg = Image.new(formatter.get( + img.format, 'RGB'), img.size) + rgbimg.paste(img) + rgbimg.save(watermarkPath, format=img.format) + + watermark = (moviepy.ImageClip(watermarkPath) + .set_duration(clip.duration) + .set_pos(('right', 'bottom'))) + + clip = moviepy.CompositeVideoClip( + [clip, watermark]) + os.remove(watermarkPath) + changed = True + else: + error += 'Non-allowed watermark extension.
' + if changed: + logger = BarLogger(id) + if 'newId' in locals(): + oldPath = path + path = os.path.join('videos', newId) + if audio: + audio.write_audiofile(path, logger=logger) + audio.close() + else: + clip.write_videofile(path, logger=logger) + clip.close() + send_email(email, path) + if changed: + if 'oldPath' in locals(): + os.remove(oldPath) + os.remove(path) + return render_template('success.html', error=error) + + return render_template('video.html', length=length, error=error) else: abort(404) diff --git a/templates/success.html b/templates/success.html old mode 100755 new mode 100644 index a41c7f6..e149577 --- a/templates/success.html +++ b/templates/success.html @@ -11,7 +11,7 @@

The following errors occurred:
{{ error | safe }}

{% endif %} -

Upload another video

+

Upload another video

\ No newline at end of file