Skip to content

Instantly share code, notes, and snippets.

@aimerneige
Last active April 1, 2023 17:28
Show Gist options
  • Save aimerneige/b1dcba78193d43eb2d7300eb4d023c55 to your computer and use it in GitHub Desktop.
Save aimerneige/b1dcba78193d43eb2d7300eb4d023c55 to your computer and use it in GitHub Desktop.
conver your pgn to a video (suitable for phoe)
#!/usr/env/bin python3
# -*- coding: utf-8 -*-
import os
import chess
import chess.pgn
import chess.svg
PGN_FILE_PATH = "./sample.pgn"
SVG_FILE_TEMP = "./svg"
PNG_FILE_TEMP = "./png"
SUBTITLE_FILE = "./subs.srt"
INKSCAPE_PATH = "./inkscape"
FRAME_RATE = 1
STEP_1_VIDEO = "./step1.mp4"
STEP_2_VIDEO = "./step2.mp4"
FINAL_VIDEO = "./output.mp4"
FONT_NAME = "Source Han Sans CN"
PRIMARY_COLOUR = "&HFFFFFF&"
FONT_SIZE = 24
MARGIN_L = 5
MARGIN_V = 48
# read pgn file
pgn = open(PGN_FILE_PATH)
game = chess.pgn.read_game(pgn)
board = game.board()
# generate the init board
svg_file_path = os.path.join(SVG_FILE_TEMP, '0.svg')
with open(svg_file_path, 'w') as f:
svg = chess.svg.board(
board=board,
size=720,
)
f.write(svg)
# generate board svg
svg_index = 1
for move in game.mainline_moves():
board.push(move)
svg = chess.svg.board(
board=board,
size=720,
)
svg_file_path = os.path.join(SVG_FILE_TEMP, f'{svg_index}.svg')
with open(svg_file_path, 'w') as f:
f.write(svg)
svg_index += 1
# convert to png
for index in range(svg_index):
svg_file_path = os.path.join(SVG_FILE_TEMP, f'{index}.svg')
png_file_path = os.path.join(PNG_FILE_TEMP, f'{index}.png')
command = f'{INKSCAPE_PATH} -w 720 -h 720 {svg_file_path} -o {png_file_path}'
print(command)
os.system(command)
# merge to mp4
command_png2mp4 = f'ffmpeg -framerate {FRAME_RATE} -i {PNG_FILE_TEMP}/%d.png -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p {STEP_1_VIDEO}'
os.system(command_png2mp4)
command_fit_size = f'ffmpeg -i {STEP_1_VIDEO} -filter_complex "scale=864:864,pad=1080:1920:108:216:black" {STEP_2_VIDEO}'
os.system(command_fit_size)
command_add_subtitle = f'ffmpeg -i {STEP_2_VIDEO} -vf "subtitles={SUBTITLE_FILE}:force_style=\'FontName={FONT_NAME},FontSize={FONT_SIZE},PrimaryColour={PRIMARY_COLOUR},Alignment=2,MarginL={MARGIN_L},MarginV={MARGIN_V}\'" {FINAL_VIDEO}'
os.system(command_add_subtitle)
#!/usr/env/bin python3
# -*- coding: utf-8 -*-
SUBTITLE_TEXT = "./subtitle.txt"
SUBTITLE_FILE = "./subs.srt"
# use bellow code to convert your text file into srt
# then manually insert a `\n`
def format_srt_time(seconds: int) -> str:
end_seconds = seconds + 1
start_hour = str(seconds // 3600).zfill(2)
seconds %= 3600
start_minute = str(seconds // 60).zfill(2)
seconds %= 60
start_second = str(seconds).zfill(2)
end_hour = str(end_seconds // 3600).zfill(2)
end_seconds %= 3600
end_minute = str(end_seconds // 60).zfill(2)
end_seconds %= 60
end_second = str(end_seconds).zfill(2)
return f'{start_hour}:{start_minute}:{start_second},000 --> {end_hour}:{end_minute}:{end_second},000'
# convert to srt
with open(SUBTITLE_TEXT, 'r') as f:
lines = f.readlines()
with open(SUBTITLE_FILE, 'w') as f:
index = 1
second = 0
for line in lines:
if line.strip() == "":
second += 1
continue
f.write(str(index))
f.write("\n")
f.write(format_srt_time(second))
f.write("\n")
f.write(line)
f.write("\n")
f.write('\n')
index += 1
second += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment