make_video.py (1689B)
1 import cv2 2 import subprocess 3 import tempfile as tf 4 import pandas as pd 5 import numpy as np 6 import os 7 8 def read_raw(file_name, points): 9 raw = np.fromfile(file_name, dtype=np.uint8) 10 real_points = [points[0], points[1], 4] 11 raw = np.squeeze(raw.reshape(real_points)) 12 img = raw[:,:,(1, 2, 3, 0)] 13 return img 14 15 def get_files(directory): 16 files = [] 17 for filename in os.listdir(directory): 18 base, ext = os.path.splitext(filename) 19 if ext == '.bin': 20 files.append(filename) 21 return files 22 23 def make_frames(directory, points): 24 imgs = [] 25 for f in get_files(directory): 26 raw_file_name = os.path.join(directory, f) 27 raw = read_raw(raw_file_name, points) 28 imgs.append(raw) 29 return imgs 30 31 32 def make_video(name, frames, framerate): 33 with tf.TemporaryDirectory() as tmp: 34 count = 0 35 for frame in frames: 36 cv2.imwrite(os.path.join(tmp, "frame_{:02d}.png".format(count)), frame) 37 count = count + 1 38 39 ffmpeg_command = [ 40 "ffmpeg", 41 "-y", 42 "-framerate", "{:d}".format(framerate), 43 "-i", os.path.join(tmp, 'frame_%02d.png'), 44 "-c:v", "libx265", 45 "-crf", "18", 46 name 47 ] 48 subprocess.run(ffmpeg_command) 49 50 51 ################################## 52 save = True 53 out_dir = "/tmp/downloads" 54 points = [1080, 1920] 55 framerate = 60 56 if save: 57 os.makedirs(out_dir, mode=0o644, exist_ok=True) 58 59 frames = make_frames("/tmp/downloads/out", points) 60 if save: 61 make_video(os.path.join(out_dir, "volumes.mp4"), frames, framerate) 62 63 #cv2.imshow("", frames[0]) 64 #cv2.waitKey(0)