🔎 upscaler#
This module provides utilities for upscaling images and videos using waifu2x-ncnn-vulkan (by default).
It includes functions to upscale individual images, batches of images in a folder, and videos by processing them frame by frame. It also handles configuration and installation of required dependencies.
Example of script:
# Imports
import stouputils.applications.upscaler as app
from stouputils.io.path import get_root_path
# Constants
ROOT: str = get_root_path(__file__) + "/upscaler"
INPUT_FOLDER: str = f"{ROOT}/input"
PROGRESS_FOLDER: str = f"{ROOT}/progress"
OUTPUT_FOLDER: str = f"{ROOT}/output"
# Main
if __name__ == "__main__":
app.video_upscaler_cli(INPUT_FOLDER, PROGRESS_FOLDER, OUTPUT_FOLDER)
config#
This module provides configuration settings and constants for the upscaler application.
It defines the URLs for downloading dependencies like waifu2x-ncnn-vulkan and FFmpeg, and provides a Config class with settings for upscaling operations, including output quality, bitrates, executable paths, and command-line arguments for the underlying tools.
Configuration options include: - Image quality settings (JPG quality) - Video encoding parameters (bitrate, codec, etc.) - Paths to external binaries (FFmpeg, waifu2x-ncnn-vulkan) - Command-line arguments for upscaling and video processing
- WAIFU2X_NCNN_VULKAN_RELEASES: dict[str, str] = {'Darwin': 'https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-macos.zip', 'Linux': 'https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-ubuntu.zip', 'Windows': 'https://github.com/nihui/waifu2x-ncnn-vulkan/releases/download/20220728/waifu2x-ncnn-vulkan-20220728-windows.zip'}[source]#
URLs to download waifu2x-ncnn-vulkan from for each common platform.
- FFMPEG_RELEASES: dict[str, str] = {'Darwin': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-macos64-gpl.tar.xz', 'Linux': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz', 'Windows': 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip'}[source]#
URLs to download FFmpeg from for each common platform.
- YOUTUBE_BITRATE_RECOMMENDATIONS: dict[str, dict[str, dict[int, int]]] = {'HDR': {'high': {0: 9500, 1080: 15000, 1440: 30000, 2160: 85000, 4320: 300000}, 'standard': {0: 6500, 1080: 10000, 1440: 20000, 2160: 56000, 4320: 200000}}, 'SDR': {'high': {0: 1500, 480: 4000, 720: 7500, 1080: 12000, 1440: 24000, 2160: 68000, 4320: 240000}, 'standard': {0: 1000, 480: 2500, 720: 5000, 1080: 8000, 1440: 16000, 2160: 45000, 4320: 160000}}}[source]#
YouTube bitrate recommendations for different resolutions and frame rates.
This dictionary contains recommended bitrates for YouTube uploads based on: - SDR vs HDR content - Standard frame rate (24, 25, 30 fps) vs high frame rate (48, 50, 60 fps) - Video resolution (from 360p up to 8K)
The values are in kbps (kilobits per second).
- class Config[source]#
Bases:
objectConfiguration class for the upscaler.
- VIDEO_FINAL_BITRATE: int = -1[source]#
Video final bitrate for the output video. -1 for YouTube recommended bitrate based on the video resolution.
- FFMPEG_EXECUTABLE: str = 'ffmpeg'[source]#
Path to the ffmpeg executable, default is “ffmpeg” in the PATH.
- FFMPEG_ARGS: tuple[str, ...] = ('-c:a', 'copy', '-c:v', 'hevc_nvenc', '-map', '0:v:0', '-map', '1:a:0?', '-preset', 'slow', '-y')[source]#
Additional arguments sent to the ffmpeg executable when calling subprocess.run().
- FFPROBE_EXECUTABLE: str = 'ffprobe'[source]#
Path to the ffprobe executable, default is “ffprobe” in the PATH. Used to get the framerate of the video.
- FFMPEG_CHECK_HELP_TEXT: str = 'usage: ffmpeg [options]'[source]#
If this text is present in the output of the ffmpeg executable, it means it’s installed correctly.
- UPSCALER_EXECUTABLE: str = 'waifu2x-ncnn-vulkan'[source]#
Path to the upscaler executable, default is “waifu2x-ncnn-vulkan” in the PATH.
- UPSCALER_ARGS: tuple[str, ...] = ('-i', 'INPUT_PATH', '-o', 'OUTPUT_PATH', '-s', 'UPSCALE_RATIO', '-n', '3')[source]#
Arguments sent to the upscaler executable when calling subprocess.run().
- UPSCALER_EXECUTABLE_HELP_TEXT: str = 'Usage: waifu2x-ncnn-vulkan -i'[source]#
If this text is present in the output of the upscaler executable, it means it’s installed correctly.
- SLIGHTLY_FASTER_MODE: bool = False[source]#
If True, the upscaler executable will be called once, which is slightly faster but you can’t see the progress.
image#
This module provides utility functions for upscaling images using waifu2x-ncnn-vulkan.
It includes functions to upscale individual images, batches of images in a folder, and handle intermediate operations like converting between image formats. The module also manages temporary directories for partial processing and tracks progress of batch operations.
Main functionalities:
Converting frames between image formats (PNG to JPG)
Upscaling individual images with configurable upscale ratio
Batch processing folders of images with progress tracking
Handling already processed images to resume interrupted operations
Example usage:
from stouputils.applications.upscaler import upscale, upscale_folder
# Upscale a single image
upscale("input.jpg", "output.jpg", 2)
# Upscale a folder of images
upscale_folder("input_folder", "output_folder", 2)
- convert_frame(
- frame_path: str,
- delete_png: bool = True,
Convert a PNG frame to JPG format to take less space.
- Parameters:
frame_path – Path to the PNG frame to convert.
delete_png – Whether to delete the original PNG file after conversion.
> convert_frame("input.png", delete_png=True) > # input.png will be converted to input.jpg and the original file will be deleted > convert_frame("input.png", delete_png=False) > # input.png will be converted to input.jpg and the original file will be kept
- get_all_files(
- folder: str,
- suffix: str | tuple[str, ...] = '',
Get all files paths in a folder, with a specific suffix if provided.
- Parameters:
folder – Path to the folder containing the files.
suffix – Suffix of the files to get (e.g. “.png”, “.jpg”, etc.).
- Returns:
List of all files paths in the folder.
>>> files: list[str] = get_all_files("some_folder", ".png") >>> len(files) 0
- create_temp_dir_for_not_upscaled(
- input_path: str,
- output_path: str,
Creates a temporary directory containing only images that haven’t been upscaled yet.
- Parameters:
input_path – Path to the folder containing input images.
output_path – Path to the folder where upscaled images are saved.
- Returns:
- A temporary directory object if there are images to process,
None if all images are already upscaled.
- upscale(
- input_path: str,
- output_path: str,
- upscale_ratio: int,
Upscale an input image (or a directory of images) with the upscaler executable.
- Parameters:
input_path – Path to the image to upscale (or a directory).
output_path – Path to the output image (or a directory).
upscale_ratio – Upscaling ratio.
> upscale("folder", "folder", 2) Traceback (most recent call last): ... AssertionError: Input and output paths cannot be the same, got 'folder' > upscale("stouputils", "stouputils/output.jpg", 2) Traceback (most recent call last): ... AssertionError: If input is a directory, output must be a directory too, got 'stouputils/output.jpg' > upscale("input.jpg", "output.jpg", 2) > # The input.jpg will be upscaled to output.jpg with a ratio of 2 > upscale("input_folder", "output_folder", 2) > # The input_folder will be upscaled to output_folder with a ratio of 2
- upscale_images(
- images: list[str],
- output_folder: str,
- upscale_ratio: int,
- desc: str = 'Upscaling images',
Upscale multiple images from a list.
- Parameters:
images – List of paths to the images to upscale.
output_folder – Path to the output folder where the upscaled images will be saved.
upscale_ratio – Upscaling ratio.
desc – Description of the function execution displayed in the progress bar. No progress bar will be displayed if desc is empty.
- upscale_folder(
- input_folder: str,
- output_folder: str,
- upscale_ratio: int,
- slightly_faster_mode: bool = True,
- desc: str = 'Upscaling folder',
Upscale all images in a folder.
- Parameters:
input_folder – Path to the input folder containing the images to upscale.
output_folder – Path to the output folder where the upscaled images will be saved.
upscale_ratio – Upscaling ratio.
slightly_faster_mode – Whether to use the slightly faster mode (no progress bar), one call to the upscaler executable.
desc – Description of the function execution displayed in the progress bar. No progress bar will be displayed if desc is empty.
video#
This module provides utility functions for upscaling videos using waifu2x-ncnn-vulkan.
It extracts frames from videos, upscales them individually, and then recombines them into a high-quality output video. The process preserves audio from the original video and handles configuration of video encoding parameters like bitrate and framerate.
Main functionalities:
Extracting frames from videos using FFmpeg
Upscaling frames using waifu2x-ncnn-vulkan
Recombining frames into videos with optimized bitrates
Handling partially processed videos to resume interrupted operations
Calculating recommended bitrates based on resolution and framerate
The module includes YouTube’s recommended bitrate settings for different resolutions, framerates, and HDR/SDR content types, ensuring optimal quality for various outputs.
Example usage:
# Imports
import stouputils.applications.upscaler as app
from stouputils.io.path import get_root_path
# Constants
ROOT: str = get_root_path(__file__) + "/upscaler"
INPUT_FOLDER: str = f"{ROOT}/input"
PROGRESS_FOLDER: str = f"{ROOT}/progress"
OUTPUT_FOLDER: str = f"{ROOT}/output"
# Main
if __name__ == "__main__":
app.video_upscaler_cli(INPUT_FOLDER, PROGRESS_FOLDER, OUTPUT_FOLDER)
- get_recommended_bitrate(
- resolution: tuple[int, int],
- frame_rate: int = 60,
- upload_type: Literal['SDR', 'HDR'] = 'SDR',
Get the recommended bitrate (in kbps) for the output video based on the video resolution.
- Parameters:
resolution – Video resolution (width, height).
frame_rate – Frame rate of the video, default is 60.
upload_type – Upload type from which the recommendation is made, default is “SDR”.
- Returns:
The recommended bitrate for the output video (in kbps)
Source: https://support.google.com/youtube/answer/1722171?hl=en#zippy=%2Cbitrate
>>> # Valid examples >>> get_recommended_bitrate((3840, 2160), 60, "SDR") 68000 >>> get_recommended_bitrate((1920, 1080), 60, "HDR") 15000 >>> get_recommended_bitrate((1920, 1080), 60, "SDR") 12000 >>> get_recommended_bitrate((1920, 1080), 30, "SDR") 8000
>>> # Invalid examples >>> get_recommended_bitrate((1920, 1080), 60, "Ratio") Traceback (most recent call last): ... AssertionError: Invalid upload type: 'Ratio' >>> get_recommended_bitrate("1920x1080", 60, "SDR") Traceback (most recent call last): ... AssertionError: Invalid resolution: 1920x1080, must be a tuple of two integers >>> get_recommended_bitrate((1920, 1080), -10, "SDR") Traceback (most recent call last): ... AssertionError: Invalid frame rate: -10, must be a positive integer
- upscale_video(
- video_file: str,
- input_folder: str,
- progress_folder: str,
- output_folder: str,
Handles a video file.
- resolve_upscale_ratio(
- extracted_frame: str,
- upscaled_frame: str | None,
The upscaling ratio of a video: the one its frames already have, else
--upscaleon the command line, else asked on stdin.- Parameters:
upscaled_frame – A frame already upscaled, None when the video starts from scratch.
- probe_framerate(input_path: str, video_file: str) str[source]#
The frame rate of a video’s first stream as ffprobe gives it, such as
"30000/1001", or"60"when it cannot be read.
- video_upscaler_cli(
- input_folder: str,
- progress_folder: str,
- output_folder: str,
Upscales videos from an input folder and saves them to an output folder.
Uses intermediate folders for extracted and upscaled frames within the progress folder. Handles resuming partially processed videos.
- Parameters:
input_folder – Path to the folder containing input videos.
progress_folder – Path to the folder for storing intermediate files (frames).
output_folder – Path to the folder where upscaled videos will be saved.