Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

featute: audiobook can be saved now #13

Merged
merged 10 commits into from
Oct 14, 2022
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