marlin-auto-animation/animator.py

72 lines
2.6 KiB
Python
Raw Permalink Normal View History

2023-05-06 19:40:22 -05:00
import os
2023-05-07 13:54:08 -05:00
output = './_Bootscreen.h'
2023-05-07 14:28:21 -05:00
video = '/path/to/video.mp4'
2023-05-06 19:40:22 -05:00
# Make video into xbm files
os.mkdir('temp')
os.mkdir('temp/pngs')
os.mkdir('temp/xbms')
# Convert video to pngs
2023-05-07 13:54:08 -05:00
os.system(f'ffmpeg -i {video} temp/pngs/%05d.png')
# Remove duplicates and rename files
files = sorted(os.listdir('temp/pngs'))
for item in files:
i = files.index(item)
if (i + 1) % 6 != 1:
os.remove(f'temp/pngs/{item}')
files = sorted(os.listdir('temp/pngs'))
for item in files:
os.rename(f'temp/pngs/{item}', f'temp/pngs/{files.index(item)+1}.png')
2023-05-06 19:40:22 -05:00
# Convert pngs to xbms
pngs = os.listdir('temp/pngs')
filenames = [os.path.splitext(os.path.basename(filename))[0] for filename in pngs]
for filename in filenames:
2023-05-07 13:54:08 -05:00
os.system(f'convert temp/pngs/{filename}.png temp/xbms/{filename}.xbm')
2023-05-06 19:40:22 -05:00
2023-05-07 13:54:08 -05:00
# Make some pngs for debugging
2023-05-09 11:45:24 -05:00
debug = False
2023-05-06 19:40:22 -05:00
if debug:
2023-05-07 13:54:08 -05:00
from random import random
2023-05-06 19:40:22 -05:00
os.mkdir('temp/debug_pngs')
for filename in filenames:
2023-05-07 13:54:08 -05:00
if random() < 0.01: # Takes a long time, so only convert 1% of the images
os.system(f'convert temp/xbms/{filename}.xbm temp/debug_pngs/{filename}.png')
2023-05-06 19:40:22 -05:00
for filename in filenames:
f = open(f'temp/xbms/{filename}.xbm', 'r')
2023-05-07 13:54:08 -05:00
lines = f.readlines()
for line in lines:
line = ' ' + line.strip().replace(', }; ', ' };')
if filename == '1':
lines.insert(0, 'const unsigned char custom_start_bmp[] PROGMEM = {')
else:
lines.insert(0, f'const unsigned char custom_start_bmp{int(filename) - 2}[] PROGMEM = {{')
f.close()
f = open(output, 'a')
f.write('\n')
f.write('\n'.join(lines))
f.write('\n')
2023-05-06 19:40:22 -05:00
f.close()
2023-05-07 13:54:08 -05:00
start_of_ending = ['#ifdef CUSTOM_BOOTSCREEN_ANIMATED_FRAME_TIME', '', ' // Each frame has its own custom duration', ' const boot_frame_t custom_bootscreen_animation[] PROGMEM = {', ' { custom_start_bmp1, 2000 }, // 2.0s', ' { custom_start_bmp1, 100 }, // 0.1s x 5 frames', ' { custom_start_bmp2, 100 },', ' { custom_start_bmp3, 100 },', ' { custom_start_bmp4, 100 },', ' { custom_start_bmp5, 100 },', ' { custom_start_bmp6, 100 },', ' { custom_start_bmp, 500 }, // 0.5s', ' };', '', '#else', '', ' // Each frame shows for CUSTOM_BOOTSCREEN_FRAME_TIME', ' const unsigned char * const custom_bootscreen_animation[] PROGMEM = {']
middle_of_ending = ' custom_start_bmp, '
for i in range(len(filenames))[:-2]:
middle_of_ending += f'custom_start_bmp{i}, '
end_of_ending = [' };', '', '#endif', '']
f = open(output, 'a')
f.write('\n')
for line in start_of_ending:
f.write(line + '\n')
f.write(middle_of_ending)
for line in end_of_ending:
f.write(line + '\n')
f.close()
2023-05-07 14:28:21 -05:00
os.system('rm -r temp/')