]> njoseph.me Git - babashka-scripts.git/blame - ebook-to-audio-book.clj
Get rid of subprocess call to unzip
[babashka-scripts.git] / ebook-to-audio-book.clj
CommitLineData
fa20f4c6
JN
1#! /usr/bin/env bb
2
3; A utility to listen to your ebooks using TTS programs
4
f7a7c885
JN
5(import '[java.nio.file Files FileSystems CopyOption])
6
fa20f4c6
JN
7(require '[babashka.process :as p]
8 '[clojure.java.io :as io]
9 '[clojure.string :refer [split]])
10
11;; TODO Check if all required utilities are installed
12
13;; TODO Allow voice selection
14(println "Selected voice is Samantha")
15
16;; TODO Use festival-tts or say depending on OS
17
f7a7c885 18
fa20f4c6
JN
19(defn run-cmd
20 [command]
f7a7c885 21 (->> command
fa20f4c6
JN
22 p/process
23 :out
24 slurp))
f7a7c885
JN
25
26(defn extract-file-from-zip
27 [zip-file-name source destination]
28 (let [zip-file (io/file zip-file-name)
29 src (io/file source)
30 dest (io/file destination)
31 fs (FileSystems/newFileSystem (.toPath zip-file) nil)
32 file-in-zip (.getPath fs source (into-array String []))]
33 (Files/copy file-in-zip (.toPath dest)
34 (into-array CopyOption []))))
35
fa20f4c6
JN
36(let [book-file (first *command-line-args*)
37 title (first (split book-file #"\."))
38 txtz-file (str title ".txtz")
39 txt-file (str title ".txt")
40 aiff-file (str title ".aiff")
41 mp3-file (str title ".mp3")]
42 (println "Converting to text...")
43 (run-cmd ["ebook-convert" book-file txtz-file])
f7a7c885 44 (extract-file-from-zip txtz-file "index.txt" txt-file)
fa20f4c6
JN
45 (println "Converting text to audio. Don't hold your breath!")
46 (run-cmd ["say" "-f" txt-file "-o" aiff-file])
47 (println "Creating mp3 file...")
48 (run-cmd ["ffmpeg" "-i" aiff-file mp3-file])
49 (println "Cleaning up...")
f7a7c885 50 (run! #(io/delete-file %) [txtz-file txt-file aiff-file])
fa20f4c6 51 (println (str "Done! Check out " mp3-file)))