@@ -37,6 +37,7 @@ def main(args):
3737
3838 # Process each line
3939 for line in lines :
40+
4041 # Remove the newline character at the end of the line
4142 line = line .strip ()
4243
@@ -151,51 +152,111 @@ def main(args):
151152
152153 # =============== SHORT VIDEO ====================
153154
154- with open (os .path .join (dr , "shorts_mp3_list.txt" ), "r" ) as f :
155- lines = f .readlines ()
155+ if os .path .exists (os .path .join (dr , "shorts_mp3_list.txt" )):
156156
157- # create list of chunks
158- outvideo = open ( os . path . join ( dr , 'short_mp4_list.txt' ), 'w' )
157+ with open ( os . path . join ( dr , "shorts_mp3_list.txt" ), "r" ) as f :
158+ lines = f . readlines ( )
159159
160- # Process each line
161- for page_num , line in enumerate (lines ):
162- # Remove the newline character at the end of the line
163- line = line .strip ()
160+ # create list of chunks
161+ outvideo = open (os .path .join (dr , 'short_mp4_list.txt' ), 'w' )
164162
165- # Split the line into components
166- components = line .split ()
163+ # Process each line
164+ for page_num , line in enumerate (lines ):
165+ # Remove the newline character at the end of the line
166+ line = line .strip ()
167167
168- # The filename is the second component
169- audio = components [1 ].replace ('.mp3' , '' )
170- video = audio .replace ('-' , '' )
168+ # Split the line into components
169+ components = line .split ()
171170
172- # convert to PNG
173- if page_num == 0 :
174- input_path = os .path .join (dr , str (page_num ))
175- else :
176- input_path = os .path .join (dr , 'slides' , f'slide_{ page_num } ' )
171+ # The filename is the second component
172+ audio = components [1 ].replace ('.mp3' , '' )
173+ video = audio .replace ('-' , '' )
177174
178- os .system (f'{ args .gs } -sDEVICE=png16m -r500 -o { os .path .join (dr , str (page_num ))} .png { input_path } .pdf' )
175+ # convert to PNG
176+ if page_num == 0 :
177+ input_path = os .path .join (dr , str (page_num ))
178+ else :
179+ input_path = os .path .join (dr , 'slides' , f'slide_{ page_num } ' )
179180
180- resolution = "scale=1920:-2"
181- os .system (f'{ args .ffmpeg } -loop 1 -i { os .path .join (dr , str (page_num ))} .png -i { os .path .join (dr , audio )} .mp3 '
182- f'-vf { resolution } -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
183- f'-shortest { os .path .join (dr , video )} .mp4' )
181+ os .system (f'{ args .gs } -sDEVICE=png16m -r500 -o { os .path .join (dr , str (page_num ))} .png { input_path } .pdf' )
184182
185- # ensure that there is no silence at the end of the video, and video len is the same as audio len
186- os .system (f'audio_duration=$( { args .ffprobe } -i { os .path .join (dr , audio ) } .mp3 -show_entries format=duration '
187- f'-v quiet -of csv="p=0"); ' f'audio_duration=$((${{audio_duration%.*}} + 1)); ' f' { args . ffmpeg } '
188- f'-i { os . path . join ( dr , video ) } .mp4 -t $audio_duration -y -c copy { os .path .join (dr , video )} _final .mp4' )
183+ resolution = "scale=1920:-2"
184+ os .system (f'{ args .ffmpeg } -loop 1 - i { os .path .join (dr , str ( page_num )) } .png -i { os . path . join ( dr , audio ) } .mp3 '
185+ f'-vf { resolution } -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
186+ f'-shortest { os .path .join (dr , video )} .mp4' )
189187
190- # list of all chunks
191- outvideo .write (f"file '{ video } _final.mp4'\n " )
188+ # ensure that there is no silence at the end of the video, and video len is the same as audio len
189+ os .system (f'audio_duration=$({ args .ffprobe } -i { os .path .join (dr , audio )} .mp3 -show_entries format=duration '
190+ f'-v quiet -of csv="p=0"); ' f'audio_duration=$((${{audio_duration%.*}} + 1)); ' f'{ args .ffmpeg } '
191+ f'-i { os .path .join (dr , video )} .mp4 -t $audio_duration -y -c copy { os .path .join (dr , video )} _final.mp4' )
192192
193- outvideo .close ()
193+ # list of all chunks
194+ outvideo .write (f"file '{ video } _final.mp4'\n " )
194195
195- # joint video
196- os .system (f'{ args .ffmpeg } -f concat -i { os .path .join (dr , "short_mp4_list.txt" )} '
197- f'-y -c copy { os .path .join (dr , "output_short.mp4" )} ' )
196+ outvideo .close ()
197+
198+ # joint video
199+ os .system (f'{ args .ffmpeg } -f concat -i { os .path .join (dr , "short_mp4_list.txt" )} '
200+ f'-y -c copy { os .path .join (dr , "output_short.mp4" )} ' )
201+
202+ # =============== QA VIDEO ====================
203+
204+ if os .path .exists (os .path .join (dr , "qa_mp3_list.txt" )):
205+
206+ with open (os .path .join (dr , "qa_mp3_list.txt" ), "r" ) as f :
207+ lines = f .readlines ()
208+
209+ # create list of chunks
210+ outvideo = open (os .path .join (dr , 'qa_mp4_list.txt' ), 'w' )
211+
212+ qa_pages = pickle .load (open (os .path .join (dr , 'qa_pages.pkl' ), 'rb' ))
213+
214+ # Process each line
215+ turn = - 1
216+ for line_num , line in enumerate (lines ):
217+ # Remove the newline character at the end of the line
218+ line = line .strip ()
219+
220+ # Split the line into components
221+ components = line .split ()
222+
223+ # The filename is the second component
224+ audio = components [1 ].replace ('.mp3' , '' )
225+ video = audio .replace ('-' , '' )
226+
227+ # convert to PNG
228+ if 'question' in audio : # question - get created slide
229+ turn += 1
230+ page_num = 0
231+ input_path = os .path .join (dr , 'questions' , f'question_{ turn } ' )
232+ else : # answer - get single page from paper
233+ p_num = qa_pages [turn ][page_num ]
234+ # extract the page from PDF
235+ 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' )
236+ input_path = os .path .join (dr , f'{ p_num } ' )
237+ page_num += 1
238+
239+ qa_page = 'qa_page.png'
240+ os .system (f'{ args .gs } -sDEVICE=png16m -r500 -o { os .path .join (dr , qa_page )} { input_path } .pdf' )
241+
242+ resolution = "scale=1920:-2"
243+ os .system (f'{ args .ffmpeg } -loop 1 -i { os .path .join (dr , qa_page )} -i { os .path .join (dr , audio )} .mp3 '
244+ f'-vf { resolution } -c:v libx264 -tune stillimage -y -c:a aac -b:a 128k -pix_fmt yuv420p '
245+ f'-shortest { os .path .join (dr , video )} .mp4' )
246+
247+ # ensure that there is no silence at the end of the video, and video len is the same as audio len
248+ os .system (f'audio_duration=$({ args .ffprobe } -i { os .path .join (dr , audio )} .mp3 -show_entries format=duration '
249+ f'-v quiet -of csv="p=0"); ' f'audio_duration=$((${{audio_duration%.*}} + 1)); ' f'{ args .ffmpeg } '
250+ f'-i { os .path .join (dr , video )} .mp4 -t $audio_duration -y -c copy { os .path .join (dr , video )} _final.mp4' )
251+
252+ # list of all chunks
253+ outvideo .write (f"file '{ video } _final.mp4'\n " )
254+
255+ outvideo .close ()
198256
257+ # joint video
258+ os .system (f'{ args .ffmpeg } -f concat -i { os .path .join (dr , "qa_mp4_list.txt" )} '
259+ f'-y -c copy { os .path .join (dr , "output_qa.mp4" )} ' )
199260
200261if __name__ == "__main__" :
201262 parser = argparse .ArgumentParser (description = 'Arguments' )
0 commit comments