How To Convert Lit to MP3

Disclaimer: The information in this post is meant to be educational only and not to be used to commit a crime, regardless of which country the information is received in.


This system was devised using a Linux machine running SuSE 10.2. It is possible, by changing various programs and scripts, to use this method on another OS, however that would be left as an exercise for your own mind. Also, you should be familiar with both the python and C programming languages in order to convert the following code examples to ft your own needs. This entire system is a bit awkward and could be further refined to work with a single program, which I may or may not write in the future. For now, this works perfectly for me, or it would if I were crass enough to actually download *.lit files…

So, you have a couple of hundred .lit files, which you purchased for use with your Microsoft Reader, sitting on your computer and no time to read them. You would like to transfer them to your portable mp3 player, but have no idea how to accomplish this, especially since you recently converted from Windows to Linux in an effort to fully utilize all of you computer without proprietary programs. Okay, so maybe it’s not that complicated, maybe you just found a torrent with thousands of .lit files and would like to listen to them instead of actually reading them… For shame! You should never steal from book companies by downloading copies in .lit format, even if you own the original book. I will go forward assuming that you actually own the .lit files, mm-kay?

(1) Locate the folder in which your .lit files are stored, then open that folder in a shell :

shane@emmy:~/txt
> cd /home2/ebooks/Asprin*

shane@emmy:/home2/ebooks/Asprin, Robert
> ls
Asprin, Robert – Myth 01 – Another Fine Myth.lit
Asprin, Robert – Myth 02 – Myth Conceptions.lit
Asprin, Robert – Myth 03.5 – Myth-ion Improbable.lit
Asprin, Robert – Myth 03 – Myth Directions.lit
Asprin, Robert – Myth 04 – Hit or Myth.lit
Asprin, Robert – Myth 05 – Myth-Ing Persons.lit
Asprin, Robert – Myth 06 – Little Myth Marker.lit
Asprin, Robert – Myth 07 – M.Y.T.H. Inc Link.lit
Asprin, Robert – Myth 08 – MythNomers & ImPervections.lit
Asprin, Robert – Myth 09 – M.Y.T.H. Inc. in Action.lit
Asprin, Robert – Myth 10 – Sweet Myth-tery of Life.lit

shane@emmy:/home2/ebooks/Asprin, Robert >

(2) Create a new folder named html and convert all of the files you wish to translate using the clit program…

shane@emmy:/home2/ebooks/Asprin, Robert
> mkdir html

shane@emmy:/home2/ebooks/Asprin, Robert
> clit “Asprin, Robert – Myth 01 – Another Fine Myth.lit” html
+—[ ConvertLIT (Version 1.5) ]—————[ Copyright (c) 2002,2003 ]—
ConvertLIT comes with ABSOLUTELY NO WARRANTY; for details
see the COPYING file or visit “http://www.gnu.org/license/gpl.html”.
This is free software, and you are welcome to redistribute it under
certain conditions. See the GPL license for details.
LIT INFORMATION………
DRM = 1
Timestamp = 44de0267
Creator = 7ffde000
Language = 00000409
….. Asprin, Robert – 1. Another Fine Myth.htm “text/html”– Asprin,_Robert_-_1._Another_Fine_Myth
….. ~Cover01.jpg “image/jpeg”– RW_~Cover01
….. ~Cover02.jpg “image/jpeg”– RW_~Cover02
….. ~Cover03.jpg “image/jpeg”– RW_~Cover03
….. ~Cover04.jpg “image/jpeg”– RW_~Cover04
….. ~Cover05.jpg “image/jpeg”– RW_~Cover05
Writing out “Asprin,_Robert_-_1._Another_Fine_Myth” as “Asprin, Robert – 1. Another Fine Myth.htm” …
Successfully written to “html/Asprin, Robert – 1. Another Fine Myth.htm”.

Writing out “RW_~Cover01″ as “~Cover01.jpg” …
Successfully written to “html/~Cover01.jpg”.

Writing out “RW_~Cover02″ as “~Cover02.jpg” …
Successfully written to “html/~Cover02.jpg”.

Writing out “RW_~Cover03″ as “~Cover03.jpg” …
Successfully written to “html/~Cover03.jpg”.

Writing out “RW_~Cover04″ as “~Cover04.jpg” …
Successfully written to “html/~Cover04.jpg”.

Writing out “RW_~Cover05″ as “~Cover05.jpg” …
Successfully written to “html/~Cover05.jpg”.

Exploded “Asprin, Robert – Myth 01 – Another Fine Myth.lit” into “html/”.

shane@emmy:/home2/ebooks/Asprin, Robert
>

(3) At this point, the clit program has converted the contents of the .lit file to html, which is nice for viewing with a browser, but it’s hard to convert this to speech, so we have to edit the file. My friend Fletcher was nice enough to write the following python script that will help to delete most of the html codes and such:

