if os.path.exists(picture_dir): shutil.rmtree(picture_dir) if os.path.exists(video_dir): shutil.rmtree(video_dir) fr = open('./txt/' + infile[:-4] + '_clean.txt') count_lines = 1 for line in fr: line = line.replace('\n', '') all_topics = get_topics_from_text1(line) print '\n\n', line, '\n' print "all topics ", all_topics, '\n\n' folder_names = [] for i in range(0, len(all_topics)): if len(all_topics) > 4: image_downloader.download_images(all_topics[i], 1) else: image_downloader.download_images(all_topics[i], 2) folder_names.append(all_topics[i].replace(' ', '_')) text_sentences = [f for f in line.split('.') if len(f) > 1] if len(text_sentences) <= 0: continue if not os.path.exists(audio_dir): os.mkdir(audio_dir) if not os.path.exists(picture_dir): os.mkdir(picture_dir) if not os.path.exists(video_dir): os.mkdir(video_dir)
def get_file_name(a,file_obj): filename = 'ed4/static/' + a path = filename infile = filename if(not filename.split('.')[1] == 'txt'): full_text_messy = convert_pdf_to_txt(infile) fw = open(infile[:-4]+'.txt', 'w') fw.write(full_text_messy) fw.close() full_text = convert_txt_to_clean(infile) audio_dir = 'ed4/static/files/audio/tmp' picture_dir = 'ed4/static/files/picture/tmp' video_dir = 'ed4/static/files/video/tmp' if os.path.exists(audio_dir): shutil.rmtree(audio_dir) if os.path.exists(picture_dir): shutil.rmtree(picture_dir) if os.path.exists(video_dir): shutil.rmtree(video_dir) fr = open(infile[:-4]+'_clean.txt') count_lines = 1 for i,line in enumerate(fr): print(i,line) line = line.replace('\n','') all_topics = get_topics_from_text1(line) print('8'*50) print(all_topics) print '\n\n',line,'\n' print "all topics ", all_topics, '\n\n' folder_names = [] for i in range(0,len(all_topics)): if len(all_topics) > 4: image_downloader.download_images(all_topics[i],1) else: image_downloader.download_images(all_topics[i],2) folder_names.append(all_topics[i].replace(' ','_')) text_sentences=[f for f in line.split('.') if len(f)>1] if len(text_sentences) <=0: continue if not os.path.exists(audio_dir): os.mkdir(audio_dir) if not os.path.exists(picture_dir): os.mkdir(picture_dir) if not os.path.exists(video_dir): os.mkdir(video_dir) print "creating "+str(len(text_sentences))+" audio files " for i in range(0,len(text_sentences)): tts = gTTS(text=text_sentences[i], lang='en', slow=False) tts.save(audio_dir+'/'+str(i)+'.mp3') print '\n',text_sentences[i],'\n' print "created "+ str(i)+ " audio file" text_clip_list=[] audio_clip_list=[] try : silence = AudioFileClip('ed4/static/files/audio/silence.mp3').subclip(0,0.1) except Exception, e: print(e) audio_clip_list.append(silence) for i in range(0,len(text_sentences)): try: sent_audio_clip=AudioFileClip(audio_dir+'/'+str(i)+'.mp3') print "length of audio: "+str(i)+" = ",sent_audio_clip.duration audio_clip_list.append(sent_audio_clip) sent_txt_clip = TextClip(format_text(text_sentences[i]),font='Courier-Bold',fontsize=350,color='yellow',bg_color='black',stroke_width=80).set_pos('bottom').set_duration(sent_audio_clip.duration).resize(width=2000) text_clip_list.append(sent_txt_clip) except: continue audio_clip=concatenate_audioclips(audio_clip_list) file_names = [] for i in range(0,len(folder_names)): files = (fn for fn in os.listdir(picture_dir+'/'+folder_names[i]) if fn.endswith('.jpg') or fn.endswith('.png') or fn.endswith('.PNG') or fn.endswith('.JPG') or fn.endswith('.jpeg') or fn.endswith('.JPEG')) for file in files: file_names.append(folder_names[i]+'/'+file) # s_file_names = sorted(file_names, key=lambda x: x.split('.')[0].split('/')[1]) s_file_names = file_names number_of_images=len(s_file_names) print s_file_names # minimum_image_size=1200 size = (1366, 768) im = Image.open('ed4/static/files/picture/black1.jpg') im.resize(size, Image.ANTIALIAS) im.save('ed4/static/files/picture/black1.jpg', "JPEG") video_clip_list=[] black_clip=ImageClip('ed4/static/files/picture/black1.jpg').set_duration(0.1).set_fps(_FPS) video_clip_list.append(black_clip) black = 'ed4/static/files/picture/black1.jpg' title_clip_list = [] if number_of_images > 0: for f in s_file_names: im = Image.open(picture_dir+'/'+f) im.resize(size, Image.ANTIALIAS) im.save(picture_dir+'/'+f, "JPEG") temp_clip=ImageClip(picture_dir+'/'+f).set_duration(audio_clip.duration/number_of_images).set_position('center').set_fps(_FPS).crossfadein(0.5) name_txt_clip = TextClip(format_text(' '.join([word[:1].upper()+word[1:] for word in f.split('/')[0].split('_')])),font='Courier-Bold',fontsize=350,color='yellow',bg_color='black',stroke_width=30).set_position('top').set_duration(audio_clip.duration/number_of_images).resize(height=60) title_clip_list.append(name_txt_clip) # temp_clip = CompositeVideoClip([temp1_clip,name_txt_clip]) video_clip_list.append(temp_clip) # minimum_image_size=min([minimum_image_size,temp_clip.size[0]]) print 'temp_clip width: ',temp_clip.size else: temp_clip=ImageClip(black).set_duration(audio_clip.duration).set_fps(_FPS) video_clip_list.append(temp_clip) # minimum_image_size=min([minimum_image_size,temp_clip.size[0]]) video_clip = concatenate_videoclips(video_clip_list).set_position('center') # print "Minimnum image size: "+str(minimum_image_size) # minimum_image_size = 0.98*minimum_image_size # resize_text_clip_list = [] # for sent_txt_clip in text_clip_list: # resize_text_clip_list.append(sent_txt_clip.resize(width=minimum_image_size)) print('*'*100, text_clip_list) txt_clip=concatenate_videoclips(text_clip_list).set_position('bottom') if len(title_clip_list) > 0: title_clip = concatenate_videoclips(title_clip_list).set_position('top') result=CompositeVideoClip([video_clip,txt_clip,title_clip]) else: result=CompositeVideoClip([video_clip,txt_clip]) print "Composite video clip size: ",result.size result_with_audio=result.set_audio(audio_clip) # print txt_clip.duration # print txt_clip.fps # print audio_clip.duration print "audio duration: "+str(audio_clip.duration) print "result duration: "+str(result.duration) print "result audio duration: "+str(result_with_audio.duration) # print video_clip.fps # print len(video_clip_list) try : result_with_audio.write_videofile(video_dir+'/'+str(count_lines)+'.mp4',codec='libx264',fps=_FPS) except : if len(title_clip_list) > 0: title_clip = concatenate_videoclips(title_clip_list).set_position('top') result=CompositeVideoClip([video_clip,txt_clip,title_clip]) else: result=CompositeVideoClip([video_clip,txt_clip]) result_with_audio=result.set_audio(audio_clip) result_with_audio.write_videofile(video_dir+'/'+str(count_lines)+'.mp4',codec='libx264',fps=_FPS) count_lines += 1 shutil.rmtree(audio_dir) shutil.rmtree(picture_dir)