|
(use '[leiningen.exec :only (deps)]) |
|
(deps '[[me.raynes/fs "1.4.4"]]) |
|
|
|
(require '[me.raynes.fs :as fs] |
|
'[clojure.java.shell :as sh]) |
|
|
|
;; OBJECTIVES |
|
;; |
|
;; - find all files that are video files and are not yet converted |
|
;; - convert them |
|
;; - - check if file is small enough, otherwise split |
|
|
|
(def arguments |
|
{:path (nth *command-line-args* 1)}) |
|
|
|
(defn absolute-path-without-extension |
|
"Helper function to remove the file extension from the absolute path" |
|
[file] |
|
(let [base (.getAbsolutePath file) |
|
dot (.lastIndexOf base ".")] |
|
(if (pos? dot) (subs base 0 dot) base))) |
|
|
|
(defn wrap-path |
|
"Wrap the absolute path without the extension and the file into a hash" |
|
[coll] |
|
(map (fn [file] |
|
{:path (absolute-path-without-extension file) |
|
:file file}) |
|
coll)) |
|
|
|
(def files-to-convert |
|
(let [raw-files (wrap-path (fs/find-files (:path arguments) #".+\.(mkv|avi|flv|wmv)")) |
|
converted-files (wrap-path (fs/find-files (:path arguments) #".+\.(mp4|m4v)")) |
|
converted-files-set (set (map #(:path %) converted-files))] |
|
(remove (fn [f] |
|
(contains? converted-files-set (:path f))) |
|
raw-files))) |
|
|
|
|
|
(defn gen-args-map |
|
[file-wrap] |
|
{:input (.getAbsolutePath (:file file-wrap)) |
|
:output (str (:path file-wrap) ".mp4") |
|
:encoder "x264" |
|
:cfr true |
|
:quality 20}) |
|
|
|
(defn gen-args-vec |
|
[args] |
|
(into ["/usr/bin/HandBrakeCLI"] |
|
(map (fn [k] |
|
(if (= (k args) true) |
|
(str "--" (name k)) |
|
(str "--" (name k) "=" (k args)))) |
|
(keys args)))) |
|
|
|
(defn process-file |
|
[file] |
|
(let [args (gen-args-map file) |
|
args-vec (gen-args-vec args)] |
|
(do |
|
(println "converting file: " (:input args)) |
|
(apply sh/sh args-vec) |
|
(println "file converted to: " (:output args)) |
|
(if (< 4000000000 (fs/size (:output args))) |
|
(do |
|
(let [path (absolute-path-without-extension (fs/file (:output args)))] |
|
(println "File needs splitting, which will be done now") |
|
(fs/mkdir path) |
|
(sh/sh "/Applications/Osmo4.app/Contents/MacOS/MP4Box" "-splits" "4000000" (:output args) |
|
:dir path) |
|
(println "File splitted"))) |
|
(println (str "File is " (fs/size (:output args)) " and doesn't need splitting")))))) |
|
|
|
(doall (map #(process-file %) files-to-convert)) |