forked from MarseyWorld/MarseyWorld
refactor process_video
parent
5d74693c22
commit
06677d269e
|
@ -7,6 +7,7 @@ from flask import abort, g
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
from .const import *
|
from .const import *
|
||||||
|
import gevent
|
||||||
|
|
||||||
|
|
||||||
def process_files():
|
def process_files():
|
||||||
|
@ -47,26 +48,34 @@ def process_audio(file):
|
||||||
return f'{SITE_FULL}{name}'
|
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):
|
def process_video(file):
|
||||||
old = f'/videos/{time.time()}'.replace('.','')
|
old = f'/videos/{time.time()}'.replace('.','')
|
||||||
new = old + '.mp4'
|
|
||||||
file.save(old)
|
file.save(old)
|
||||||
|
new = old + '.mp4'
|
||||||
|
|
||||||
if file.filename.split('.')[-1].lower() == 'webm':
|
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:
|
else:
|
||||||
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "warning", "-i", old, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "copy", new], check=True)
|
||||||
|
os.remove(old)
|
||||||
os.remove(old)
|
if os.stat(new).st_size > 8 * 1024 * 1024:
|
||||||
size = os.stat(new).st_size
|
with open(new, 'rb') as f:
|
||||||
if os.stat(new).st_size > 8 * 1024 * 1024:
|
os.remove(new)
|
||||||
with open(new, 'rb') as f:
|
req = requests.request("POST", "https://pomf2.lain.la/upload.php",
|
||||||
os.remove(new)
|
files={'files[]': f}, timeout=20).json()
|
||||||
req = requests.request("POST", "https://pomf2.lain.la/upload.php",
|
return req['files'][0]['url']
|
||||||
files={'files[]': f}, timeout=20).json()
|
return f'{SITE_FULL}{new}'
|
||||||
return req['files'][0]['url']
|
|
||||||
|
|
||||||
return f'{SITE_FULL}{new}'
|
|
||||||
|
|
||||||
|
|
||||||
def process_image(filename=None, resize=0):
|
def process_image(filename=None, resize=0):
|
||||||
|
|
|
@ -47,11 +47,16 @@ def error_405(e):
|
||||||
|
|
||||||
@app.errorhandler(413)
|
@app.errorhandler(413)
|
||||||
def error_413(e):
|
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"):
|
if request.headers.get("Authorization") or request.headers.get("xhr"):
|
||||||
return {"error": "Max image/audio size is 8 MB (16 MB for paypigs)"}, 413
|
return {"error": "Max image/audio size is 8 MB (16 MB for paypigs)"}, 413
|
||||||
else: return render_template('errors/413.html', err=True), 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)
|
@app.errorhandler(429)
|
||||||
def error_429(e):
|
def error_429(e):
|
||||||
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "429 Too Many Requests"}, 429
|
if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "429 Too Many Requests"}, 429
|
||||||
|
|
|
@ -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 %}
|
Loading…
Reference in New Issue