question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How with google text-to-speech (gTTS) I can save to the MP3 file 2 Variables with different languages? (Python)

See original GitHub issue

How with google text-to-speech I can save to the MP3 file 2 Variables with different languages? Please help me. Everywhere is written only for 1 language. This is my code:

from gtts import gTTS
import os
import pickle
import pandas as pd

frame = pd.read_csv('file.csv', header=0, sep = '\t', encoding='cp1251')
print(frame)

text1 = list()
text2 = list()

for a, b in zip(frame['English'], frame['Русский']):
    text1.append(a)
    text2.append(b)

print(text1,
      text2)

text1 = str(text1)
text2 = str(text2)

tts1 = gTTS(text=text1, lang='en')
tts2 = gTTS(text=text2, lang='ru')


# tts2.save("from_file.mp3") it work just for one Variable!



with open('from_file.mp3', 'wb') as pickle_file:
    pickle.dump([tts1, tts2], pickle_file) 
# with pickle it doesn't work!

os.system("from_file.mp3")

file content:

English   Русский

tell	  говорить
fly	  летать
sit	  сидеть
act	  действовать

As a solution: I can loop every word with gTTS and add in mp3 file, but how can I add data in mp3 without delete past data ?? I want to create an audio dictionary.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
cd412commented, Feb 27, 2018

This should help you get two languages in one mp3 file.

from gtts import gTTS
import os
import pandas as pd

frame = pd.read_csv('file.csv', header=0, sep = '\t')
print(frame)

text1 = list()
text2 = list()

for a, b in zip(frame['English'], frame['Russian']):
    text1.append(a)
    text2.append(b)

print(text1,
      text2)

text1 = str(text1)
text2 = str(text2)

tts1 = gTTS(text=text1, lang='en')
tts2 = gTTS(text=text2, lang='ru')

with open('from_file.mp3', 'wb') as fp:
    tts1.write_to_fp(fp)
    tts2.write_to_fp(fp)    

os.system("from_file.mp3")

# file contents:
# English	Russian
# one		1	
# two		2
# three		3
# four		4
1reaction
vladislav55commented, Apr 19, 2018

You need to use for . You will have 1 mp3 file with this sequence of words --> ‘en’, ‘zh-tw’, ‘en’, ‘zh-tw’… “File.mp3” should be in the project directory or specify the full path to the file.

You can use:

tts1 = gTTS(text=t1, lang='en', slow=True)

if you want to play slowly. You can also use Pandas if you want to take the words from a file (.txt , .csv …)

from gtts import gTTS
import os
import pandas as pd

frame = pd.read_csv('file.csv', header=0, sep = '\t', encoding='cp1251')
print(frame)

text1 = list()
text2 = list()

for a, b in zip(frame['English'], frame['Русский']):
    text1.append(a)
    text2.append(b)

#print(text1, text2)

with open('from_file.mp3', 'wb') as fp:
    for t1, t2 in zip(text1, text2):
        t1 = str(t1)
        t2 = str(t2)
        #print(t1)
        #print(t2)
        q = gTTS(text=t1, lang='en')
        w = gTTS(text=t2, lang='ru')
        #print(q)
        #print(w)
        q.write_to_fp(fp)
        w.write_to_fp(fp)
 
os.system("from_file.mp3")
Read more comments on GitHub >

github_iconTop Results From Across the Web

How with google text-to-speech (gTTS) I can ... - Stack Overflow
How with google text-to-speech (gTTS) I can save to the MP3 file 2 Variables with different languages? (Python) Save this question. Show ...
Read more >
Python - Transform text in mp3 with Google Text to Speech gTTS
It is possible to convert a text into an audio file in mp3 format with a simple script.My blogs about Python : ...
Read more >
How to get started with Google Text-to-Speech using Python
We will import the gTTS library from the gtts module which can be used for speech translation. The text variable is a string...
Read more >
Using the Text-to-Speech API with Python - Google Codelabs
The Text-to-Speech API enables developers to generate human-like speech. The API converts text into audio formats such as WAV, MP3, or Ogg Opus....
Read more >
Make Your Python Program Speak! - Level Up Coding
text = "Hello World!" tts = gTTS(text, slow=False, pre_processor_funcs = [abbreviations, end_of_line]) # Save the audio in a mp3 file tts ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found