Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 886177a

Browse files
added pdf2video converter
1 parent cc6299d commit 886177a

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

‎Pdf2Video_Converter/Readme.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Pdf2Video Converter
2+
## Short description
3+
It will take a pdf file and a video as input and Add text audio of pdf to the Given Video.
4+
## Setup instructions
5+
In order to run this script, you need to have Python and pip installed on your system. After you're done installing Python and pip, run the following command from your terminal to install the requirements from the same folder (directory) of the project.
6+
```
7+
pip install -r requirements.txt
8+
```
9+
10+
After satisfying all the requirements for the project:-
11+
12+
- Place the required pdf and video in same folder as of script.
13+
- Run following command :-
14+
```
15+
python script.py
16+
```
17+
or
18+
```
19+
python3 script.py
20+
```
21+
Depending upon the python version. Make sure that you are running the command from the same virtual environment in which the required modules are installed.
22+
23+
24+
## Output
25+
User is asked for a Pdf file and video(mp4) file and Output Video file is stored in the folder
26+
27+
28+
## Author(s)
29+
[Sukriti Sood](https://github.com/Sukriti-sood)

‎Pdf2Video_Converter/requirements.txt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
certifi==2020年12月5日
2+
chardet==4.0.0
3+
click==8.0.0
4+
decorator==4.4.2
5+
gTTS==2.2.2
6+
idna==2.10
7+
imageio==2.9.0
8+
imageio-ffmpeg==0.4.3
9+
moviepy==1.0.3
10+
mutagen==1.45.1
11+
numpy==1.20.3
12+
pdf2image==1.15.1
13+
Pillow==8.2.0
14+
proglog==0.1.9
15+
pytesseract==0.3.7
16+
requests==2.25.1
17+
six==1.16.0
18+
tqdm==4.60.0
19+
urllib3==1.26.4

‎Pdf2Video_Converter/script.py‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /