| """Tiles mp4s of 2:1 frames at 24 fps into one mp4 without sound, COLS tiles per row, each TILE x TILE/2 with its label | |
| in its top left corner, encoded at the x264 quality CRF (lower is better). A video shorter than the longest holds its | |
| last frame. Usage: grid.py OUT COLS TILE CRF LABEL=VIDEO...""" | |
| import sys | |
| import av | |
| import numpy as np | |
| from PIL import Image, ImageDraw, ImageFont | |
| out, cols, tile, crf = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), sys.argv[4] | |
| items = [arg.split("=", 1) for arg in sys.argv[5:]] | |
| th = tile // 2 | |
| font = ImageFont.load_default(size=th // 9) | |
| def frames(path): | |
| with av.open(path) as c: | |
| return [f.to_image().resize((tile, th), Image.LANCZOS) for f in c.decode(c.streams.video[0])] | |
| clips = [(label, frames(path)) for label, path in items] | |
| count = max(len(f) for _, f in clips) | |
| rows = (len(clips) + cols - 1) // cols | |
| # The index goes first, so that a player can start before it has the whole file. | |
| with av.open(out, mode="w", options={"movflags": "faststart"}) as container: | |
| stream = container.add_stream("libx264", rate=24, options={"crf": crf, "preset": "medium"}) | |
| stream.width, stream.height, stream.pix_fmt = cols * tile, rows * th, "yuv420p" | |
| for k in range(count): | |
| canvas = Image.new("RGB", (cols * tile, rows * th)) | |
| draw = ImageDraw.Draw(canvas) | |
| for n, (label, f) in enumerate(clips): | |
| x, y = (n % cols) * tile, (n // cols) * th | |
| canvas.paste(f[min(k, len(f) - 1)], (x, y)) | |
| draw.text((x + th // 30, y + th // 40), label, "white", font, stroke_width=2, stroke_fill="black") | |
| for packet in stream.encode(av.VideoFrame.from_ndarray(np.asarray(canvas), format="rgb24")): | |
| container.mux(packet) | |
| for packet in stream.encode(): | |
| container.mux(packet) | |
| print(f"wrote {out}: {len(clips)} videos, {count} frames, {cols * tile}x{rows * th}") | |
Xet Storage Details
- Size:
- 1.89 kB
- Xet hash:
- cf1a5a4cd5dd25703cee64766058e42a18de99e7351c454bd394413554b1d941
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.