Compare commits

..

No commits in common. "master" and "1.0.1" have entirely different histories.

7 changed files with 280 additions and 386 deletions

@ -5,34 +5,27 @@ A CLI to generate comparison image sets with
## Usage ## Usage
``` ```
usage: FrameForge [-h] [-v] [--source SOURCE] [--encode ENCODE] [--frames FRAMES] [--image-dir IMAGE_DIR] usage: Comparison Image Generator [-h] [-v] [--source SOURCE] [--encode ENCODE] [--image-dir IMAGE_DIR]
[--indexer {lsmash,ffms2}] [--img-lib {imwri,fpng}] [--source-index-path SOURCE_INDEX_PATH] [--indexer {lsmash,ffms2}] [--index-dir INDEX_DIR] [--sub-size SUB_SIZE]
[--encode-index-path ENCODE_INDEX_PATH] [--sub-size SUB_SIZE] [--sub-alignment SUB_ALIGNMENT] [--left-crop LEFT_CROP] [--right-crop RIGHT_CROP] [--top-crop TOP_CROP]
[--left-crop LEFT_CROP] [--right-crop RIGHT_CROP] [--top-crop TOP_CROP] [--bottom-crop BOTTOM_CROP] [--bottom-crop BOTTOM_CROP] [--adv-resize-left ADV_RESIZE_LEFT]
[--adv-resize-left ADV_RESIZE_LEFT] [--adv-resize-right ADV_RESIZE_RIGHT] [--adv-resize-right ADV_RESIZE_RIGHT] [--adv-resize-top ADV_RESIZE_TOP]
[--adv-resize-top ADV_RESIZE_TOP] [--adv-resize-bottom ADV_RESIZE_BOTTOM] [--tone-map] [--adv-resize-bottom ADV_RESIZE_BOTTOM] [--tone-map] [--re-sync RE_SYNC]
[--re-sync RE_SYNC] [--comparison-count COMPARISON_COUNT] [--subtitle-color SUBTITLE_COLOR] [--comparison-count COMPARISON_COUNT] [--subtitle-color SUBTITLE_COLOR]
[--release-sub-title RELEASE_SUB_TITLE] [--release-sub-title RELEASE_SUB_TITLE]
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version show program's version number and exit -v, --version show program's version number and exit
--source SOURCE Path to source file --source SOURCE Path to source file
--encode ENCODE Path to encode file --encode ENCODE Path to encode file
--frames FRAMES Only use this if you want to specify the frames to generate, this disables sync frames
--image-dir IMAGE_DIR --image-dir IMAGE_DIR
Path to base image folder Path to base image folder
--indexer {lsmash,ffms2} --indexer {lsmash,ffms2}
Indexer choice Indexer choice
--img-lib {imwri,fpng} --index-dir INDEX_DIR
Image library to use Path to look/create indexes
--source-index-path SOURCE_INDEX_PATH
Path to look/create indexes for source
--encode-index-path ENCODE_INDEX_PATH
Path to look/create indexes for encode
--sub-size SUB_SIZE Size of subtitles --sub-size SUB_SIZE Size of subtitles
--sub-alignment SUB_ALIGNMENT
Alignment of subtitles (.ass)
--left-crop LEFT_CROP --left-crop LEFT_CROP
Left crop Left crop
--right-crop RIGHT_CROP --right-crop RIGHT_CROP
@ -53,7 +46,7 @@ options:
--comparison-count COMPARISON_COUNT --comparison-count COMPARISON_COUNT
Amount of comparisons to generate Amount of comparisons to generate
--subtitle-color SUBTITLE_COLOR --subtitle-color SUBTITLE_COLOR
Hex color code for subtitle color (i.e. --subtitle-color "#fff000") Hex color code for subtitle color
--release-sub-title RELEASE_SUB_TITLE --release-sub-title RELEASE_SUB_TITLE
Release group subtitle name (this will show on the encode images) Release group subtitle name (this will show on the encode images)
``` ```

