rDrama/files/helpers/media.py

70 lines
1.9 KiB
Python
Raw Normal View History

2022-05-04 23:09:46 +00:00
from PIL import Image, ImageOps
from PIL.ImageSequence import Iterator
from webptools import gifwebp
import subprocess
2022-05-07 06:04:14 +00:00
import os
from flask import abort
2022-05-22 16:13:19 +00:00
import requests
import time
from .const import *
2022-05-22 22:15:29 +00:00
2022-05-23 19:00:14 +00:00
def process_audio(file):
2022-05-24 15:32:49 +00:00
name = f'/audio/{time.time()}'.replace('.','') + '.' + file.filename.split('.')[-1]
2022-05-22 22:15:29 +00:00
file.save(name)
2022-05-23 18:03:59 +00:00
2022-05-23 19:13:33 +00:00
if SITE_NAME == 'WPD' or os.stat(name).st_size > 8 * 1024 * 1024:
2022-05-23 18:03:59 +00:00
with open(name, 'rb') as f:
os.remove(name)
req = requests.request("POST", "https://pomf2.lain.la/upload.php", files={'files[]': f}, timeout=20).json()
return req['files'][0]['url']
2022-05-22 22:15:29 +00:00
return f'{SITE_FULL}{name}'
2022-05-22 16:13:19 +00:00
def process_video(file):
2022-05-22 20:05:44 +00:00
name = f'/videos/{time.time()}'.replace('.','')
file.save(name)
2022-05-22 22:37:59 +00:00
2022-05-23 19:00:14 +00:00
os.system(f'ffmpeg -y -loglevel warning -i {name} -map_metadata -1 -c:v copy -c:a copy {name}.mp4')
2022-05-22 20:05:44 +00:00
os.remove(name)
2022-05-24 15:32:49 +00:00
name += '.' + file.filename.split('.')[-1]
2022-05-22 20:05:44 +00:00
2022-05-23 19:00:14 +00:00
size = os.stat(name).st_size
2022-05-23 19:13:33 +00:00
if SITE_NAME == 'WPD' or os.stat(name).st_size > 8 * 1024 * 1024:
2022-05-23 19:00:14 +00:00
with open(name, 'rb') as f:
os.remove(name)
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}{name}'
2022-05-22 16:13:19 +00:00
2022-05-07 06:04:14 +00:00
def process_image(patron, filename=None, resize=0):
size = os.stat(filename).st_size
if size > 16 * 1024 * 1024 or not patron and size > 8 * 1024 * 1024:
os.remove(filename)
abort(413)
2022-05-04 23:09:46 +00:00
i = Image.open(filename)
if resize and i.width > resize:
try: subprocess.call(["convert", filename, "-coalesce", "-resize", f"{resize}>", filename])
except: pass
elif i.format.lower() != "webp":
exif = i.getexif()
for k in exif.keys():
if k != 0x0112:
exif[k] = None
del exif[k]
i.info["exif"] = exif.tobytes()
if i.format.lower() == "gif":
gifwebp(input_image=filename, output_image=filename, option="-mixed -metadata none -f 100 -mt -m 6")
else:
i = ImageOps.exif_transpose(i)
i.save(filename, format="WEBP", method=6)
2022-05-22 20:05:44 +00:00
return filename