import sgmllib, string, sys
class Stripper(sgmllib.SGMLParser):
def __init__(self):
self.data = []
sgmllib.SGMLParser.__init__(self)
def unknown_starttag(self, tag, attrib):
self.data.append(” “)
def unknown_endtag(self, tag):
self.data.append(” “)
def handle_data(self, data):
self.data.append(data)
def gettext(self):
text = string.join(self.data, “”)
text = string.join(string.split(text))
return text.replace(“.”, “. “)

def htmlstrip(text):
s = Stripper()
s.feed(text)
s.close()

return s.gettext()

try:

inFile = open(sys.argv[1], ‘r’)
outFile = open(sys.argv[1]+’.txt’, ‘w’)
for eachLine in inFile.readlines():
outFile.write(htmlstrip(eachLine)+’\r\n’)
inFile.close()
outFile.close()
print “Done”

except:
print “Please enter a filename: python stripper.py filename”

(3a) Copy/Paste that into your favorite text editor and save it as ‘stripper.py’. This script should be placed into the html directory. Now cd to the html director and have a peek:

shane@emmy:/home2/ebooks/Asprin, Robert
> cd html

shane@emmy:/home2/ebooks/Asprin, Robert/html
> ls
Asprin, Robert – 1. Another Fine Myth.htm ~Cover01.jpg ~Cover03.jpg ~Cover05.jpg
Asprin, Robert – Myth 01 – Another Fine Myth.opf ~Cover02.jpg ~Cover04.jpg stripper.py

shane@emmy:/home2/ebooks/Asprin, Robert/html
>

(3b) As you can see, there are quite a few files created by clit, so you can delete anything ending with *jpg or *.opf. Now then, let’s run the new script against the remaining html file:

shane@emmy:/home2/ebooks/Asprin, Robert/html
> rm -rf *.opf

shane@emmy:/home2/ebooks/Asprin, Robert/html
> rm -rf *.jpg

shane@emmy:/home2/ebooks/Asprin, Robert/html
> python stripper.py “Asprin, Robert – 1. Another Fine Myth.htm”
Done

shane@emmy:/home2/ebooks/Asprin, Robert/html
>

(3c) Unfortunately, this will not strip everything you will want removed, so go ahead and open the resulting text file with your favorite editor and delete everything before the start of the first chapter.

(4a) Now that you have the text file looking the way you want it, you’ll want to break it into smaller parts. I use the bash program ‘split’ to accomplish this:

shane@emmy:/home2/ebooks/Asprin, Robert/html
> split -da 4 -l 70 “Asprin, Robert – 1. Another Fine Myth.htm.txt” 001_

(4b) This will split the file into manageable pieces, in this instance it will split it into 83 separate files named 001_0000 to 001_0082 which makes it easier for the next step. Using the festival speech engine, which when properly installed, provides a ‘text2wave’ script which can be used at the command line. If you do not have festival properly installed, the following code will not work!

(4c) I wrote the following C program to facilitate translating everything over. It is meant to handle multiple split books and should be modified to fit your particular needs:

#include
#include

int main () {

int i = 0, j = 0;
char a[2460];
char b[2460];

/* ‘j’ keeps track of the current book. In this case, I only want to translate 1 series
* of files starting with 001_ but if I wanted to do 10 series, I would just increase the
* ‘j < 2′ to ‘j < 11′.
*/
for (j = 1; j < 2; j++) {
/* ‘i’ keeps track of which piece of the series we are translating. In this case, we
* have 83 pieces ranging from 0000 to 0082. In the case you are translating several
* series, change ’83′ to the highest number of pieces available to any series.
*/
for (i = 0; i < 83; i++) {
if (j < 10) {
if (i < 10) {
sprintf(a, “00%d_000%d”, j, i);
} else {
sprintf(a, “00%d_00%d”, j, i);
}
} else {
if (i < 10) {
sprintf(a, “0%d_000%d”, j, i);
} else {
sprintf(a, “0%d_00%d”, j, i);
}
}
/* This is where we actually use text2wave */
sprintf(b, “text2wave %s -o %s.wav”, a, a);
system (b);
/* And now we use ‘lame’ to transcode from .wav to .mp3 */
sprintf(b, “lame %s.wav %s.mp3 -V2 –vbr-new -q0 –lowpass 19.7″, a, a);
system (b);
sprintf(b, “echo ————%s Completed —————”, a); system (b);
}
}

/* Now we will create a new directory named ‘mp3′ */
system(“mkdir mp3″);
/* And now we move all of the mp3 files to the new directory */
system(“mv *.mp3 mp3″);

/* And finally we delete the unwanted wave files… */
system(“rm -rf *.wav”);
system(“echo txt2mp3 task completed!”);

return (1);
}

(4d) Copy/Paste that code to your favorite text editor, save it as convert.c
(4e) Compile convert.c with ‘gcc -o convert convert.c’ and then run the resultant binary with:
./convert

(5) Once convert has finished, you will have translated the *.lit file into a computer-read version of your favorite book and these into a set of *.mp3 files that will play on your favorite mp3 player for your entertainment needs!

You Might Also Like...

  • No Related Post
blog comments powered by Disqus