-
Notifications
You must be signed in to change notification settings - Fork 108
/
tile_videos.py
49 lines (35 loc) · 1.27 KB
/
tile_videos.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
from pathlib import Path
import imageio
import numpy as np
from tqdm import tqdm
from PIL import Image
def make_movie(sides=None):
video_dir = Path("movies")
videos = list(video_dir.glob("*.mp4"))
output_file = Path("tiled.mp4")
n_frames = 300
tile_size = 128
num_videos = len(videos)
if not sides:
one_side = int(np.ceil(np.sqrt(num_videos)))
sides = (one_side, one_side)
w = imageio.get_writer(output_file,
format='FFMPEG',
mode='I',
fps=25,
codec='libx264',
pixelformat='yuv420p')
readers = [imageio.get_reader(vid_file) for vid_file in videos]
for frame in tqdm(range(n_frames)):
canvas = Image.new('RGB', (tile_size*sides[0], tile_size*sides[1]))
for idx, reader in enumerate(readers):
im = reader.get_next_data()
im = Image.fromarray(im)
im = im.resize((tile_size, tile_size))
pos = np.unravel_index(idx, sides)
pixel_pos = [int(np.floor(x*tile_size)) for x in pos]
canvas.paste(im, pixel_pos)
w.append_data(np.array(canvas))
w.close()
if __name__ == "__main__":
make_movie((4, 5))