@ -3,11 +3,10 @@ from argparse import ArgumentParser
from frame_forge import GenerateImages from frame_forge import GenerateImages
from frame_forge.exceptions import FrameForgeError from frame_forge.exceptions import FrameForgeError
from frame_forge.utils import exit_application from frame_forge.utils import exit_application
from frame_forge.cli_utils import frame_list
program_name = "FrameForge" program_name = "FrameForge"
__version__ = "1.3.1" __version__ = "1.0.1"
if __name__ == "__main__": if __name__ == "__main__":
@ -19,37 +18,15 @@ if __name__ == "__main__":
parser.add_argument("--source", type=str, help="Path to source file") parser.add_argument("--source", type=str, help="Path to source file")
parser.add_argument("--encode", type=str, help="Path to encode file") parser.add_argument("--encode", type=str, help="Path to encode file")
parser.add_argument(
"--frames",
type=frame_list,
help="Only use this if you want to specify the "
"frames to generate, this disables sync frames",
)
parser.add_argument("--image-dir", type=str, help="Path to base image folder") parser.add_argument("--image-dir", type=str, help="Path to base image folder")
parser.add_argument( parser.add_argument(
"--indexer", "--indexer",
type=str, type=str,
choices=["lsmash", "ffms2"], choices=["lsmash", "ffms2"],
default="lsmash",
help="Indexer choice", help="Indexer choice",
) )
parser.add_argument( parser.add_argument("--index-dir", type=str, help="Path to look/create indexes")
"--img-lib", parser.add_argument("--sub-size", type=int, help="Size of subtitles")
type=str,
choices=["imwri", "fpng"],
default="fpng",
help="Image library to use",
)
parser.add_argument(
"--source-index-path", type=str, help="Path to look/create indexes for source"
)
parser.add_argument(
"--encode-index-path", type=str, help="Path to look/create indexes for encode"
)
parser.add_argument("--sub-size", type=int, default=20, help="Size of subtitles")
parser.add_argument(
"--sub-alignment", type=int, default=7, help="Alignment of subtitles (.ass)"
)
parser.add_argument("--left-crop", type=int, help="Left crop") parser.add_argument("--left-crop", type=int, help="Left crop")
parser.add_argument("--right-crop", type=int, help="Right crop") parser.add_argument("--right-crop", type=int, help="Right crop")
parser.add_argument("--top-crop", type=int, help="Top crop") parser.add_argument("--top-crop", type=int, help="Top crop")
@ -70,9 +47,7 @@ if __name__ == "__main__":
"--comparison-count", type=int, help="Amount of comparisons to generate" "--comparison-count", type=int, help="Amount of comparisons to generate"
) )
parser.add_argument( parser.add_argument(
"--subtitle-color", "--subtitle-color", type=str, help="Hex color code for subtitle color"
type=str,
help='Hex color code for subtitle color (i.e. --subtitle-color "#fff000")',
) )
parser.add_argument( parser.add_argument(
"--release-sub-title", "--release-sub-title",
@ -98,16 +73,6 @@ if __name__ == "__main__":
1, 1,
) )
index_suffix = ".lwi" if args.indexer == "lsmash" else ".ffindex"
for index_input in [args.source_index_path, args.encode_index_path]:
if index_input:
if Path(index_input).suffix != index_suffix:
exit_application(
f"When using {args.indexer} indexer you must use '{index_suffix}' "
"for your source/encode index path suffix",
1,
)
if args.image_dir: if args.image_dir:
image_dir = Path(args.image_dir) image_dir = Path(args.image_dir)
else: else:
@ -118,14 +83,10 @@ if __name__ == "__main__":
img_generator = GenerateImages( img_generator = GenerateImages(
source_file=Path(args.source), source_file=Path(args.source),
encode_file=Path(args.encode), encode_file=Path(args.encode),
frames=args.frames,
image_dir=image_dir, image_dir=image_dir,
indexer=args.indexer, indexer=args.indexer,
img_lib=args.img_lib, index_directory=args.index_dir,
source_index_path=args.source_index_path,
encode_index_path=args.encode_index_path,
sub_size=args.sub_size, sub_size=args.sub_size,
sub_alignment=args.sub_alignment,
left_crop=args.left_crop, left_crop=args.left_crop,
right_crop=args.right_crop, right_crop=args.right_crop,
top_crop=args.top_crop, top_crop=args.top_crop,
@ -142,18 +103,8 @@ if __name__ == "__main__":
subtitle_color=args.subtitle_color, subtitle_color=args.subtitle_color,
release_sub_title=args.release_sub_title, release_sub_title=args.release_sub_title,
) )
except Exception as init_error:
exit_application(f"Initiation Error: {init_error}", 1)
try:
img_gen = img_generator.process_images() img_gen = img_generator.process_images()
if img_gen: if img_gen:
exit_application(f"\nOutput: {img_gen}", 0) exit_application(f"Output: {img_gen}", 0)
except FrameForgeError as ff_error: except FrameForgeError as error:
img_generator.clean_temp(False) exit_application(error, 1)
exit_application(str(ff_error), 1)
except Exception as except_error:
img_generator.clean_temp(False)
exit_application(f"Unhandled Exception: {except_error}", 1)
finally:
img_generator.clean_temp(False)

