{"id":2113,"date":"2019-10-19T16:30:14","date_gmt":"2019-10-19T11:00:14","guid":{"rendered":"http:\/\/www.mka.in\/wp\/?p=2113"},"modified":"2019-10-19T16:35:21","modified_gmt":"2019-10-19T11:05:21","slug":"text-to-speech-conversion-in-python","status":"publish","type":"post","link":"https:\/\/www.mka.in\/wp\/text-to-speech-conversion-in-python\/","title":{"rendered":"Text to speech conversion in python"},"content":{"rendered":"\n<p>Think of an real life scenario where you need text to speech conversion&#8230;&#8230;ok, what about reading a story for you ?<\/p>\n\n\n\n<p>Let us see how simple is this to implement in python. You need gTTS, google text to speech, python module for this. Here is how you install this module<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nsudo pip3 install gTTS<\/code><\/pre>\n\n\n\n<p>Since we are saving converted audio in a file, so you also need a command line audio player which you can invoke in python code using &#8220;os&#8221; module. You can install and use mpg321 or any other player of your choice.<\/p>\n\n\n\n<p>And here is the simple code to convert text into audio. This program will save audio in speech.mp3 file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#!\/usr\/bin\/python3\n\nfrom gtts import gTTS \nimport os \n  \nintext = 'Dog is chasing a cat and cat is chasing a mouse. Mouse is running hard to save his life. Thats life !!'\nlanguage = 'en'\n  \nt2s = gTTS(text = intext, lang=language, slow=False) \nt2s.save(\"speech.mp3\") \n  \nos.system(\"mpg321 speech.mp3\") <\/code><\/pre>\n\n\n\n<p>Save above code in text2speech.py and execute as following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n.\/text2speech.py \nHigh Performance MPEG 1.0\/2.0\/2.5 Audio Player for Layer 1, 2, and 3.\nVersion 0.3.2-1 (2012\/03\/25). Written and copyrights by Joe Drew,\nnow maintained by Nanakos Chrysostomos and others.\nUses code from various people. See 'README' for more!\nTHIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!\n\nPlaying MPEG stream from speech.mp3 ...\nMPEG 2.0 layer III, 32 kbit\/s, 24000 Hz mono\n                                                                            \n[0:07] Decoding of speech.mp3 finished.<\/code><\/pre>\n\n\n\n<p>Above execution saves audio of text &#8220;Dog is chasing a cat and cat is chasing a mouse. Mouse is running hard to save his life. Thats life !!&#8221; in speech.mp3 and then plays audio using mpg321 player.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Think of an real life scenario where you need text to speech conversion&#8230;&#8230;ok, what about reading a story for you ? Let us see how simple is this to implement in python. You need gTTS, google text to speech, python module for this. Here is how you install this module Since we are saving converted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2115,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[53,111],"class_list":["post-2113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech-bytes","tag-python","tag-text-to-speech"],"_links":{"self":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/comments?post=2113"}],"version-history":[{"count":2,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2113\/revisions"}],"predecessor-version":[{"id":2116,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/posts\/2113\/revisions\/2116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/media\/2115"}],"wp:attachment":[{"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/media?parent=2113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/categories?post=2113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mka.in\/wp\/wp-json\/wp\/v2\/tags?post=2113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}