Add compression estimates, transparent image output and full format selection
This commit is contained in:
1 parent
65cebb31ca
commit
2b897267d1
22 files changed
+220
-30
No files matched your search
@@ -83,7 +83,9 @@ def save_output(source, suffix, writer, name=None):
|
||||
|
||||
def image_load(file):
|
||||
with Image.open(file) as image:
|
||||
return ImageOps.exif_transpose(image).copy()
|
||||
image=ImageOps.exif_transpose(image)
|
||||
if image.mode=='LA' or 'transparency' in image.info: image=image.convert('RGBA')
|
||||
return image.copy()
|
||||
def image_write(image, out, target, quality=85):
|
||||
image.info.clear()
|
||||
fmt = FORMATS[target]
|
||||
@@ -189,17 +191,26 @@ def convert(file,target,options):
|
||||
else: text_write(text_read(file),out,target)
|
||||
return save_output(file,target,writer,options.get('outputName'))
|
||||
|
||||
def compress(file,options):
|
||||
def compression_format(file):
|
||||
kind=ext(file)
|
||||
if kind in IMAGE:
|
||||
if kind in {'png','webp','avif'}: return kind
|
||||
with Image.open(file) as image:
|
||||
if 'A' in image.getbands() or 'transparency' in image.info: return 'png'
|
||||
return 'jpg'
|
||||
return 'mp4' if kind in VIDEO else 'mp3' if kind in AUDIO else 'pdf'
|
||||
|
||||
def compress(file,options,estimate=False):
|
||||
kind=ext(file); quality=int(bounded(options.get('quality'),75,5,95))
|
||||
limit=int(bounded(options.get('maxSizeMB'),0,0,100000)*1024*1024)
|
||||
suffix = ('jpg' if kind in IMAGE else 'mp4' if kind in VIDEO else 'mp3' if kind in AUDIO else 'pdf')
|
||||
suffix = compression_format(file)
|
||||
def writer(out):
|
||||
if kind in IMAGE:
|
||||
image=image_load(file)
|
||||
suffix_format='jpg'
|
||||
suffix_format=suffix
|
||||
for q in range(quality,4,-5):
|
||||
image_write(image,out,suffix_format,q)
|
||||
if not limit or out.stat().st_size <= limit: break
|
||||
if not limit or out.stat().st_size <= limit or suffix=='png': break
|
||||
for _ in range(8):
|
||||
if not limit or out.stat().st_size <= limit: break
|
||||
image.thumbnail((max(1,int(image.width*.8)),max(1,int(image.height*.8))))
|
||||
@@ -215,10 +226,27 @@ def compress(file,options):
|
||||
elif kind=='pdf':
|
||||
pdf=PdfWriter(clone_from=reader(file))
|
||||
for page in pdf.pages: page.compress_content_streams()
|
||||
pdf.compress_identical_objects(remove_identicals=True,remove_orphans=True)
|
||||
pdf.compress_identical_objects(remove_duplicates=True,remove_unreferenced=True)
|
||||
pdf.write(out)
|
||||
else: raise ValueError('Diese Datei lässt sich nicht komprimieren.')
|
||||
return {'targetMet':not limit or out.stat().st_size<=limit}
|
||||
if estimate:
|
||||
original=Path(file).stat().st_size
|
||||
if kind in VIDEO|AUDIO:
|
||||
info=probe(file); duration=max(info['duration'],.1)
|
||||
if kind in VIDEO:
|
||||
bitrate=max(32000,int(limit*8/duration*.88-128000)) if limit else int(300000+quality*40000)
|
||||
size=int(duration*(bitrate+(128000 if info['audio'] else 0))/8*1.03)
|
||||
else:
|
||||
bitrate=max(32,min(320,int(limit*8/duration/1000*.95))) if limit else max(32,int(quality*2.56))
|
||||
size=int(duration*bitrate*1000/8*1.03)
|
||||
exact=False
|
||||
else:
|
||||
with tempfile.TemporaryDirectory(prefix='.filebow-'+os.environ.get('FILEBOW_JOB_ID','local')+'-',dir=Path(file).parent) as folder:
|
||||
emit({'type':'stage','directory':folder})
|
||||
out=Path(folder)/('estimate.'+suffix); writer(out); size=out.stat().st_size
|
||||
exact=True
|
||||
return {'ok':True,'input':str(file),'size':size,'originalSize':original,'format':suffix,'exact':exact,'targetMet':not limit or size<=limit}
|
||||
return save_output(file,suffix,writer,options.get('outputName'))
|
||||
|
||||
def edit(file,options):
|
||||
@@ -274,6 +302,7 @@ def combined(files,target,options):
|
||||
def dispatch(file,op,target,options):
|
||||
if op=='convert': return convert(file,target,options)
|
||||
if op=='compress': return compress(file,options)
|
||||
if op=='estimate-compression': return compress(file,options,estimate=True)
|
||||
if op=='edit': return edit(file,options)
|
||||
if op=='strip-metadata': return metadata(file,options)
|
||||
if op=='pdf-password':
|
||||
|
||||
Reference in new issue
Block a user