@ -1,14 +1,14 @@
import re import re
import shutil import shutil
import tempfile
from random import choice from random import choice
from pathlib import Path from pathlib import Path
from typing import Tuple from typing import Tuple
from numpy import linspace from numpy import linspace
from unidecode import unidecode
import awsmfunc
import vapoursynth as vs import vapoursynth as vs
from awsmfunc import ScreenGenEncoder, ScreenGen, FrameInfo, DynamicTonemap
from frame_forge.exceptions import FrameForgeError from frame_forge.exceptions import FrameForgeError
from frame_forge.font_scaler import FontScaler
from frame_forge.utils import get_working_dir, hex_to_bgr from frame_forge.utils import get_working_dir, hex_to_bgr
@ -17,14 +17,10 @@ class GenerateImages:
self, self,
source_file: Path, source_file: Path,
encode_file: Path, encode_file: Path,
frames: str,
image_dir: Path, image_dir: Path,
indexer: str, indexer: str,
img_lib: str, index_directory: None | str,
source_index_path: None | str,
encode_index_path: None | str,
sub_size: int, sub_size: int,
sub_alignment: int,
left_crop: int, left_crop: int,
right_crop: int, right_crop: int,
top_crop: int, top_crop: int,
@ -43,15 +39,11 @@ class GenerateImages:
self.source_node = None self.source_node = None
self.reference_source_file = None self.reference_source_file = None
self.encode_file = encode_file self.encode_file = encode_file
self.frames = frames
self.encode_node = None self.encode_node = None
self.image_dir = image_dir self.image_dir = image_dir
self.indexer = indexer self.indexer = indexer
self.img_lib = ScreenGenEncoder(img_lib) self.index_dir = index_directory
self.source_index_path = source_index_path
self.encode_index_path = encode_index_path
self.sub_size = sub_size self.sub_size = sub_size
self.sub_alignment = sub_alignment
self.left_crop = left_crop self.left_crop = left_crop
self.right_crop = right_crop self.right_crop = right_crop
self.top_crop = top_crop self.top_crop = top_crop
@ -69,11 +61,7 @@ class GenerateImages:
self.core = vs.core self.core = vs.core
self.load_plugins() self.load_plugins()
self.temp_dir: Path = None def process_images(self):
def process_images(self) -> Path:
self.check_index_paths()
if self.indexer == "lsmash": if self.indexer == "lsmash":
self.index_lsmash() self.index_lsmash()
@ -83,42 +71,29 @@ class GenerateImages:
num_source_frames = len(self.source_node) num_source_frames = len(self.source_node)
num_encode_frames = len(self.encode_node) num_encode_frames = len(self.encode_node)
# ASS subtitle styles # Format: Name, Fontname, Font-size, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic,
# Font Name, Font Size, Primary Color, Secondary Color, Outline Color, Back Color, Bold, # Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL,
# Italic, Underline, Strikeout, Scale X, Scale Y, Spacing, Angle, Border Style, Outline Width, # MarginR, MarginV,
# Shadow Depth, Alignment, Left Margin, Right Margin, Vertical Margin, Encoding
# bgr color # bgr color
color = "&H14FF39" color = "&H000ac7f5"
if self.subtitle_color: if self.subtitle_color:
color = hex_to_bgr(self.subtitle_color) color = hex_to_bgr(self.subtitle_color)
selected_sub_style = ( selected_sub_style = (
f"Segoe UI,{self.sub_size},{color},&H00000000,&H00000000,&H00000000," f"Segoe UI,{self.sub_size},{color},&H00000000,&H00000000,&H00000000,"
f"1,0,0,0,100,100,0,0,1,1,0,{self.sub_alignment},10,10,10,1" "1,0,0,0,100,100,0,0,1,1,0,7,5,0,0,1"
) )
sync_sub_base = (
"Segoe UI,{size},&H31FF31&,&H00000000,&H00000000,&H00000000," selected_sub_style_ref, selected_sub_style_sync = self.sync_font_scaling(
"1,0,0,0,100,100,0,0,1,1,0,{pos},10,10,10,1" num_source_frames=num_source_frames, scaling_factor=1.35
)
selected_sub_style_ref = sync_sub_base.format(
size=str(self.sub_size + 5), pos="7"
)
selected_sub_style_sync = sync_sub_base.format(
size=str(self.sub_size + 5), pos="9"
) )
self.check_de_interlaced(num_source_frames, num_encode_frames) self.check_de_interlaced(num_source_frames, num_encode_frames)
b_frames = None b_frames = self.get_b_frames(num_source_frames)
if not self.frames:
b_frames = self.get_b_frames(num_source_frames)
( screenshot_comparison_dir, screenshot_sync_dir = self.generate_folders()
temp_screenshot_comparison_dir,
temp_selected_dir,
temp_screenshot_sync_dir,
) = self.generate_temp_folders()
self.handle_crop() self.handle_crop()
@ -128,28 +103,17 @@ class GenerateImages:
vs_source_info, vs_encode_info = self.handle_subtitles(selected_sub_style) vs_source_info, vs_encode_info = self.handle_subtitles(selected_sub_style)
if not self.frames: img_job = self.generate_screens(
self.generate_screens( b_frames,
b_frames, vs_source_info,
vs_source_info, vs_encode_info,
vs_encode_info, screenshot_comparison_dir,
temp_screenshot_comparison_dir, screenshot_sync_dir,
temp_screenshot_sync_dir, selected_sub_style_ref,
selected_sub_style_ref, selected_sub_style_sync,
selected_sub_style_sync, )
)
else:
self.generate_exact_screens(
vs_source_info,
vs_encode_info,
temp_screenshot_comparison_dir,
)
final_folder = self.generate_final_folder() return img_job
self.move_images(temp_screenshot_comparison_dir.parent, final_folder)
self.clean_temp()
return final_folder
@staticmethod @staticmethod
def screen_gen_callback(sg_call_back): def screen_gen_callback(sg_call_back):
@ -158,6 +122,29 @@ class GenerateImages:
flush=True, flush=True,
) )
def sync_font_scaling(
self, num_source_frames, scaling_factor: float
) -> Tuple[str, str]:
calculate_str_len = max(
len("frame: reference"), len(str(f"frame: {num_source_frames}"))
)
scale_position = FontScaler().get_adjusted_scale(
self.sub_size + 2, scaling_factor
)
calculate_right_subs = int(
self.source_node.width
- ((calculate_str_len + self.sub_size + 2) * scale_position)
)
selected_sub_style_ref = (
f"Segoe UI,{self.sub_size + 2},&H31FF31&,&H00000000,&H00000000,&H00000000,"
f"1,0,0,0,100,100,0,0,1,1,0,7,{calculate_right_subs},0,0,1"
)
selected_sub_style_sync = (
f"Segoe UI,{self.sub_size + 2},&H31FF31&,&H00000000,&H00000000,&H00000000,"
f"1,0,0,0,100,100,0,0,1,1,0,7,{calculate_right_subs},0,0,1"
)
return selected_sub_style_ref, selected_sub_style_sync
def generate_ref_screens( def generate_ref_screens(
self, selected_sub_style_ref, frames: list, screenshot_sync_dir self, selected_sub_style_ref, frames: list, screenshot_sync_dir
): ):
@ -168,14 +155,13 @@ class GenerateImages:
text=f"Reference\nFrame: {ref_frame}", text=f"Reference\nFrame: {ref_frame}",
style=selected_sub_style_ref, style=selected_sub_style_ref,
) )
ScreenGen( awsmfunc.ScreenGen(
vs_encode_ref_info, vs_encode_ref_info,
frame_numbers=[ref_frame], frame_numbers=[ref_frame],
fpng_compression=1, fpng_compression=1,
folder=screenshot_sync_dir, folder=screenshot_sync_dir,
suffix="b_encode__%d", suffix="b_encode__%d",
callback=self.screen_gen_callback, callback=self.screen_gen_callback,
encoder=self.img_lib,
) )
def generate_sync_screens( def generate_sync_screens(
@ -188,49 +174,15 @@ class GenerateImages:
text=f"Sync\nFrame: {sync_frame}", text=f"Sync\nFrame: {sync_frame}",
style=selected_sub_style_sync, style=selected_sub_style_sync,
) )
ScreenGen( awsmfunc.ScreenGen(
vs_sync_info, vs_sync_info,
frame_numbers=[sync_frame], frame_numbers=[sync_frame],
fpng_compression=1, fpng_compression=1,
folder=Path(screenshot_sync_dir), folder=Path(screenshot_sync_dir),
suffix="a_source__%d", suffix="a_source__%d",
callback=self.screen_gen_callback, callback=self.screen_gen_callback,
encoder=self.img_lib,
) )
def generate_exact_screens(
self,
vs_source_info,
vs_encode_info,
screenshot_comparison_dir,
) -> Path:
print("\nGenerating screenshots, please wait", flush=True)
# generate source images
ScreenGen(
vs_source_info,
frame_numbers=self.frames,
fpng_compression=1,
folder=screenshot_comparison_dir,
suffix="a_source__%d",
callback=self.screen_gen_callback,
encoder=self.img_lib,
)
# generate encode images
ScreenGen(
vs_encode_info,
frame_numbers=self.frames,
fpng_compression=1,
folder=screenshot_comparison_dir,
suffix="b_encode__%d",
callback=self.screen_gen_callback,
encoder=self.img_lib,
)
print("Screen generation completed", flush=True)
return screenshot_comparison_dir
def generate_screens( def generate_screens(
self, self,
b_frames, b_frames,
@ -240,7 +192,7 @@ class GenerateImages:
screenshot_sync_dir, screenshot_sync_dir,
selected_sub_style_ref, selected_sub_style_ref,
selected_sub_style_sync, selected_sub_style_sync,
) -> Path: ) -> str:
print("\nGenerating screenshots, please wait", flush=True) print("\nGenerating screenshots, please wait", flush=True)
# handle re_sync if needed # handle re_sync if needed
@ -257,46 +209,41 @@ class GenerateImages:
sync_frames = b_frames sync_frames = b_frames
# generate source images # generate source images
ScreenGen( awsmfunc.ScreenGen(
vs_source_info, vs_source_info,
frame_numbers=sync_frames, frame_numbers=sync_frames,
fpng_compression=1, fpng_compression=1,
folder=screenshot_comparison_dir, folder=screenshot_comparison_dir,
suffix="a_source__%d", suffix="a_source__%d",
callback=self.screen_gen_callback, callback=self.screen_gen_callback,
encoder=self.img_lib,
) )
# generate encode images # generate encode images
ScreenGen( awsmfunc.ScreenGen(
vs_encode_info, vs_encode_info,
frame_numbers=b_frames, frame_numbers=b_frames,
fpng_compression=1, fpng_compression=1,
folder=screenshot_comparison_dir, folder=screenshot_comparison_dir,
suffix="b_encode__%d", suffix="b_encode__%d",
callback=self.screen_gen_callback, callback=self.screen_gen_callback,
encoder=self.img_lib,
) )
# generate some sync frames # generate some sync frames
print("\nGenerating a few sync frames", flush=True) print("\nGenerating a few sync frames", flush=True)
# select two frames randomly from list # select two frames randomly from list
get_sync_1 = choice(b_frames) sync_1 = choice(b_frames)
remove_sync1 = b_frames.copy() remove_sync1 = b_frames.copy()
remove_sync1.remove(get_sync_1) remove_sync1.remove(sync_1)
get_sync_2 = choice(remove_sync1) sync_2 = choice(remove_sync1)
# sync list
ref_sync_list = sorted([get_sync_1, get_sync_2])
# reference subs # reference subs
self.generate_ref_screens( self.generate_ref_screens(
selected_sub_style_ref, ref_sync_list, screenshot_sync_dir selected_sub_style_ref, [sync_1, sync_2], screenshot_sync_dir
) )
# sync subs 1 # sync subs 1
sync_subs_1 = [ref_sync_list[0] + i for i in range(-5, 6)] sync_subs_1 = [sync_1 + i for i in range(-5, 6)]
self.generate_sync_screens( self.generate_sync_screens(
sync_subs_1, sync_subs_1,
@ -305,7 +252,7 @@ class GenerateImages:
) )
# sync subs 2 # sync subs 2
sync_subs_2 = [ref_sync_list[1] + i for i in range(-5, 6)] sync_subs_2 = [sync_2 + i for i in range(-5, 6)]
self.generate_sync_screens( self.generate_sync_screens(
sync_subs_2, sync_subs_2,
@ -314,13 +261,13 @@ class GenerateImages:
) )
print("Screen generation completed", flush=True) print("Screen generation completed", flush=True)
return screenshot_comparison_dir return str(screenshot_comparison_dir)
def handle_subtitles(self, selected_sub_style): def handle_subtitles(self, selected_sub_style):
vs_source_info = self.core.sub.Subtitle( vs_source_info = self.core.sub.Subtitle(
clip=self.source_node, text="Source", style=selected_sub_style clip=self.source_node, text="Source", style=selected_sub_style
) )
vs_encode_info = FrameInfo( vs_encode_info = awsmfunc.FrameInfo(
clip=self.encode_node, clip=self.encode_node,
title=self.release_sub_title if self.release_sub_title else "", title=self.release_sub_title if self.release_sub_title else "",
style=selected_sub_style, style=selected_sub_style,
@ -330,8 +277,10 @@ class GenerateImages:
def handle_hdr(self): def handle_hdr(self):
if self.tone_map: if self.tone_map:
self.source_node = DynamicTonemap(clip=self.source_node, libplacebo=False) self.source_node = awsmfunc.DynamicTonemap(
self.encode_node = DynamicTonemap( clip=self.source_node, libplacebo=False
)
self.encode_node = awsmfunc.DynamicTonemap(
clip=self.encode_node, clip=self.encode_node,
reference=self.reference_source_file, reference=self.reference_source_file,
libplacebo=False, libplacebo=False,
@ -388,8 +337,8 @@ class GenerateImages:
bottom=self.bottom_crop if self.bottom_crop else 0, bottom=self.bottom_crop if self.bottom_crop else 0,
) )
def generate_final_folder(self) -> Path: def generate_folders(self):
print("\nCreating final output folder", flush=True) print("\nCreating folders for images", flush=True)
if self.image_dir: if self.image_dir:
image_output_dir = Path(self.image_dir) image_output_dir = Path(self.image_dir)
else: else:
@ -397,58 +346,34 @@ class GenerateImages:
Path(self.encode_file).parent / f"{Path(self.encode_file).stem}_images" Path(self.encode_file).parent / f"{Path(self.encode_file).stem}_images"
) )
# remove any accent characters from path
image_output_dir = Path(unidecode(str(image_output_dir)))
# check if temp image dir exists, if so delete it!
if image_output_dir.exists(): if image_output_dir.exists():
shutil.rmtree(image_output_dir, ignore_errors=True) shutil.rmtree(image_output_dir, ignore_errors=True)
# create main image dir
image_output_dir.mkdir(exist_ok=True, parents=True) image_output_dir.mkdir(exist_ok=True, parents=True)
print("Folder creation completed", flush=True) # create comparison image directory and define it as variable
Path(Path(image_output_dir) / "img_comparison").mkdir(exist_ok=True)
screenshot_comparison_dir = str(Path(Path(image_output_dir) / "img_comparison"))
return image_output_dir # create selected image directory and define it as variable
Path(Path(image_output_dir) / "img_selected").mkdir(exist_ok=True)
def generate_temp_folders(self) -> Tuple[Path, Path, Path]: # create sync image directory and define it as variable
print("\nCreating temporary folders for images", flush=True) Path(Path(image_output_dir) / "img_sync").mkdir(exist_ok=True)
self.temp_dir = Path(tempfile.mkdtemp(prefix="ff_")) screenshot_sync_dir = str(Path(Path(image_output_dir) / "img_sync"))
screenshot_comparison_dir = Path(Path(self.temp_dir) / "img_comparison") # create sub directories
screenshot_comparison_dir.mkdir(exist_ok=True) Path(Path(image_output_dir) / "img_sync/sync1").mkdir(exist_ok=True)
Path(Path(image_output_dir) / "img_sync/sync2").mkdir(exist_ok=True)
selected_dir = Path(Path(self.temp_dir) / "img_selected")
selected_dir.mkdir(exist_ok=True)
screenshot_sync_dir = Path(Path(self.temp_dir) / "img_sync")
screenshot_sync_dir.mkdir(exist_ok=True)
Path(screenshot_sync_dir / "sync1").mkdir(exist_ok=True)
Path(screenshot_sync_dir / "sync2").mkdir(exist_ok=True)
print("Folder creation completed", flush=True) print("Folder creation completed", flush=True)
return screenshot_comparison_dir, selected_dir, screenshot_sync_dir return screenshot_comparison_dir, screenshot_sync_dir
def move_images(self, temp_folder: Path, output_folder: Path) -> None:
print("\nMoving generated images")
for sub_folder in temp_folder.iterdir():
if sub_folder.is_dir():
target_sub_folder = output_folder / sub_folder.name
target_sub_folder.mkdir(parents=True, exist_ok=True)
for item in sub_folder.iterdir():
target_item = target_sub_folder / item.name
if item.is_dir():
shutil.move(item, target_item)
else:
shutil.move(item, target_sub_folder)
print("Image move completed", flush=True)
def clean_temp(self, status: bool = True) -> None:
if status:
print("\nRemoving temp folder")
shutil.rmtree(self.temp_dir, ignore_errors=True)
if status:
print("Temp folder removal completed")
def get_b_frames(self, num_source_frames): def get_b_frames(self, num_source_frames):
print( print(
@ -465,11 +390,9 @@ class GenerateImages:
) )
try: try:
pict_types = ("B", b"B")
for i, frame in enumerate(b_frames): for i, frame in enumerate(b_frames):
while ( while (
self.encode_node.get_frame(frame).props["_PictType"] self.encode_node.get_frame(frame).props["_PictType"].decode() != "B"
not in pict_types
): ):
frame += 1 frame += 1
b_frames[i] = frame b_frames[i] = frame
@ -528,9 +451,10 @@ class GenerateImages:
flush=True, flush=True,
) )
def _index_source_lsmash(self): def index_lsmash(self):
print("Indexing source", flush=True) print("Indexing source", flush=True)
# index source file
# if index is found in the StaxRip temp working directory, attempt to use it # if index is found in the StaxRip temp working directory, attempt to use it
if ( if (
Path(str(Path(self.source_file).with_suffix("")) + "_temp/").is_dir() Path(str(Path(self.source_file).with_suffix("")) + "_temp/").is_dir()
@ -540,134 +464,152 @@ class GenerateImages:
): ):
print("Index found in StaxRip temp, attempting to use", flush=True) print("Index found in StaxRip temp, attempting to use", flush=True)
# define cache path
lwi_cache_path = Path( lwi_cache_path = Path(
str(Path(self.source_file).with_suffix("")) + "_temp/temp.lwi" str(Path(self.source_file).with_suffix("")) + "_temp/temp.lwi"
) )
elif self.source_index_path.exists(): # try to use index on source file with the cache path
print("Index found, attempting to use", flush=True) try:
lwi_cache_path = self.source_index_path self.source_node = self.core.lsmas.LWLibavSource(
source=self.source_file, cachefile=lwi_cache_path
)
self.reference_source_file = self.core.lsmas.LWLibavSource(
source=self.source_file, cachefile=lwi_cache_path
)
print("Using existing index", flush=True)
# if index cannot be used
except vs.Error:
print("L-Smash version miss-match, indexing source again", flush=True)
# index source file
self.source_node = self.core.lsmas.LWLibavSource(self.source_file)
self.reference_source_file = self.core.lsmas.LWLibavSource(
self.source_file
)
# if no existing index is found index source file # if no existing index is found index source file
else: else:
lwi_cache_path = Path(Path(self.source_file).with_suffix(".lwi")) cache_path = Path(Path(self.source_file).with_suffix(".lwi"))
try:
# create index
self.source_node = self.core.lsmas.LWLibavSource(
self.source_file, cachefile=cache_path
)
self.reference_source_file = self.core.lsmas.LWLibavSource(
self.source_file, cachefile=cache_path
)
except vs.Error:
# delete index
Path(self.source_file).with_suffix(".lwi").unlink(missing_ok=True)
# create index
self.source_node = self.core.lsmas.LWLibavSource(
self.source_file, cachefile=cache_path
)
self.reference_source_file = self.core.lsmas.LWLibavSource(
self.source_file, cachefile=cache_path
)
try: print("Source index completed\n\nIndexing encode", flush=True)
self.source_node = self.core.lsmas.LWLibavSource(
source=self.source_file, cachefile=lwi_cache_path
)
self.reference_source_file = self.core.lsmas.LWLibavSource(
source=self.source_file, cachefile=lwi_cache_path
)
print("Using existing index", flush=True)
except vs.Error:
print("L-Smash version miss-match, indexing source again", flush=True)
self.source_node = self.core.lsmas.LWLibavSource(self.source_file)
self.reference_source_file = self.core.lsmas.LWLibavSource(self.source_file)
print("Source index completed", flush=True) # define a path for encode index to go
if self.index_dir:
def _index_encode_lsmash(self): index_base_path = Path(self.index_dir) / Path(self.encode_file).name
print("\nIndexing encode", flush=True) cache_path_enc = index_base_path.with_suffix(".lwi")
if self.encode_index_path:
cache_path_enc = self.encode_index_path
else: else:
cache_path_enc = Path(Path(self.encode_file).with_suffix(".lwi")) cache_path_enc = Path(Path(self.encode_file).with_suffix(".lwi"))
try: try:
# create index
self.encode_node = self.core.lsmas.LWLibavSource( self.encode_node = self.core.lsmas.LWLibavSource(
self.encode_file, cachefile=cache_path_enc self.encode_file, cachefile=cache_path_enc
) )
except vs.Error: except vs.Error:
# delete index
cache_path_enc.unlink(missing_ok=True) cache_path_enc.unlink(missing_ok=True)
# create index
self.encode_node = self.core.lsmas.LWLibavSource( self.encode_node = self.core.lsmas.LWLibavSource(
self.encode_file, cachefile=cache_path_enc self.encode_file, cachefile=cache_path_enc
) )
print("Encode index completed", flush=True) print("Encode index completed", flush=True)
def index_lsmash(self):
"""Index source/encode with lsmash"""
self._index_source_lsmash()
self._index_encode_lsmash()
def _index_source_ffms2(self):
print("Indexing source", flush=True)
# if index is found in the StaxRip temp working directory, attempt to use it
if (
Path(str(Path(self.source_file).with_suffix("")) + "_temp/").is_dir()
and Path(
str(Path(self.source_file).with_suffix("")) + "_temp/temp.ffindex"
).is_file()
):
print("Index found in StaxRip temp, attempting to use", flush=True)
ffindex_cache_path = Path(
str(Path(self.source_file).with_suffix("")) + "_temp/temp.ffindex"
)
elif self.source_index_path.exists():
print("Index found, attempting to use", flush=True)
ffindex_cache_path = self.source_index_path
# if no existing index is found index source file
else:
ffindex_cache_path = Path(Path(self.source_file).with_suffix(".ffindex"))
print(
"FFMS2 library doesn't allow progress, please wait while the index is completed",
flush=True,
)
try:
self.source_node = self.core.ffms2.Source(
self.source_file, cachefile=ffindex_cache_path
)
self.reference_source_file = self.core.ffms2.Source(
self.source_file, cachefile=ffindex_cache_path
)
except vs.Error:
Path(self.source_file).with_suffix(".ffindex").unlink(missing_ok=True)
print(
"FFMS2 library doesn't allow progress, please wait while the index is completed",
flush=True,
)
self.source_node = self.core.ffms2.Source(
self.source_file, cachefile=ffindex_cache_path
)
self.reference_source_file = self.core.ffms2.Source(
self.source_file, cachefile=ffindex_cache_path
)
print("Source index completed", flush=True)
def _index_encode_ffms2(self):
print("\nIndexing encode", flush=True)
if self.encode_index_path:
cache_path_enc = self.encode_index_path
else:
cache_path_enc = Path(str(self.encode_file) + ".ffindex")
try:
self.encode_node = self.core.ffms2.Source(
self.encode_file, cachefile=cache_path_enc
)
except vs.Error:
cache_path_enc.unlink(missing_ok=True)
self.encode_node = self.core.ffms2.Source(
self.encode_file, cachefile=cache_path_enc
)
print("Encode index completed", flush=True)
def index_ffms2(self): def index_ffms2(self):
"""Index source/encode with ffms2""" print("Indexing source", flush=True)
self._index_source_ffms2() # index source file
self._index_encode_ffms2() # if index is found in the StaxRip temp working directory, attempt to use it
if (
Path(str(Path(self.source_file).with_suffix("")) + "_temp/").is_dir()
and Path(
str(Path(self.source_file).with_suffix("")) + "_temp/temp.ffindex"
).is_file()
):
print("Index found in StaxRip temp, attempting to use", flush=True)
# define cache path
ffindex_cache_path = Path(
str(Path(self.source_file).with_suffix("")) + "_temp/temp.ffindex"
)
# try to use index on source file with the cache path
try:
self.source_node = self.core.ffms2.Source(
source=self.source_file, cachefile=ffindex_cache_path
)
self.reference_source_file = self.core.ffms2.Source(
source=self.source_file, cachefile=ffindex_cache_path
)
print("Using existing index", flush=True)
# if index cannot be used
except vs.Error:
print("FFMS2 version miss-match, indexing source again", flush=True)
# index source file
self.source_node = self.core.ffms2.Source(self.source_file)
self.reference_source_file = self.core.ffms2.Source(self.source_file)
# if no existing index is found index source file
else:
try:
# create index
print(
"FFMS2 library doesn't allow progress, please wait while the index is completed",
flush=True,
)
self.source_node = self.core.ffms2.Source(self.source_file)
self.reference_source_file = self.core.ffms2.Source(self.source_file)
except vs.Error:
# delete index
Path(self.source_file).with_suffix(".ffindex").unlink(missing_ok=True)
# create index
print(
"FFMS2 library doesn't allow progress, please wait while the index is completed",
flush=True,
)
self.source_node = self.core.ffms2.Source(self.source_file)
self.reference_source_file = self.core.ffms2.Source(self.source_file)
print("Source index completed\n\nIndexing encode", flush=True)
# define a path for encode index to go
if self.index_dir:
index_base_path = Path(self.index_dir) / Path(self.encode_file).name
cache_path_enc = Path(str(index_base_path) + ".ffindex")
else:
cache_path_enc = Path(self.encode_file + ".ffindex")
try:
self.encode_node = self.core.ffms2.Source(
self.encode_file, cachefile=cache_path_enc
)
except vs.Error:
cache_path_enc.unlink(missing_ok=True)
self.encode_node = self.core.ffms2.Source(
self.encode_file, cachefile=cache_path_enc
)
print("Encode index completed", flush=True)
def load_plugins(self): def load_plugins(self):
plugin_path = get_working_dir() / "img_plugins" plugin_path = get_working_dir() / "img_plugins"
@ -676,21 +618,3 @@ class GenerateImages:
else: else:
for plugin in plugin_path.glob("*.dll"): for plugin in plugin_path.glob("*.dll"):
self.core.std.LoadPlugin(Path(plugin).resolve()) self.core.std.LoadPlugin(Path(plugin).resolve())
def check_index_paths(self):
indexer_ext = ".lwi" if self.indexer == "lsmash" else ".ffindex"
if not self.source_index_path or not Path(self.source_index_path).exists():
source_path_obj = Path(self.source_file)
self.source_index_path = source_path_obj.parent / Path(
f"{source_path_obj.stem}{indexer_ext}"
)
else:
self.source_index_path = Path(self.source_index_path)
if not self.encode_index_path or not Path(self.encode_index_path).exists():
encode_path_obj = Path(self.source_file)
self.encode_index_path = encode_path_obj.parent / Path(
f"{encode_path_obj.stem}{indexer_ext}"
)
else:
self.encode_index_path = Path(self.encode_index_path)

