Skip to content

Instantly share code, notes, and snippets.

@jdkealy
Created February 5, 2019 19:41
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 jdkealy/65edec6c28a89f378c27803fb78be6d8 to your computer and use it in GitHub Desktop.
Save jdkealy/65edec6c28a89f378c27803fb78be6d8 to your computer and use it in GitHub Desktop.
(defn get-length
[path]
(let [cmd (str
"ffmpeg -i '"
path
"' 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//"
)
output (.start (ProcessBuilder. (into-array ["/bin/bash" "-c" cmd])))
info (slurp (.getInputStream output))]
(let [[h m s] (clojure.string/split info #":" ) ]
(+
(* (Long/parseLong h ) 60 60)1
(* (Long/parseLong m) 60)
(Float/parseFloat s)))))
@tallpeak
Copy link

tallpeak commented Feb 6, 2019

;; This might be easier to use on Windows, since I removed grep and split and perhaps bash/sh dependencies

(require '[clojure.java.shell :refer [sh]])
(require '[clojure.string :as str])
(defn get-length
  [path]
    (let [ out (sh "ffmpeg" "-i" path)
           outall (let [{:keys [out err]} out] (str out err))
           DurationLine (apply str (filter #(.contains % "Duration") (str/split outall #"\n")))
           duration (str/split (first (str/split DurationLine #",")) #":")
           [_junk h m s] duration ]
      (+
       (* (Long/parseLong (str/trim h) ) 60 60)1
       (* (Long/parseLong m) 60)
       (Float/parseFloat s))))

(def path "/mnt/big/backup/somefile.mpg")
(get-length path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment