-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from ayan-joshi/Adding-features-to-Audiobook
Adding features to audiobook
- Loading branch information
Showing
2 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,49 @@ | ||
import pyttsx3 | ||
import PyPDF2 | ||
from PyPDF2 import PdfReader | ||
import threading | ||
import keyboard # Import the keyboard library | ||
|
||
pdf = None | ||
stop_thread = False # Variable to signal stopping the playback | ||
|
||
def play(pdfReader): | ||
global pdf | ||
global stop_thread | ||
|
||
speaker = pyttsx3.init() | ||
|
||
for page_num in range(pdfReader.numPages): | ||
text = pdfReader.getPage(page_num).extractText() | ||
for page_num in range(len(pdfReader.pages)): | ||
if stop_thread: | ||
break # Exit the loop if stop_thread is True | ||
text = pdfReader.pages[page_num].extract_text() | ||
speaker.say(text) | ||
speaker.runAndWait() | ||
|
||
speaker.stop() | ||
|
||
def stop_playback(): | ||
global stop_thread | ||
input("Press Enter to stop playback...") | ||
stop_thread = True # Set the flag to stop playback | ||
|
||
file = input("Enter your PDF file name : ") # Enter your own PDF file path | ||
file = input("Enter your PDF file name: ") | ||
|
||
while True: | ||
try: | ||
book = open(file, "rb") | ||
pdf = PdfReader(file) | ||
break | ||
|
||
except Exception as e: | ||
print("An error occured:\n", e) | ||
print("\nEnter again\n") | ||
print("An error occurred:\n", e) | ||
print("\nEnter the file name again:\n") | ||
file = input("Enter your PDF file name: ") | ||
|
||
# Create a separate thread for playback | ||
playback_thread = threading.Thread(target=play, args=(pdf,)) | ||
playback_thread.start() | ||
|
||
# Start a thread for stopping playback with keyboard input | ||
keyboard.add_hotkey('q', lambda: stop_playback()) | ||
keyboard.wait() # Wait for the hotkey event | ||
|
||
pdfReader = PyPDF2.PdfFileReader(book) | ||
# Wait for the playback to finish | ||
playback_thread.join() |