-
Notifications
You must be signed in to change notification settings - Fork 3
/
kwzVideo.py
84 lines (75 loc) · 2.6 KB
/
kwzVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# =========================
# kwzVideo.py version 1.0.0
# =========================
#
# Convert kwzs to mp4, using ffmpeg
# (no audio for now)
#
# Usage:
# python kwzVideo.py <input.kwz> <output.mp4>
from sys import argv
from kwz import KWZParser, PALETTE
import subprocess as sp
import numpy as np
# convert palette to bytes
PALETTE = [bytes(color) for color in PALETTE]
if len(argv) != 3:
print("\nUsage: python3 kwzVideo.py <input.kwz> <output.mp4>\n")
exit(1)
else:
infile = argv[1]
outfile = argv[2]
with open(infile, "rb") as kwz:
parser = KWZParser(kwz)
process = sp.Popen([
"ffmpeg",
# Hide the FFMPEG banner
"-hide_banner",
"-loglevel", "info",
"-y",
# Set the input frame size
"-s", "320x240",
# Set the frame rate
"-r", str(parser.framerate),
# Input format is raw, uncompressed frames
"-f", "rawvideo",
# thread_queue_size is the buffer size of the packets which are waiting to be encoded.
# The default value is 8, but I was getting a "Thread message queue blocking; consider raising the thread_queue_size option"
"-thread_queue_size", "600",
# Input color format is rgb
"-pix_fmt", "rgb24",
# Read frame input from stdin
"-i", "pipe:0",
# Set output codecs and other settings to the most suitable values for HTML5 video playback
"-vcodec", "libx264",
"-pix_fmt", "yuv420p",
# Rescale each frame, using nearest neighbor interpolation
"-vf", "scale=%d:%d:flags=neighbor" % (320*2, 240*2),
# Use all available threads for slight speed boost
"-threads", "0",
# Set compression preset
"-preset", "medium",
# Tune output for animation
"-tune", "animation",
# "-f", "mp4",
outfile
], stdin=sp.PIPE)
image = np.zeros((240, 320), dtype="V3")
for frame_index in range(parser.frame_count):
colors = parser.get_frame_palette(frame_index)
# fill image with paper color
image.fill(PALETTE[colors[0]])
layers = parser.decode_frame(frame_index)
# merge layers into image using numpy magic (aka -- masks)
image[layers[2] == 1] = PALETTE[colors[5]]
image[layers[2] == 2] = PALETTE[colors[6]]
image[layers[1] == 1] = PALETTE[colors[3]]
image[layers[1] == 2] = PALETTE[colors[4]]
image[layers[0] == 1] = PALETTE[colors[1]]
image[layers[0] == 2] = PALETTE[colors[2]]
# convert image to bytes
process.stdin.write(image.tobytes())
# Close the stdin pipe and wait for completion
process.stdin.close()
process.wait()
print("\nFinished conversion!\n")