Fixed bugs
This commit is contained in:
parent
d887231d04
commit
abfece95e7
3 changed files with 77 additions and 72 deletions
2
mail.py
Executable file → Normal file
2
mail.py
Executable file → Normal file
|
@ -38,7 +38,7 @@ def send_email(destination, attachment):
|
||||||
# Add header as key/value pair to attachment part
|
# Add header as key/value pair to attachment part
|
||||||
part.add_header(
|
part.add_header(
|
||||||
'Content-Disposition',
|
'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
|
# Add attachment to message and convert message to string
|
||||||
|
|
145
main.py
Executable file → Normal file
145
main.py
Executable file → Normal file
|
@ -76,78 +76,83 @@ def video():
|
||||||
abort(400)
|
abort(400)
|
||||||
path = os.path.join('videos', id)
|
path = os.path.join('videos', id)
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
clip = moviepy.VideoFileClip(path)
|
with moviepy.VideoFileClip(path) as clip:
|
||||||
if request.method == 'POST':
|
length = int(clip.duration)
|
||||||
start = request.form.get('start')
|
if request.method == 'POST':
|
||||||
end = request.form.get('end')
|
start = request.form.get('start')
|
||||||
extension = request.form.get('extension')
|
end = request.form.get('end')
|
||||||
email = request.form.get('email')
|
extension = request.form.get('extension')
|
||||||
if not email:
|
email = request.form.get('email')
|
||||||
abort(400)
|
if not email:
|
||||||
if start and end:
|
abort(400)
|
||||||
if is_integer(start) and is_integer(end) and int(start) < int(end):
|
if start and end:
|
||||||
if int(start) < 0 or int(end) > clip.duration:
|
if is_integer(start) and is_integer(end) and int(start) < int(end):
|
||||||
error += 'Selected subclip out of bounds.<br>'
|
if int(start) < 0 or int(end) > clip.duration:
|
||||||
else:
|
error += 'Selected subclip out of bounds.<br>'
|
||||||
clip = clip.subclip(start, end)
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
error += 'Invalid start or end times.<br>'
|
|
||||||
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.<br>'
|
|
||||||
else:
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
error += 'Requested recode extension not allowed.<br>'
|
|
||||||
if 'watermark' in request.files:
|
|
||||||
file = request.files['watermark']
|
|
||||||
if file.filename:
|
|
||||||
if audio:
|
|
||||||
error += 'Audio cannot be watermarked.<br>'
|
|
||||||
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
|
|
||||||
else:
|
else:
|
||||||
error += 'Non-allowed watermark extension.<br>'
|
clip = clip.subclip(start, end)
|
||||||
if changed:
|
changed = True
|
||||||
logger = BarLogger(id)
|
else:
|
||||||
if 'newId' in locals():
|
error += 'Invalid start or end times.<br>'
|
||||||
os.remove(path)
|
if extension:
|
||||||
id = newId
|
if extension in ALLOWED_CONVERT_EXTENSIONS:
|
||||||
path = os.path.join('videos', id)
|
if extension in AUDIO_EXTENSIONS:
|
||||||
if audio:
|
audio = clip.audio
|
||||||
audio.write_audiofile(path, logger=logger)
|
newId = id.rsplit('.', 1)[0] + '.' + extension
|
||||||
else:
|
if id == newId:
|
||||||
clip.write_videofile(path, logger=logger)
|
error += 'A different extension than the currently used one is needed.<br>'
|
||||||
send_email(email, path)
|
else:
|
||||||
os.remove(path)
|
changed = True
|
||||||
return render_template('success.html', error=error)
|
else:
|
||||||
|
error += 'Requested recode extension not allowed.<br>'
|
||||||
|
if 'watermark' in request.files:
|
||||||
|
file = request.files['watermark']
|
||||||
|
if file.filename:
|
||||||
|
if audio:
|
||||||
|
error += 'Audio cannot be watermarked.<br>'
|
||||||
|
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.<br>'
|
||||||
|
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:
|
else:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
|
2
templates/success.html
Executable file → Normal file
2
templates/success.html
Executable file → Normal file
|
@ -11,7 +11,7 @@
|
||||||
<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="/">Upload another video</a></p>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
Reference in a new issue