forked from imelnyk/ArxivPapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakevideo.py
270 lines (201 loc) · 10.7 KB
/
makevideo.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os
import zipfile
import re
import fitz
from PIL import Image, ImageDraw
import shutil
import glob
import pickle
import argparse
def main(args):
files = glob.glob(os.path.join(f"{args.paperid}_files", "zipfile.zip"))
if files:
zip_name = max(files, key=os.path.getmtime)
# time_str = zip_name.split('-')[1].strip('.zip')
dr = os.path.join(f"{args.paperid}_files", "output")
else:
return
if os.path.exists(dr):
shutil.rmtree(dr)
with zipfile.ZipFile(zip_name, 'r') as zipf:
# Extract all the contents of the zip file
zipf.extractall(dr)
with open(os.path.join(dr, "mp3_list.txt"), "r") as f:
lines = f.readlines()
# create list of chunks
outvideo = open(os.path.join(dr, 'mp4_list.txt'), 'w')
block_coords = pickle.load(open(os.path.join(dr, 'block_coords.pkl'), 'rb'))
gptpagemap = pickle.load(open(os.path.join(dr, 'gptpagemap.pkl'), 'rb'))
# Process each line
for line in lines:
# Remove the newline character at the end of the line
line = line.strip()
# Split the line into components
components = line.split()
# The filename is the second component
audio = components[1].replace('.mp3', '')
video = audio.replace('-', '')
# The number is the fourth component (without the #)
match = re.search(r'page(\d+)', components[1])
page_num = int(match.group(1))
# extract first page of PDF
os.system(f'{args.gs} -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dFirstPage={page_num+1} -dLastPage={page_num+1} -sOutputFile={os.path.join(dr, str(page_num))}.pdf {os.path.join(dr,"main.pdf")} > /dev/null 2>&1')
# convert to PNG
os.system(f'{args.gs} -sDEVICE=png16m -r300 -o {os.path.join(dr, str(page_num))}.png {os.path.join(dr, str(page_num))}.pdf')
if 'summary' not in components[1]:
doc = fitz.open(f'{os.path.join(dr, str(page_num))}.pdf')
page = doc[0]
match = re.search(r'block(\d+)', components[1])
block_num = int(match.group(1))
for pb in gptpagemap:
if isinstance(pb, list):
if pb[1] == page_num and pb[2] == block_num:
coords = block_coords[pb[0]][block_num]
break
# Load an image
image = Image.open(f'{os.path.join(dr, str(page_num))}.png')
# Calculate scale factors
scale_x = image.width / page.rect.width
scale_y = image.height / page.rect.height
# Rescale coordinates
x0, y0, x1, y1 = coords
x0 *= scale_x
y0 *= scale_y
x1 *= scale_x
y1 *= scale_y
# find out if this rectangle is on the left, right or whole width
if coords[2] > page.rect.width / 2:
pointerleft = Image.open('imgs/pointertoleft.png')
pointer = pointerleft.convert("RGBA")
onrightside = True
else:
pointerright = Image.open('imgs/pointertoright.png')
pointer = pointerright.convert("RGBA")
onrightside = False
# Create draw object
draw = ImageDraw.Draw(image)
# Define thickness
thickness = 5
# Draw several rectangles to simulate thickness
for i in range(thickness):
draw.rectangle([x0 - i - 5, y0 - i - 5, x1 + i + 5, y1 + i + 5], outline="green")
# Calculate the center of the rectangle
rect_center_y = (y0 + y1) / 2
# Scale down the pointer image while preserving aspect ratio
desired_height = image.height / 20
aspect_ratio = pointer.width / pointer.height
new_width = int(aspect_ratio * desired_height)
pointer = pointer.resize((new_width, int(desired_height)))
# Calculate position for the pointer
if onrightside:
pointer_x0 = x1 + 20
else:
pointer_x0 = x0 - 20 - new_width
pointer_y0 = rect_center_y - (pointer.height / 2)
# Paste the pointer on the main image
image.paste(pointer, (int(pointer_x0), int(pointer_y0)), pointer) # The last argument is for transparency
# Save the combined image to a file
image.save(f'{os.path.join(dr, str(page_num))}.png')
# process each image-audio pair to create video chunk
resolution = "scale=1920:-2"
os.system(f'{args.ffmpeg} -loop 1 -i {os.path.join(dr, str(page_num))}.png -i {os.path.join(dr, audio)}.mp3 '
f'-vf {resolution} -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
f'-shortest {os.path.join(dr, video)}.mp4')
# ensure that there is no silence at the end of the video, and video len is the same as audio len
os.system(f'audio_duration=$({args.ffprobe} -i {os.path.join(dr, audio)}.mp3 '
f'-show_entries format=duration -v quiet -of csv="p=0"); '
f'audio_duration=$((${{audio_duration%.*}} + 1)); '
f'{args.ffmpeg} -i {os.path.join(dr, video)}.mp4 -t $audio_duration '
f'-y -c copy {os.path.join(dr, video)}_final.mp4')
# list of all chunks
outvideo.write(f"file '{video}_final.mp4'\n")
outvideo.close()
# joint video
os.system(f'{args.ffmpeg} -f concat -i {os.path.join(dr, "mp4_list.txt")} -y -c copy {os.path.join(dr, "output.mp4")}')
# =============== SHORT VIDEO ====================
if os.path.exists(os.path.join(dr, "shorts_mp3_list.txt")):
with open(os.path.join(dr, "shorts_mp3_list.txt"), "r") as f:
lines = f.readlines()
# create list of chunks
outvideo = open(os.path.join(dr, 'short_mp4_list.txt'), 'w')
# Process each line
for page_num, line in enumerate(lines):
# Remove the newline character at the end of the line
line = line.strip()
# Split the line into components
components = line.split()
# The filename is the second component
audio = components[1].replace('.mp3', '')
video = audio.replace('-', '')
# convert to PNG
if page_num == 0:
input_path = os.path.join(dr, str(page_num))
else:
input_path = os.path.join(dr, 'slides', f'slide_{page_num}')
os.system(f'{args.gs} -sDEVICE=png16m -r500 -o {os.path.join(dr, str(page_num))}.png {input_path}.pdf')
resolution = "scale=1920:-2"
os.system(f'{args.ffmpeg} -loop 1 -i {os.path.join(dr, str(page_num))}.png -i {os.path.join(dr, audio)}.mp3 '
f'-vf {resolution} -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
f'-shortest {os.path.join(dr, video)}.mp4')
# ensure that there is no silence at the end of the video, and video len is the same as audio len
os.system(f'audio_duration=$({args.ffprobe} -i {os.path.join(dr, audio)}.mp3 -show_entries format=duration '
f'-v quiet -of csv="p=0"); 'f'audio_duration=$((${{audio_duration%.*}} + 1)); 'f'{args.ffmpeg} '
f'-i {os.path.join(dr, video)}.mp4 -t $audio_duration -y -c copy {os.path.join(dr, video)}_final.mp4')
# list of all chunks
outvideo.write(f"file '{video}_final.mp4'\n")
outvideo.close()
# joint video
os.system(f'{args.ffmpeg} -f concat -i {os.path.join(dr, "short_mp4_list.txt")} '
f'-y -c copy {os.path.join(dr, "output_short.mp4")}')
# =============== QA VIDEO ====================
if os.path.exists(os.path.join(dr, "qa_mp3_list.txt")):
with open(os.path.join(dr, "qa_mp3_list.txt"), "r") as f:
lines = f.readlines()
# create list of chunks
outvideo = open(os.path.join(dr, 'qa_mp4_list.txt'), 'w')
qa_pages = pickle.load(open(os.path.join(dr, 'qa_pages.pkl'), 'rb'))
# Process each line
turn = -1
for line_num, line in enumerate(lines):
# Remove the newline character at the end of the line
line = line.strip()
# Split the line into components
components = line.split()
# The filename is the second component
audio = components[1].replace('.mp3', '')
video = audio.replace('-', '')
# convert to PNG
if 'question' in audio: # question - get created slide
turn += 1
page_num = 0
input_path = os.path.join(dr, 'questions', f'question_{turn}')
else: # answer - get single page from paper
p_num = qa_pages[turn][page_num]
# extract the page from PDF
os.system(f'{args.gs} -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dFirstPage={p_num+1} -dLastPage={p_num+1} -sOutputFile={os.path.join(dr, str(p_num))}.pdf {os.path.join(dr, "main.pdf")} > /dev/null 2>&1')
input_path = os.path.join(dr, f'{p_num}')
page_num += 1
qa_page = 'qa_page.png'
os.system(f'{args.gs} -sDEVICE=png16m -r500 -o {os.path.join(dr, qa_page)} {input_path}.pdf')
resolution = "scale=1920:-2"
os.system(f'{args.ffmpeg} -loop 1 -i {os.path.join(dr, qa_page)} -i {os.path.join(dr, audio)}.mp3 '
f'-vf {resolution} -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
f'-shortest {os.path.join(dr, video)}.mp4')
# ensure that there is no silence at the end of the video, and video len is the same as audio len
os.system(f'audio_duration=$({args.ffprobe} -i {os.path.join(dr, audio)}.mp3 -show_entries format=duration '
f'-v quiet -of csv="p=0"); 'f'audio_duration=$((${{audio_duration%.*}} + 1)); 'f'{args.ffmpeg} '
f'-i {os.path.join(dr, video)}.mp4 -t $audio_duration -y -c copy {os.path.join(dr, video)}_final.mp4')
# list of all chunks
outvideo.write(f"file '{video}_final.mp4'\n")
outvideo.close()
# joint video
os.system(f'{args.ffmpeg} -f concat -i {os.path.join(dr, "qa_mp4_list.txt")} '
f'-y -c copy {os.path.join(dr, "output_qa.mp4")}')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument("--paperid", type=str, default='')
parser.add_argument("--ffmpeg", type=str, default='ffmpeg')
parser.add_argument("--ffprobe", type=str, default='ffprobe')
parser.add_argument("--gs", type=str, default='gs')
args = parser.parse_args()
main(args)