|
| 1 | +# Import libraries |
| 2 | +from PIL import Image |
| 3 | +import pytesseract |
| 4 | +from mutagen.mp3 import MP3 |
| 5 | +from moviepy.editor import VideoFileClip |
| 6 | +import moviepy.editor as mpe |
| 7 | +from gtts import gTTS |
| 8 | +from pdf2image import convert_from_path |
| 9 | +import os |
| 10 | + |
| 11 | + |
| 12 | +def pdf2text(PDF_file): |
| 13 | + |
| 14 | + # Getting all pages of Pdf |
| 15 | + pages = convert_from_path(PDF_file, 500) |
| 16 | + |
| 17 | + image_counter = 1 |
| 18 | + |
| 19 | + print("Converting to images......") |
| 20 | + for page in pages: |
| 21 | + |
| 22 | + filename = "page_"+str(image_counter)+".jpg" |
| 23 | + |
| 24 | + page.save(filename, 'JPEG') |
| 25 | + |
| 26 | + image_counter = image_counter + 1 |
| 27 | + |
| 28 | + filelimit = image_counter-1 |
| 29 | + |
| 30 | + mtext = "" |
| 31 | + |
| 32 | + print("Extracting Text.......") |
| 33 | + for i in range(1, filelimit + 1): |
| 34 | + |
| 35 | + filename = "page_"+str(i)+".jpg" |
| 36 | + |
| 37 | + mtext += str(((pytesseract.image_to_string(Image.open(filename))))) |
| 38 | + |
| 39 | + # replacing the text like arg-ument (which are included in new line with hyphen with word) |
| 40 | + mtext = mtext.replace('-\n', '') |
| 41 | + |
| 42 | + # Deleting Image files |
| 43 | + for i in range(1, filelimit + 1): |
| 44 | + filename = "page_"+str(i)+".jpg" |
| 45 | + os.remove(filename) |
| 46 | + |
| 47 | + return mtext |
| 48 | + |
| 49 | + |
| 50 | +def text2video(mtext, video_file, Pdf_file_name): |
| 51 | + |
| 52 | + language = 'en' |
| 53 | + |
| 54 | + # Converting text to audio |
| 55 | + myobj = gTTS(text=mtext, lang=language, slow=False) |
| 56 | + |
| 57 | + myobj.save("output.mp3") |
| 58 | + |
| 59 | + audio = MP3("output.mp3") |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + # duration of audio file in seconds |
| 64 | + audio_length = int(audio.info.length) |
| 65 | + |
| 66 | + |
| 67 | + videoclip = VideoFileClip(video_file) |
| 68 | + |
| 69 | + |
| 70 | + if int(videoclip.duration)>audio_length: |
| 71 | + |
| 72 | + # Clipping orignal video according to the length of video |
| 73 | + videoclip = videoclip.subclip(0, audio_length) |
| 74 | + |
| 75 | + background_music = mpe.AudioFileClip("output.mp3") |
| 76 | + |
| 77 | + new_clip = videoclip.set_audio(background_music) |
| 78 | + |
| 79 | + name_of_vdeo_file = Pdf_file_name.split(".pdf")[0]+"(video).mp4" |
| 80 | + |
| 81 | + new_clip.write_videofile(name_of_vdeo_file) |
| 82 | + os.remove("output.mp3") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + # Getting name of pdf file |
| 87 | + PDF_file = input("Enter the name of Pdf file with extension:- ") |
| 88 | + |
| 89 | + # Getting name of video file |
| 90 | + video_file = input("Enter the name of video File with extension:- ") |
| 91 | + |
| 92 | + # Extracting Text from Pdf |
| 93 | + text = pdf2text(PDF_file) |
| 94 | + |
| 95 | + # Converting text to video |
| 96 | + text2video(text, video_file, PDF_file) |
0 commit comments