Skip to content

Instantly share code, notes, and snippets.

@sentientwaffle
Created March 24, 2012 19:08
Show Gist options
  • Save sentientwaffle/2186807 to your computer and use it in GitHub Desktop.
Save sentientwaffle/2186807 to your computer and use it in GitHub Desktop.
linux text-to-mp3

Convert a text file into a directory of MP3 files. Perfect for do-it-yourself robot audiobooks!

Installation

You will need the following dependencies from apt-get:

  • espeak
  • lame

Install them with:

$ sudo apt-get install espeak lame

Usage

ruby speak.rb some_book.txt

This will generate a directory named some_book, which contains a bunch of MP3 files! They are broken up into multiple files in approximately 30 minute chunks for your listening convenience.

MINS_PER_MP3 = 30
def speak(file_name)
story_name = file_name.split(".")[0]
# Make a directory to put all of the _mp3_ files in.
`mkdir #{story_name}`
# Generate the _wav_ files.
`espeak -v en-us -f #{file_name} -s 130 -w ./#{story_name}/#{story_name}.wav --split=#{MINS_PER_MP3}`
# Convert the _wav_ files to _mp3_.
Dir["./#{story_name}/*.wav"].each do |f|
`lame -V2 #{f} #{f.gsub(/\.wav$/, ".mp3")}`
end
# Remove the _wav_ files.
`rm ./#{story_name}/*.wav`
end
file_name = ARGV[0]
if file_name && file_name.strip && ARGV.length == 1
speak file_name
else
puts "Usage: ruby speak.rb <text file>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment