Skip to content

Instantly share code, notes, and snippets.

@philippkueng
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippkueng/7dfe18aa2945639154bf to your computer and use it in GitHub Desktop.
Save philippkueng/7dfe18aa2945639154bf to your computer and use it in GitHub Desktop.
Convert all video files in a directory recursively to mp4 files of max size 4GB

README

MP4BOX
Download and install the [GPAC package](http://gpac.wp.mines-telecom.fr/downloads/gpac-nightly-builds/#Mac OS X 64 bits)

HandBrakeCLI
Download and install the HandBrake binary

Leiningen

  1. First install Leiningen by following the instructions on their website
  2. Then create the file profiles.clj in your ~/.lein/ directory with {:user {:plugins [[lein-exec "0.3.4"]]}}
  3. Download this script and execute it with lein exec convert.clj "/path/to/video/folder"
(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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment