commit c101cd6e33438517a5e5ecb93ef1c451b87db8d1 Spenser Truex <struex0@gmail.com> 2018-07-31 15:00:19 -0700 Juggling some packages. Successful video conversion. Processors.
librivox.asd | 9 +++++---- packages.lisp | 36 ++++++++++++++++++++++++++++++++++++ rss.asd | 6 ------ rss/rss.lisp | 9 --------- src/bash.lisp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/ffmpeg.lisp | 46 ---------------------------------------------- src/librivox.lisp | 5 +---- utils.lisp | 8 ++++---- youtube.asd | 6 ------ youtube/youtube.lisp | 2 -- 10 files changed, 90 insertions(+), 81 deletions(-)
diff --git a/librivox.asd b/librivox.asd index 86176dd..66a55e4 100644 --- a/librivox.asd +++ b/librivox.asd @@ -1,8 +1,9 @@ (asdf:defsystem :librivox :serial t - :depends-on (:drakma :uiop :rss :youtube) + :depends-on (:drakma :uiop :cl-feedparser :bordeaux-threads) :description "Librivox auto downloader/youtube uploader." - :components ((:file "utils") - (:file "src/librivox") - (:file "src/ffmpeg"))) + :components ((:file "packages") + (:file "utils") + (:file "src/bash") + (:file "src/librivox"))) diff --git a/packages.lisp b/packages.lisp new file mode 100644 index 0000000..88d33dc --- /dev/null +++ b/packages.lisp @@ -0,0 +1,36 @@ +(defpackage :utils + (:use :cl :cl-user) + (:export :remove-after :nth-digit)) +(defpackage :rss + (:use :cl :drakma :utils :cl-feedparser) + (:import-from :drakma :http-request) + (:import-from :utils :remove-after) + ;;(:import-from :feedparser) + ;;(:export ) + ) +(defpackage :youtube + (:use :cl :drakma :utils)) + +(defpackage :bash + (:use :cl :uiop :utils) + (:import-from :utils :nth-digit) + (:export :convert + :processor-cores)) + +(defpackage :librivox + (:use :cl :drakma :utils :uiop :youtube :rss :bordeaux-threads) + (:import-from :uiop :run-program) + (:import-from :bash :convert :processor-cores) + (:import-from :bordeaux-threads + :make-thread + :make-lock + :join-thread + :thread-yield + :make-condition-variable + :condition-wait + :condition-notify + :all-threads + :thread-alive-p + :destroy-thread + :interrupt-thread + :join-thread)) diff --git a/rss.asd b/rss.asd deleted file mode 100644 index c34414c..0000000 --- a/rss.asd +++ /dev/null @@ -1,6 +0,0 @@ -(asdf:defsystem :rss - :serial t - :description "RSS feed reader." - :depends-on (:drakma :cl-feedparser) - :components ((:file "utils") - (:file "rss/rss"))) diff --git a/rss/rss.lisp b/rss/rss.lisp index 226b3f9..f6f0ad2 100644 --- a/rss/rss.lisp +++ b/rss/rss.lisp @@ -1,12 +1,3 @@ -(defpackage :rss - (:use :cl :drakma :utils :cl-feedparser) - (:import-from :drakma - :http-request) - (:import-from :utils - :remove-after) - ;(:import-from :feedparser) - ;(:export ) - ) (in-package :rss) (defvar *persistence* "persistent.lisp") #|(let ((x (parse-feed "https://librivox.org/rss/latest_releases"))) diff --git a/src/bash.lisp b/src/bash.lisp new file mode 100644 index 0000000..65003eb --- /dev/null +++ b/src/bash.lisp @@ -0,0 +1,44 @@ +(in-package :bash) +(defvar *bash-output* *debug-io*) +(defvar *downloads-dir* "/home/jose/common-lisp/librivox/downloads/") +(defun run-line (code-string &optional (output *bash-output*)) + (run-program (format nil code-string *downloads-dir*) :output output)) +(defun run-line-integer-output (code-string) + (parse-integer (run-program (format nil code-string *downloads-dir*) + :output 'string) + :junk-allowed t)) +(defun hours (seconds) + (/ (- seconds (* 60 (minutes seconds)) (seconds seconds)) + 60 60)) +(defun minutes (seconds) + (nth-digit 1 seconds 60)) +(defun seconds (seconds) + (nth-digit 0 seconds 60)) +(defun processor-cores () + (run-line-integer-output "grep -c ^processor /proc/cpuinfo~*")) +(defun convert (image-filename output-filename zip-filename) + "Convert the zip to video." + ;; ~A for the download directory, ~:*~A after the first. + ;; KLUDGE: if there is more than one zip + ;; KLUDGE: rm returns error code 1 for some reason, causing continuations. + ;; I would rather log those and automatically continue. + (ignore-errors (let ((output-filename (merge-pathnames output-filename *downloads-dir*)) + (image-filename (merge-pathnames image-filename *downloads-dir*)) + (zip-filename (merge-pathnames zip-filename *downloads-dir*))) + (run-line (format nil "unzip ~A -d ~~A" zip-filename)) + (run-line "for f in ~A*.mp3; do echo \"file '$f'\" >> ~:*~Aconcat.txt; done") + (run-line "ffmpeg -f concat -safe 0 -i ~Aconcat.txt -c copy ~:*~Aoutput1.mp3") + (let ((seconds (run-line-integer-output "mp3info -p \"%S\" ~Aoutput1.mp3"))) + (run-line (format nil "ffmpeg -loop 1 -i ~A -t ~D:~D:~D -c mjpeg ~~Atemp.mp4" ; + image-filename + (hours seconds) + (minutes seconds) + (seconds seconds))) + (run-line (format nil "ffmpeg -i ~~Atemp.mp4 -i ~~:*~~Aoutput1.mp3 -c copy ~A" + output-filename)) + (run-line "rm ~Atemp.mp4") + (run-line "rm ~A*.mp3") + (run-line (format nil "rm ~A ~A~~*" zip-filename image-filename)) + (run-line "rm ~Aconcat.txt"))))) +;;; (convert "abe_mawruss_1807.jpg" "final-test-probably1.mp4" "abeandmawruss_1807_librivox.zip") + diff --git a/src/ffmpeg.lisp b/src/ffmpeg.lisp deleted file mode 100644 index 044f3e3..0000000 --- a/src/ffmpeg.lisp +++ /dev/null @@ -1,46 +0,0 @@ -(in-package :librivox) -(defvar *bash-output* *debug-io*) -(defparameter *seconds* 0 "The time in seconds of the entire book.") -(defvar *downloads-dir* "/home/jose/common-lisp/librivox/downloads/") -(defun run-lines (&rest code-strings) - (mapcar #'(lambda (x) (run-program (format nil x *downloads-dir*) :output *bash-output*)) - code-strings)) -(defun nth-digit (digit number base) - (if (= 0 digit) - (mod number base) - (nth-digit (1- digit) (floor number base) base))) -(defun hours (seconds) - (/ (- seconds (* 60 (minutes seconds)) (seconds seconds)) - 60 60)) -(defun minutes (seconds) - (nth-digit 1 seconds 60)) -(defun seconds (seconds) - (nth-digit 0 seconds 60)) - -(defun convert (image-pathname output-pathname) - "Convert the zip to video." - ;; ~A for the download directory, ~:*~A after the first. - (let ((output-pathname (merge-pathnames output-pathname *downloads-dir*))) - (unwind-protect (let ((image-pathname (merge-pathnames image-pathname *downloads-dir*))) - (run-lines "unzip ~A*.zip -d ~:*~A" - "for f in ~A*.mp3; do echo \"file '$f'\" >> ~:*~Aconcat.txt; done" - "ffmpeg -f concat -safe 0 -i ~Aconcat.txt -c copy ~:*~Aoutput1.mp3") - (setf *seconds* (parse-integer (run-program (format nil "mp3info -p \"%S\" ~Aoutput1.mp3" *downloads-dir*) ; - :output 'string) - :junk-allowed t)) - (run-lines (format nil "ffmpeg -loop 1 -i ~A -t ~D:~D:~D -c mjpeg ~~Atemp.mp4" ; - image-pathname - (hours *seconds*) - (minutes *seconds*) - (seconds *seconds*))) - - (run-lines (format nil"ffmpeg -i ~~Atemp.mp4 -i ~~:*~~Aoutput1.mp3 -c copy ~~:*~~Aconverted/~A" - output-pathname) - "rm ~Atemp.mp4" - "rm ~A*.mp3" - "rm ~Aconcat.txt")) - (run-lines "rm ~Atemp.mp4" - "rm ~A*.mp3" - "rm ~Aconcat.txt")))) -;;; Example input: (convert "image.jpg" "final-test-probably.mp4") - diff --git a/src/librivox.lisp b/src/librivox.lisp index c12a024..2ce7fb9 100644 --- a/src/librivox.lisp +++ b/src/librivox.lisp @@ -1,6 +1,3 @@ -(defpackage :librivox - (:use :cl :drakma :utils :uiop) - (:import-from :uiop - :run-program)) (in-package :librivox) +(defparameter *max-threads* (processor-cores)) (defvar *url* "https://librivox.org/rss/latest_releases") diff --git a/utils.lisp b/utils.lisp index 2278d16..c853ba1 100644 --- a/utils.lisp +++ b/utils.lisp @@ -1,7 +1,7 @@ -(defpackage :utils - (:use :cl :cl-user) - (:export :remove-after)) (in-package :utils) - (defun remove-after (item list &key (test #'eql)) (subseq list (search (list item) list :test test))) +(defun nth-digit (digit number base) + (if (= 0 digit) + (mod number base) + (nth-digit (1- digit) (floor number base) base))) diff --git a/youtube.asd b/youtube.asd deleted file mode 100644 index 9bae284..0000000 --- a/youtube.asd +++ /dev/null @@ -1,6 +0,0 @@ -(asdf:defsystem :youtube - :serial t - :description "Youtube data api." - :depends-on (:drakma) - :components ((:file "utils") - (:file "youtube/youtube"))) diff --git a/youtube/youtube.lisp b/youtube/youtube.lisp index 2e0a9b2..98fb50a 100644 --- a/youtube/youtube.lisp +++ b/youtube/youtube.lisp @@ -1,3 +1 @@ -(defpackage :youtube - (:use :cl :drakma :utils)) (in-package :youtube)