@ -1,7 +0,0 @@
import re
def frame_list(frames: str) -> list:
if not re.match(r"\d+(?::\d+)*$", frames):
raise ValueError("Input must be in the format of int:int i.e. 101:104")
return [int(x) for x in frames.split(":")]

@ -0,0 +1,25 @@
import math
class FontScaler:
def __init__(self, original_font_size=100, original_scale=3.5):
self.original_font_size = original_font_size
self.original_scale = original_scale
def calculate_scale_factor(self, desired_font_size):
"""Calculate the scale factor based on the desired font size."""
return math.log(desired_font_size) / math.log(self.original_font_size)
def adjust_scale_factor(self, scale_factor, multiplier):
"""Adjust the scale factor with a multiplier."""
return scale_factor * multiplier
def scale_font(self, scale_factor):
"""Scale the font size based on the original scale and scale factor."""
return self.original_scale * scale_factor
def get_adjusted_scale(self, desired_font_size, multiplier=1.0):
"""Get the adjusted font scale based on the desired font size and multiplier."""
scale_factor = self.calculate_scale_factor(desired_font_size)
adjusted_scale_factor = self.adjust_scale_factor(scale_factor, multiplier)
return self.scale_font(adjusted_scale_factor)

41
poetry.lock generated

@ -17,8 +17,10 @@ version = "1.3.4"
description = "awesome VapourSynth functions" description = "awesome VapourSynth functions"
optional = false optional = false
python-versions = ">=3.9" python-versions = ">=3.9"
files = [] files = [
develop = false {file = "awsmfunc-1.3.4-py3-none-any.whl", hash = "sha256:d9ce9cf90dfdb66b4561a5d3b011232e663ad0d879e2a276827bff9b8b3b37e1"},
{file = "awsmfunc-1.3.4.tar.gz", hash = "sha256:8330332f5c4818322b4090b24499b1dc4e4e371460de70c4bd62a112f4157255"},
]
[package.dependencies] [package.dependencies]
numpy = "*" numpy = "*"
@ -27,13 +29,7 @@ vs-rekt = ">=1.0.0"
vsutil = ">=0.7.0" vsutil = ">=0.7.0"
[package.extras] [package.extras]
dev = ["ruff", "toml"] dev = ["pylint", "toml", "yapf"]
[package.source]
type = "git"
url = "https://github.com/OpusGang/awsmfunc"
reference = "HEAD"
resolved_reference = "e1290f799162749fc627951290bfb4089f2f39cb"
[[package]] [[package]]
name = "black" name = "black"
@ -282,30 +278,41 @@ docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "pygments
testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.develop (>=7.21)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-ruff", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.develop (>=7.21)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-ruff", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"]
testing-integration = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "packaging (>=23.1)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"] testing-integration = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "packaging (>=23.1)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"]
[[package]]
name = "unidecode"
version = "1.3.7"
description = "ASCII transliterations of Unicode text"
optional = false
python-versions = ">=3.5"
files = [
{file = "Unidecode-1.3.7-py3-none-any.whl", hash = "sha256:663a537f506834ed836af26a81b210d90cbde044c47bfbdc0fbbc9f94c86a6e4"},
{file = "Unidecode-1.3.7.tar.gz", hash = "sha256:3c90b4662aa0de0cb591884b934ead8d2225f1800d8da675a7750cbc3bd94610"},
]
[[package]] [[package]]
name = "vapoursynth" name = "vapoursynth"
version = "65" version = "64"
description = "A frameserver for the 21st century" description = "A frameserver for the 21st century"
optional = false optional = false
python-versions = "*" python-versions = "*"
files = [ files = [
{file = "VapourSynth-65-cp311-cp311-win_amd64.whl", hash = "sha256:9fab72bb6dbcdd8d0e6643da68908f85387bc764b836524e97a1e6e8989ca13e"}, {file = "VapourSynth-64-cp311-cp311-win_amd64.whl", hash = "sha256:ad046a704537276f7ebb4132e1210e8922ec4b2403a4b44f5fe30fd9456a6412"},
{file = "VapourSynth-65-cp38-cp38-win_amd64.whl", hash = "sha256:d9b49b595dc929d63250bd82f05e75238b6dfc4a50ab08e7fc4fe4f8888f6b95"}, {file = "VapourSynth-64-cp38-cp38-win_amd64.whl", hash = "sha256:794f93bcaa1ce79510c11962f677a7d18685a0c29aa36586cb307d1eb9d7f2e0"},
{file = "VapourSynth-65.zip", hash = "sha256:1d42d461ef9988a3477134e478a2291d79f3469635cde8af2c66b1e87c36f711"}, {file = "VapourSynth-64.zip", hash = "sha256:29425a135ca68cbb17b3dfcad0097375b75f94af1f499ba89bcd2b03b2651846"},
] ]
[[package]] [[package]]
name = "vapoursynth-portable" name = "vapoursynth-portable"
version = "65" version = "64"
description = "A frameserver for the 21st century" description = "A frameserver for the 21st century"
optional = false optional = false
python-versions = "*" python-versions = "*"
files = [ files = [
{file = "VapourSynth_portable-65-py2.py3-none-win_amd64.whl", hash = "sha256:6a66615d8a25a71766d035054f9980fd8be47dbd97332e9c068933d1b25061b3"}, {file = "VapourSynth_portable-64-py2.py3-none-win_amd64.whl", hash = "sha256:82b26b38774197a7ef53cbd3206bafb02d197100fad513d635bf83f9981dad6c"},
] ]
[package.dependencies] [package.dependencies]
vapoursynth = "65" vapoursynth = "64"
[[package]] [[package]]
name = "vs-rekt" name = "vs-rekt"
@ -339,4 +346,4 @@ vapoursynth = "*"
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "3.11.5" python-versions = "3.11.5"
content-hash = "d8f86839d70dbe0a58d107ebf4bb36e7296975e8da5f6bfd366566cbe15b89e6" content-hash = "820bfb62c5fba1b17221bc1a02144932dbce64fa44c698f24f7205c2fc4a3d18"

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "frame-forge" name = "frame-forge"
version = "1.0.3" version = "1.0.1"
description = "CLI to offload image generation to" description = "CLI to offload image generation to"
authors = ["jlw4049 <jlw_4049@hotmail.com>"] authors = ["jlw4049 <jlw_4049@hotmail.com>"]
license = "MIT" license = "MIT"
@ -8,9 +8,10 @@ readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "3.11.5" python = "3.11.5"
awsmfunc = "^1.3.4"
unidecode = "^1.3.7"
vapoursynth-portable = "64"
numpy = "^1.26.2" numpy = "^1.26.2"
vapoursynth-portable = "65"
awsmfunc = {git = "https://github.com/OpusGang/awsmfunc"}
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]