forked from rDrama/rDrama
1
0
Fork 0

Use subprocess.check_call in media.py

master
Snakes 2022-06-17 16:15:42 -04:00
parent 62d5a0d8dc
commit a0d923e759
1 changed files with 20 additions and 9 deletions

View File

@ -16,7 +16,8 @@ def process_audio(file):
if os.stat(name).st_size > 8 * 1024 * 1024:
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()
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}'
@ -25,19 +26,26 @@ def process_audio(file):
def process_video(file):
old = f'/videos/{time.time()}'.replace('.','')
new = old + '.mp4'
# TODO: Respect the format of the uploaded file or transcode everything to
# MPEG-4. Preferably read actual format from file data.
if extension == 'webm':
file.save(new)
else:
file.save(old)
os.system(f'ffmpeg -y -loglevel warning -i {old} -map_metadata -1 -c:v copy -c:a copy {new}')
file.save(old)
try:
# This throws an uncaught CalledProcessError on non-zero return code.
# Until we explore how to get this to fail safely, will just bubble it
# to the top and 500 the request.
subprocess.check_call([
'ffmpeg', '-y', '-loglevel', 'warning', '-i', old,
'-map_metadata', '-1', '-c:v', 'copy', '-c:a', 'copy', new])
finally:
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()
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}'
@ -53,7 +61,9 @@ def process_image(patron, filename=None, resize=0):
i = Image.open(filename)
if resize and i.width > resize:
try: subprocess.call(["convert", filename, "-coalesce", "-resize", f"{resize}>", filename])
try:
subprocess.check_call(["convert", filename,
"-coalesce", "-resize", f"{resize}>", filename])
except: pass
elif i.format.lower() != "webp":
@ -65,7 +75,8 @@ def process_image(patron, filename=None, resize=0):
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")
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)