diff --git a/ipod-shuffle-4g.py b/ipod-shuffle-4g.py index 8225a56..de440c7 100755 --- a/ipod-shuffle-4g.py +++ b/ipod-shuffle-4g.py @@ -97,7 +97,7 @@ def group_tracks_by_id3_template(tracks, template): return sorted(grouped_tracks_dict.items()) class Text2Speech(object): - valid_tts = {'pico2wave': True, 'RHVoice': True, 'espeak': True, 'say': True} + valid_tts = {'pico2wave': True, 'RHVoice': True, 'espeak': True, 'say': True, 'gtts': True} @staticmethod def check_support(): @@ -124,6 +124,14 @@ def check_support(): else: voiceoverAvailable = True + # Check for gtts-cli voiceover + # https://github.com/pndurette/gTTS + if not exec_exists_in_path("gtts-cli"): + Text2Speech.valid_tts['gtts-cli'] = False + print("Warning: gtts-cli not found, voicever won't be generated using it.") + else: + voiceoverAvailable = True + # Check for Russian RHVoice voiceover if not exec_exists_in_path("RHVoice"): Text2Speech.valid_tts['RHVoice'] = False @@ -156,6 +164,8 @@ def text2speech(out_wav_path, text): return True elif Text2Speech.say(out_wav_path, text): return True + elif Text2Speech.gtts(out_wav_path, text): + return True else: return False @@ -181,6 +191,19 @@ def say(out_wav_path, unicodetext): subprocess.call(["say", "-o", out_wav_path, '--data-format=LEI16', '--file-format=WAVE', '--', unicodetext]) return True + @staticmethod + def gtts(out_wav_path, unicodetext): + if not Text2Speech.valid_tts['gtts']: + return False + tmp_mp3_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) + tmp_mp3_file.close() + tmp_wav_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False) + tmp_wav_file.close() + subprocess.call(["gtts-cli", "--lang", "en", unicodetext, "--output", tmp_mp3_file.name]) + subprocess.call(["ffmpeg", "-y", "-loglevel", "error", "-hide_banner", "-nostats", "-i", tmp_mp3_file.name, tmp_wav_file.name]) + subprocess.call(["mv", tmp_wav_file.name, out_wav_path]) + return True + @staticmethod def espeak(out_wav_path, unicodetext): if not Text2Speech.valid_tts['espeak']: