diff --git a/audiblez/core.py b/audiblez/core.py index ad9ebad..d96033b 100755 --- a/audiblez/core.py +++ b/audiblez/core.py @@ -174,6 +174,18 @@ def gen_audio_segments(pipeline, text, voice, speed, stats=None, max_sentences=N return audio_segments +def gen_text(text, voice='af_heart', output_file='text.wav', speed=1, play=False): + lang_code = voice[:1] + pipeline = KPipeline(lang_code=lang_code) + load_spacy() + audio_segments = gen_audio_segments(pipeline, text, voice=voice, speed=speed); + final_audio = np.concatenate(audio_segments) + soundfile.write(output_file, final_audio, sample_rate) + if play: + subprocess.run(['ffplay', '-autoexit', '-nodisp', output_file]) + + + def find_document_chapters_and_extract_texts(book): """Returns every chapter that is an ITEM_DOCUMENT and enriches each chapter with extracted_text.""" document_chapters = [] @@ -253,7 +265,7 @@ def create_m4b(chapter_files, filename, cover_image, output_folder): audio = AudioSegment.from_wav(wav_file) combined_audio += audio print('Converting to Mp4...') - combined_audio.export(tmp_file_path, format="mp4", codec="aac", bitrate="128k") + combined_audio.export(tmp_file_path, format="mp4", bitrate="128k") final_filename = Path(output_folder) / filename.replace('.epub', '.m4b') print('Creating M4B file...') diff --git a/audiblez/ui.py b/audiblez/ui.py index 6c838cb..43afcf8 100644 --- a/audiblez/ui.py +++ b/audiblez/ui.py @@ -495,7 +495,7 @@ class MainWindow(wx.Frame): tmp_preview_wav_file = NamedTemporaryFile(suffix='.wav', delete=False) soundfile.write(tmp_preview_wav_file, final_audio, core.sample_rate) cmd = ['ffplay', '-autoexit', '-nodisp', tmp_preview_wav_file.name] - subprocess.Popen(' '.join(cmd), stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid) + subprocess.run(cmd) button.SetLabel("🔊 Preview") button.Enable() diff --git a/test/test_main.py b/test/test_main.py index 2a84d31..638b3d8 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -2,11 +2,13 @@ import os import unittest from pathlib import Path -from audiblez.core import main +from ebooklib import epub + +from audiblez.core import main, find_document_chapters_and_extract_texts class MainTest(unittest.TestCase): - def base(self, name, url, **kwargs): + def base(self, name, url='', **kwargs): if not Path(f'{name}.epub').exists(): os.system(f'wget {url} -O {name}.epub') Path(f'{name}.m4b').unlink(missing_ok=True) @@ -42,3 +44,9 @@ class MainTest(unittest.TestCase): def test_chinese(self): url = 'https://www.gutenberg.org/ebooks/24225.epub3.images' self.base('chinese', url, voice='zf_xiaobei') + + def test_leigh(self): + book = epub.read_epub('leigh.epub') + document_chapters = find_document_chapters_and_extract_texts(book) + chapters = [c for c in document_chapters if c.get_name() == 'Text/Chap07.xhtml'] + self.base('leigh', voice='af_heart', selected_chapters=chapters, max_sentences=5)