-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
174 lines (138 loc) · 5.68 KB
/
utils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import cv2
import numpy as np
import subprocess
from tqdm import tqdm
import sys
import matplotlib.pyplot as plt
import shutil
import argparse
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
def list_jpg_files_in_subfolders(root_folder):
jpg_files = []
# Walk through the root folder and its subfolders
for root, _, files in os.walk(root_folder):
for file in files:
if file.lower().endswith((".jpg", ".png", ".JPG", ".jpeg", ".PNG")):
jpg_files.append(os.path.join(root, file))
return jpg_files
def list_audio_files_in_subfolders(root_folder):
audio_files = []
# Walk through the root folder and its subfolders
for root, _, files in os.walk(root_folder):
for file in files:
if file.lower().endswith(('.mp3', '.wav')):
audio_files.append(os.path.join(root, file))
return audio_files
def add_audio_to_video(video_tmp_path, audio_path, output_path):
cmd = 'ffmpeg -hide_banner -loglevel error -y -i "' + video_tmp_path + '" -i "' + \
audio_path + '" "' + output_path + '"'
subprocess.call(cmd, shell=True)
os.remove(video_tmp_path) # remove the template video
def stack2videos(path_vid1, path_vid2, audio_path, fps, out_path):
# Read videos
reader1 = cv2.VideoCapture(path_vid1)
reader2 = cv2.VideoCapture(path_vid2)
# Make video writer
width = int(reader1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(reader1.get(cv2.CAP_PROP_FRAME_HEIGHT))
# get ratio of vid2
w2 = int(reader2.get(cv2.CAP_PROP_FRAME_WIDTH))
h2 = int(reader2.get(cv2.CAP_PROP_FRAME_HEIGHT))
ratio = h2/w2
new_h = int(ratio*width)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
tmp_path = os.path.join(os.path.dirname(out_path), 'tmp.mp4')
writer = cv2.VideoWriter(tmp_path, fourcc, fps, (width, height + new_h))
# print('t: ', (width, height + new_h, 3))
# Stack images
limit = min(int(reader1.get(cv2.CAP_PROP_FRAME_COUNT)), int(reader2.get(cv2.CAP_PROP_FRAME_COUNT)))
for i in tqdm(range(limit)):
_, frame1 = reader1.read()
_, frame2 = reader2.read()
frame2 = cv2.resize(frame2, (width, new_h))
# print(frame2.shape)
img = np.vstack((frame1, frame2))
cv2.waitKey(1)
writer.write(img)
writer.release()
reader1.release()
reader2.release()
cv2.destroyAllWindows()
# Add audio
output_path = os.path.join(out_path)
add_audio_to_video(tmp_path, audio_path, output_path)
def stack2video_horizontal(path_vid1, path_vid2, audio_path, fps, out_path):
# Read videos
reader1 = cv2.VideoCapture(path_vid1)
reader2 = cv2.VideoCapture(path_vid2)
# Make video writer
width = int(reader1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(reader1.get(cv2.CAP_PROP_FRAME_HEIGHT))
# get ratio of vid2
w2 = int(reader2.get(cv2.CAP_PROP_FRAME_WIDTH))
h2 = int(reader2.get(cv2.CAP_PROP_FRAME_HEIGHT))
ratio = w2/h2
new_w = int(ratio*height)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
tmp_path = os.path.join(os.path.dirname(out_path), 'tmp.mp4')
writer = cv2.VideoWriter(tmp_path, fourcc, fps, (width + new_w, height))
# print('t: ', (width, height + new_h, 3))
# Stack images
limit = min(int(reader1.get(cv2.CAP_PROP_FRAME_COUNT)), int(reader2.get(cv2.CAP_PROP_FRAME_COUNT)))
for i in tqdm(range(limit)):
_, frame1 = reader1.read()
_, frame2 = reader2.read()
frame2 = cv2.resize(frame2, (new_w, height))
# print(frame2.shape)
img = np.hstack((frame1, frame2))
cv2.waitKey(1)
writer.write(img)
writer.release()
reader1.release()
reader2.release()
cv2.destroyAllWindows()
os.path.join(out_path)
# Add audio
if audio_path is not None:
add_audio_to_video(tmp_path, audio_path, out_path)
else:
print(tmp_path, out_path)
shutil.copyfile(tmp_path, out_path)
def plot_images(images, prompt, n_images):
from math import ceil
rows, columns = 2, ceil(n_images/2)
fig, axes = plt.subplots(nrows=rows, ncols=columns, figsize=(4*columns,4*rows))
fig.patch.set_facecolor('#1f1f1f')
fig.suptitle(prompt.replace(',', '\n', 1), fontsize=12, color='white')
img_count=0
for i in range(rows):
for j in range(columns):
if img_count < len(images):
axes[i, j].set_title(f'Image {img_count+1}', fontsize=8, color='white')
axes[i, j].axis('off')
axes[i, j].imshow(images[img_count])
img_count+=1
plt.show()
def compute_total_time(n, s, i, f):
total = n * f + (n-1)*(s+1)*i
return total
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--input_path", help="Path to folder with images", type=str)
parser.add_argument("--folder_name", help="Name of the folder to read", type=str)
parser.add_argument("--s", help="Number of diffusion images", type=int, default=5)
parser.add_argument("--i", help="Number of seconds to interpolate between images", type=int, default=10)
parser.add_argument("--f", help="Number of seconds to freeze per original image", type=int, default=20)
args = parser.parse_args()
n = len(os.listdir(os.path.join(args.input_path, args.folder_name)))
total = compute_total_time(n, args.s, args.i, args.f)
print('\n')
print(f'The video {args.folder_name} will be %02d:%02ds long'%(int(total//60), int(total%60)))
print('\n')