Text to MP3 using pyTTS - Non-programmer question

Dieter Deyke Dieter_Deyke at CoCreate.com
Fri Aug 18 11:13:47 EDT 2006


seyeRMReyes at gmail.com writes:

> I'm not a programmer, but I'd like to make a program that will open and
> read a txt file and output to a mp3 file. I don't need to ever hear the
> voice, but I'd like the program to direct
>
> I've been google'ing around and have found a few tutorials about
> converting pdfs to mp3 and  converting typed text (in the source file)
> to wave, but I'm looking for the ability to directly convert from txt
> to mp3/ogg/wma.
>
> Currently I'm running on WinXP, but I also have access to a Linux box.
>
> Any help would be appreciated.

I have written a script to convert .pdf to .wma, which may get you
started:

#! /usr/bin/env python
# -*- coding: iso-latin-1 -*-

import os
import sys

basename, suffix = os.path.splitext(sys.argv[1])
suffix = suffix.lower()

pdfname = basename + ".pdf"
txtname = basename + ".txt"
wavname = basename + ".wav"

# pdf --> txt

if suffix == ".pdf":
    os.system('pdftotext "%s"' % (pdfname, ))
    suffix = ".txt"

# txt --> wav

if suffix == ".txt":
    import re
    data = open(txtname).read()
    data = data.replace(". . .", "")
    data = data.replace("<", "")
    data = data.replace(">", "")
    data = data.replace("Mr.", "Mister")
    data = data.replace("«", '"')
    data = data.replace("»", '"')
    data = data.replace(',,', '"')
    data = re.sub("\\d+\\s*
", "", data)
    data = re.sub("\n\\s*\\d+\\.*\\s*\n", "\n", data)
    data = re.sub("\n\\s*\n\\s*\n", "\n\n", data)
    data = re.sub("-([a-z])", "\\1", data)
    open(txtname, "w").write(data)
    import pyTTS
    tts = pyTTS.Create()
    tts.SetRate(0)
    tts.Volume = 100
    tts.SetVoiceByName("ATT-DT-14-Klara16")
    tts.SpeakToWave(wavname, data)
    suffix = ".wav"

# split, wav --> mp3/ogg/wma

if suffix == ".wav":
    import wave
    os.mkdir(basename)
    fi = wave.open(wavname, "r")
    nchannels, sampwidth, framerate, nframes, comptype, compname = fi.getparams()
    n = 0
    while 1:
        n += 1
        data = fi.readframes(600 * framerate)
        if not data: break
        fo = wave.open("tmp.wav", "w")
        fo.setparams((nchannels, sampwidth, framerate, 0, comptype, compname))
        fo.writeframes(data)
        fo.close()
        # os.system('lame -m m --cbr -b 32 -q 0 -S tmp.wav "%s\\%02d.mp3" "%s"' % (basename, n))
        # os.system('oggenc -q -1 -o "%s\\%02d.ogg" tmp.wav' % (basename, n))
        os.system('cscript WMCmd.vbs -profile a20_1 -input tmp.wav -output "%s\\%02d.wma' % (basename, n))
    fi.close()
    os.remove("tmp.wav")

os.remove(wavname) ### This is just too big to keep around

--
Dieter Deyke
mailto:Dieter_Deyke at CoCreate.com mailto:deyke at comcast.net
Vs lbh pna ernq guvf, lbh unir jnl gbb zhpu gvzr.



More information about the Python-list mailing list