Skip to content

Commit

Permalink
Merge pull request #13 from codePerfectPlus/save_audiobook
Browse files Browse the repository at this point in the history
featute: audiobook can be saved now
  • Loading branch information
aayushi-droid authored Oct 14, 2022
2 parents a146a46 + f010fe0 commit 58b5955
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pip install audiobook

```python
from audiobook import AudioBook
ab = AudioBook("file_path")
ab.text_to_speech()
ab = AudioBook() # argument: Speech-Speed="slow/normal/fast"

ab.save_audio(file_path, password=None) # save audio file
ab.read_book(file_path, password=None) # listen to the book
```

## Usages
Expand Down Expand Up @@ -69,7 +71,18 @@ sudo apt update && sudo apt install espeak ffmpeg libespeak1

## Project status

- Alpha
## V1.0.0

- [x] Save Audio Book locally
- [x] Listen to the book
- [x] Speech-speed control
- [x] Read password protected PDF
- [x] Create json file for the book

## Upcoming Features

- [ ] Support more extensions


## Author

Expand Down
27 changes: 25 additions & 2 deletions audiobook/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
logger = logging.getLogger("PyPDF2")
logger.setLevel(logging.INFO)


speed_dict = {
"slow": 100,
"normal": 150,
"fast": 200}


def speak_text(engine, text):
def speak_text(engine, text, print=False):
if print:
print(text)
engine.say(text)
engine.runAndWait()

Expand Down Expand Up @@ -43,7 +44,29 @@ def create_json_book(self, pdf_file_path, password=None):
text = pageObj.extractText()
book_dict[num] = text
return book_dict, pages

def save_audio(self, pdf_file_path, password=None):
if not os.path.exists(pdf_file_path):
raise FileNotFoundError("File not found!")

if not pdf_file_path.endswith(".pdf"):
raise ValueError("File must be a pdf!")

with open(pdf_file_path, "rb") as fp:
basename = os.path.basename(pdf_file_path).split(".")[0]
os.makedirs(basename, exist_ok=True)
logging.info('Saving audio files in folder: {}'.format(basename))
pdfReader = PyPDF2.PdfFileReader(fp)
if pdfReader.isEncrypted:
logging.info("File is encrypted, trying to decrypt...")
pdfReader.decrypt(password)
pages = pdfReader.numPages
for num in range(0, pages):
pageObj = pdfReader.getPage(num)
text = pageObj.extractText()
self.engine.save_to_file(text, os.path.join(basename, basename + "_" + (str(num) + ".mp3")))
self.engine.runAndWait()

def read_book(self, pdf_file_path, password=None):
if not os.path.exists(pdf_file_path):
raise FileNotFoundError("File not found!")
Expand Down

0 comments on commit 58b5955

Please sign in to comment.