Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit dc8f4f1

Browse files
improved multithreading
Significantly reduced the memory usage
1 parent 9fedb20 commit dc8f4f1

File tree

2 files changed

+27
-52
lines changed

2 files changed

+27
-52
lines changed

t2v.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def getDisplayMode(mode):
3434

3535

3636
def textToFrame(data):
37-
ind = data[0]
38-
text = data[1]
39-
opts = data[2]
37+
text = data[0]
38+
opts = data[1]
4039
# print(os.getpid())
4140

4241
monospace = cv2.freetype.createFreeType2()
@@ -59,7 +58,7 @@ def textToFrame(data):
5958
frame = cv2.resize(frame, dsize=(
6059
opts['output_width'], opts['output_height']))
6160
# cv2.imwrite('test/{:04}.png'.format(ind), frame)
62-
return (ind, frame)
61+
return frame
6362

6463

6564
def main():
@@ -105,24 +104,15 @@ def main():
105104
output = args.output + '_tmp.mp4'
106105
print(output)
107106

108-
frames_unord = []
109-
with mp.Pool(mp.cpu_count()) as pool:
110-
with tqdm(total=len(texts)) as t:
111-
for res in pool.imap_unordered(
112-
textToFrame, zip(range(len(texts)), texts, repeat(opts))):
113-
t.update()
114-
frames_unord.append(res)
115-
116-
frames = sorted(frames_unord)
117-
frames = list(map(lambda p: p[1], frames))
118-
119107
out = cv2.VideoWriter(output, cv2.VideoWriter_fourcc(
120108
*'mp4v'), FPS, (WIDTH, HEIGHT))
121109

122-
with tqdm(total=len(frames)) as t:
123-
for frame in frames:
124-
out.write(frame)
125-
t.update()
110+
with mp.Pool(mp.cpu_count()) as pool:
111+
with tqdm(total=len(texts)) as t:
112+
for res in pool.imap(
113+
textToFrame, zip(texts, repeat(opts))):
114+
t.update()
115+
out.write(res)
126116

127117
out.release()
128118

v2t.py

+18-33
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ def slicer(a, chunk_i, chunk_j, two_d=True):
5454
return c
5555

5656

57-
def frameToText(data):
58-
k = data[0]
59-
frame = data[1]
60-
opts = data[2]
57+
def frameToText(frame, opts):
6158
text = ''
6259

6360
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
@@ -87,13 +84,17 @@ def frameToText(data):
8784
tmp3 = [tmp2[i:i+chunk_size] for i in range(0, len(tmp2), chunk_size)]
8885

8986
text += '\n'.join(tmp3)
90-
return (k, text)
87+
return text
9188

9289

93-
def loadFrame(data):
94-
ind = data[0]
95-
path = data[1]
96-
return (ind, cv2.imread(path))
90+
def loadFrame(path):
91+
return cv2.imread(path)
92+
93+
94+
def loadFrameAndConvertToText(data):
95+
path = data[0]
96+
opts = data[1]
97+
return frameToText(loadFrame(path), opts)
9798

9899

99100
def main(args):
@@ -120,38 +121,24 @@ def main(args):
120121
opts['PALETTE'] = PALETTE
121122
opts['mode'] = args.mode
122123

123-
frames_unord = []
124-
125124
with tempfile.TemporaryDirectory() as tmpdir:
126125
print(tmpdir)
127126
ffmpeg.input(filepath).output(os.path.join(tmpdir, '%d.png')).run()
128127
files = os.listdir(tmpdir)
129128
files.sort(key=lambda f: int(re.sub('\D', '', f)))
130129
with mp.Pool(mp.cpu_count()) as pool:
131130
with tqdm(total=len(files)) as t:
132-
for res in pool.imap_unordered(loadFrame, zip(range(len(files)), list(map(lambda f: os.path.join(tmpdir, f), files)))):
133-
frames_unord.append(res)
131+
for res in pool.imap(loadFrameAndConvertToText, zip(list(map(lambda f: os.path.join(tmpdir, f), files)), repeat(opts))):
132+
texts.append(res)
134133
t.update()
135134

136-
frames = sorted(frames_unord)
137-
frames = list(map(lambda p: p[1], frames))
138-
139-
texts_unord = []
140-
141-
with mp.Pool(mp.cpu_count()) as pool:
142-
with tqdm(total=len(frames)) as t:
143-
for res in pool.imap_unordered(frameToText, zip(range(len(frames)), frames, repeat(opts))):
144-
t.update()
145-
texts_unord.append(res)
146-
147-
texts = sorted(texts_unord)
148-
texts = list(map(lambda p: p[1], texts))
149-
150135
print('frames loaded')
151136

152-
fps = MediaInfo.parse(filepath).tracks[0].frame_rate
153-
origwidth = frames[0].shape[1]
154-
origheight = frames[0].shape[0]
137+
media_info = [info for info in MediaInfo.parse(
138+
filepath).tracks if info.track_type == 'Video'][0]
139+
fps = media_info.frame_rate
140+
origwidth = media_info.width
141+
origheight = media_info.height
155142
width = ceil(origwidth / N_WIDTH)
156143
height = ceil(origheight / N_HEIGHT)
157144

@@ -160,9 +147,7 @@ def main(args):
160147
f.write("{},{},{},{},{},{}\n".format(
161148
filepath, fps, int(width), int(height), origwidth, origheight))
162149
f.write('=====\n')
163-
for text in texts:
164-
f.write(text)
165-
f.write('\n')
150+
f.write('\n'.join(texts))
166151

167152

168153
if __name__ == "__main__":

0 commit comments

Comments
 (0)