Get rid of subprocess call to unzip
[babashka-scripts.git] / ebook-to-audio-book.clj
1 #! /usr/bin/env bb
2
3 ; A utility to listen to your ebooks using TTS programs
4
5 (import '[java.nio.file Files FileSystems CopyOption])
6
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
18
19 (defn run-cmd
20 [command]
21 (->> command
22 p/process
23 :out
24 slurp))
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
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])
44 (extract-file-from-zip txtz-file "index.txt" txt-file)
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...")
50 (run! #(io/delete-file %) [txtz-file txt-file aiff-file])
51 (println (str "Done! Check out " mp3-file)))