[Tutorial] How to save space in your Ren'Py game

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
uyjulian
Regular
Posts: 128
Joined: Sun Mar 08, 2015 1:40 pm
Github: uyjulian
Contact:

[Tutorial] How to save space in your Ren'Py game

#1 Post by uyjulian »

These commands will allow you to reduce your game's space up to 90% without any script editing.
Change directory to your game's project directory first.
Please note that the original files will be deleted, so if you want the original files to be kept, copy the original files outside of your game's project directory.
You need GNU coreutils, GNU parallel, Perl, ffmpeg, and webp tools installed.
If you are running Windows, these can be installed from within Windows Subsystem for Linux.

Audio format (change any occurrence of ogg to your audio format):

Code: Select all

find . -name \*.ogg -print0 | parallel -0 --nice 19 ffmpeg -y -i {} -c:a libopus -b:a 64k -f webm {}.mka
find . -name \*.ogg.mka -exec rename -f 's/.ogg.mka$/.ogg/' {} +;
These parameters are good enough for my use; however, if you want to play with the codec parameters, see the ffmpeg documentation.

Video format (change any occurrence of avi to your video format):

Code: Select all

find . -name \*.avi -execdir ffmpeg -y -i {} -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 4 -speed 4 -tile-columns 6 -frame-parallel 1 -an -passlogfile {} -f webm /dev/null \;
find . -name \*.avi -execdir ffmpeg -i {} -c:v libvpx-vp9 -pass 2 -b:v 1000K -threads 4 -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -c:a libopus -b:a 64k -passlogfile {} {}.mkv \;
find . -name \*.log -delete
find . -name \*.avi.mkv -exec rename -f 's/.avi.mkv$/.avi/' {} +;
These parameters are good enough for my use; however, if you want to play with the codec parameters, see the ffmpeg documentation.

Lossless image format (change any occurrence of png to your lossless image format):

Code: Select all

find . -name \*.png -print0 | parallel -0 --nice 19 cwebp {} -lossless -o {}.webp \;
find . -name \*.png.webp -exec rename -f 's/.png.webp$/.png/' {} +;
These parameters are good enough for my use; however, if you want to play with the codec parameters, see the cwebp documentation.

Lossy image format (change any occurrence of jpg to your lossy image format):

Code: Select all

find . -name \*.jpg -print0 | parallel -0 --nice 19 cwebp {} -o {}.jpg \;
find . -name \*.jpg.webp -exec rename -f 's/.jpg.webp$/.jpg/' {} +;
These parameters are good enough for my use; however, if you want to play with the codec parameters, see the cwebp documentation.
Last edited by uyjulian on Thu Jun 11, 2020 12:59 am, edited 1 time in total.

User avatar
uyjulian
Regular
Posts: 128
Joined: Sun Mar 08, 2015 1:40 pm
Github: uyjulian
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#2 Post by uyjulian »

I will release a new tool soon, which will allow you to save space by just pointing the tool to your Ren'Py game directory, which may or may not be archived.
I will add advanced options, each for images/video/audio, to select what quality you want.
Max quality audio will be compressed with libwavpack or the audio will be copied into Matroska container, with all metadata stripped (except gap data for MP3) whichever is smaller; while lowest quality audio will become 6 kbps opus.
Max quality video will be compressed with libvpx-vp9 two-pass encoding lossless or the original stream copied, whatever is smaller, and the audio stream will follow the audio setting above. The video size/bitrate ratio in the middle will be about (1920x1080)/(5000kbps); while lowest quality video size/bitrate ratio will be (1920x1080)/(1000kbps).
I think the AV1 bitstream will be frozen soon, so when AV1 support gets added into ffmpeg and Ren'Py, AV1 codec will be used instead.
Max quality images will be WEBP format with max lossless settings or the original image; low quality images will be WEBP encoded with -q25. Hopefully renpytom will accept a patched sdl_image with support for bpg and still video frames decoded by ffmpeg, but we'll see.
Max quality for everything will be selected by default, and filenames will not be transformed, so scripts can continue to work unmodified.

I might release a tool that will obfuscate filenames in RPA archives, so it makes the difficulty of unpacking your game much higher. However, I will have to deal with rewriting scripts, dealing with scripts that used generated filenames, the images/ and other special directories... I'm curious how easy it is to add support for other archive formats in Ren'Py (like zip/7z/squashfs/psarc/xp3/etc).

User avatar
uyjulian
Regular
Posts: 128
Joined: Sun Mar 08, 2015 1:40 pm
Github: uyjulian
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#3 Post by uyjulian »

Decreasing the -q parameter in cwebp may give you blurry results. That may not be what you want. If you prefer sharpness over number of colors, using pngquant and cwebp -lossless may give similar size, but sharper quality.

The AV1 bitstream is frozen. There is a specification for the AVIF format, but an implementation has not been written yet.

Opus 1.2 has support for fullband stereo audio at 32kbps bitrate. With voice data, it is hard to hear a difference with 16kbps bitrate. With music data, it is hard to hear a difference with 48kbps. If you are using cymbals or similar instruments in your music, 72kbps may be a better choice.

On iOS, Opus encapsulated in Core Audio Format might be able to be used. I haven't checked on this yet, however.

Save your server bandwidth; save your users' storage space; compress and encode your games now to the latest WebP, Opus, and VP9 standards!

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#4 Post by PyTom »

On iOS, Opus encapsulated in Core Audio Format might be able to be used.
Ren'Py doesn't use the system library for audio playback, just fullscreen video.

I'm not going to accept any patches that add patented technology to Ren'Py, which probably rules out some of the formats you listed, BPG among them.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
uyjulian
Regular
Posts: 128
Joined: Sun Mar 08, 2015 1:40 pm
Github: uyjulian
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#5 Post by uyjulian »

libavif seems to be progressing well: https://github.com/AOMediaCodec/libavif

I may work o loading images using ffmpeg instead of sdl2_image.

User avatar
Amethysts
Regular
Posts: 41
Joined: Thu Aug 23, 2018 1:17 pm
Projects: Coalescence
Skype: amethysts-studio
itch: amethysts
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#6 Post by Amethysts »

Your commands are VERY useful, thanks for that !

It helped me save almost 20% of a 80Mo project :)

User avatar
uyjulian
Regular
Posts: 128
Joined: Sun Mar 08, 2015 1:40 pm
Github: uyjulian
Contact:

Re: [Tutorial] How to save space in your Ren'Py game

#7 Post by uyjulian »

Looks like Chrome and Firefox implemented AVIF support.

Now, it's only a matter of time until ffmpeg and/or sdl_image implement support for it…
I'll update this post on how to use AVIF image format after Ren'Py implements support for it.

Post Reply

Who is online

Users browsing this forum: No registered users