Generate concat*.txt files

This commit is contained in:
augustin64 2024-09-02 10:12:35 +02:00
parent b6309cf16c
commit 7f287353a4
2 changed files with 61 additions and 15 deletions

View File

@ -59,6 +59,18 @@ def crop_image(file):
img.crop((0, min_non_white, img.width-1, max_non_white)).save(file)
return img.width, max_non_white - min_non_white
def generate_concat(images, dest):
if os.path.exists(dest):
if input("Overwrite {dest} ? [y/N] ").lower() != "y":
return
content = "ffconcat version 1.0"
for img in images:
content += "\nfile {img}\nduration 8"
with open(dest, "w") as f:
f.write(content)
def adjust_images(images):
width, height = -1, -1
@ -90,8 +102,17 @@ if __name__ == "__main__":
sys.exit(1)
generate_images(sys.argv[1], sys.argv[2])
img_dir = os.path.dirname(sys.argv[2])
images = [
img for img in os.listdir(img_dir) if img.startswith(os.path.basename(sys.argv[2])) and img.endswith(".png") and "short" not in img
]
short_images = [
img for img in os.listdir(img_dir) if img.startswith(os.path.basename(sys.argv[2])) and img.endswith(".png") and "short" in img
]
adjust_images([
img for img in os.listdir(os.path.dirname(sys.argv[2])) if
img.startswith(os.path.basename(sys.argv[2])) and img.endswith(".png") and "short" not in img
os.path.join(img_dir, img) for img in images
])
generate_concat(images, os.path.join(img_dir, "concat.txt"))
generate_concat(short_images, os.path.join(img_dir, "concat_short.txt"))
logger.log("Generated concat*.txt files. Please edit them")

View File

@ -2,6 +2,7 @@ from xml.etree import ElementTree
import functools
import zipfile
import shutil
import json
"""
@ -16,9 +17,16 @@ class Patches:
patches = Patches()
def register_patch(file_ext):
def register_patch(ext=None, filename=None):
if ext is None and filename is None:
raise ValueError("You must specify at least one of the two parameters")
def inner_wrapper(view):
patches._data[view.__name__] = (view, file_ext)
if ext is not None:
patches._data[view.__name__] = (view, ext, False)
if filename is not None:
patches._data[view.__name__] = (view, filename, True)
@functools.wraps(view)
def wrapped_view(**kwargs):
@ -28,7 +36,7 @@ def register_patch(file_ext):
return inner_wrapper
@register_patch("mscx")
@register_patch(ext="mscx")
def create_pagebreaks(data):
"""
Replaces all manual linebreaks by pagebreaks in a .mscx file (xml)
@ -53,7 +61,7 @@ def create_pagebreaks(data):
return ElementTree.tostring(root)
@register_patch("mss")
@register_patch(ext="mss")
def remove_footer(data):
"""
Disable page footer in .mss (xml)
@ -66,6 +74,15 @@ def remove_footer(data):
return ElementTree.tostring(root)
@register_patch(filename="audiosettings.json")
def better_soundfont(data):
data = json.loads(data)
for track in data["tracks"]:
pass
return json.dump(data)
"""
The "main" apply function
@ -88,24 +105,32 @@ def apply(source, dest, patches):
Apply functions to patch files inside a mscz
- source : source file
- dest : destination file, must differ
- patches : list of (function, file extension)
- patches : list of (function, file extension or filename, is_filename)
"""
patches_by_ext = {}
for func, ext in patches:
if ext not in patches_by_ext:
patches_by_ext[ext] = []
patches_by_filename = {}
for func, arg, is_filename in patches:
if arg not in patches_by_ext:
patches_by_ext[arg] = []
patches_by_ext[arg].append(func)
patches_by_ext[ext].append(func)
if arg not in patches_by_filename:
patches_by_filename[arg] = []
patches_by_filename[arg].append(func)
shutil.copy(source, dest)
with zipfile.ZipFile(source) as inzip, zipfile.ZipFile(dest, "w") as outzip:
for inzipinfo in inzip.infolist():
with inzip.open(inzipinfo) as infile:
ext = inzipinfo.filename.split(".")[-1]
if ext in patches_by_ext:
data = infile.read()
if ext in patches_by_ext:
for func in patches_by_ext[ext]:
data = func(data)
if inzipinfo.filename in patches_by_filename:
for func in patches_by_filename[inzipinfo.filename]:
data = func(data)
outzip.writestr(inzipinfo.filename, data)
else:
outzip.writestr(inzipinfo.filename, infile.read())