refactor process_video

master
Aevann1 2022-06-19 16:49:44 +02:00
parent 5d74693c22
commit 06677d269e
3 changed files with 47 additions and 13 deletions

View File

@ -7,6 +7,7 @@ from flask import abort, g
import requests
import time
from .const import *
import gevent
def process_files():
@ -47,28 +48,36 @@ def process_audio(file):
return f'{SITE_FULL}{name}'
def webm_to_mp4(old, new):
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", new], check=True, stderr=subprocess.STDOUT)
os.remove(old)
requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, json={'files': [f"{SITE_FULL}{new}"]}, timeout=5)
def process_video(file):
old = f'/videos/{time.time()}'.replace('.','')
new = old + '.mp4'
file.save(old)
new = old + '.mp4'
if file.filename.split('.')[-1].lower() == 'webm':
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", new], check=True, stderr=subprocess.STDOUT)
file.save(new)
if os.stat(new).st_size > 16 * 1024 * 1024:
abort(418)
else:
gevent.spawn(webm_to_mp4, old, new)
return f'{SITE_FULL}{new}'
else:
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
os.remove(old)
size = os.stat(new).st_size
if os.stat(new).st_size > 8 * 1024 * 1024:
with open(new, 'rb') as f:
os.remove(new)
req = requests.request("POST", "https://pomf2.lain.la/upload.php",
files={'files[]': f}, timeout=20).json()
return req['files'][0]['url']
return f'{SITE_FULL}{new}'
def process_image(filename=None, resize=0):
size = os.stat(filename).st_size

View File

@ -47,11 +47,16 @@ def error_405(e):
@app.errorhandler(413)
def error_413(e):
return {"error": "Max image/audio size is 8 MB (16 MB for paypigs)"}, 413
if request.headers.get("Authorization") or request.headers.get("xhr"):
return {"error": "Max image/audio size is 8 MB (16 MB for paypigs)"}, 413
else: return render_template('errors/413.html', err=True), 413
@app.errorhandler(418)
def error_418(e):
if request.headers.get("Authorization") or request.headers.get("xhr"):
return {"error": "WEBM videos are not allowed, please convert your video to MP4 and re-upload it!"}, 418
else: return render_template('errors/418.html', err=True), 418
@app.errorhandler(429)
def error_429(e):
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "429 Too Many Requests"}, 429

View File

@ -0,0 +1,20 @@
{% extends "default.html" %}
{% block title %}
<title>WEBM videos are not allowed</title>
{% endblock %}
{% block pagetype %}error-418{% endblock %}
{% block content %}
<div class="row justify-content-center">
<div class="col-10 col-md-5">
<div class="text-center px-3 my-8">
<img alt=":#marseyretard:" loading="lazy" src="/e/marseyretard.webp">
<pre></pre>
<h1 class="h5">WEBM videos are not allowed</h1>
<div><a href="/" class="btn btn-primary">please convert your video to MP4 and re-upload it!</a></div>
</div>
</div>
</div>
{% endblock %}