Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])
:dependencies [[org.clojure/clojure "1.8.0"]])
41 changes: 21 additions & 20 deletions src/ffmpeg_clj/core.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
(ns ffmpeg-clj.core
(:require
[clojure.java.shell :refer [sh]]
[clojure.string :as s]))
(:require
[clojure.java.shell :refer [sh]]
[clojure.string :as s]))

(def ^:dynamic *bin* "ffmpeg")

(defn cmd [& argv]
(->> argv
(map #(if (keyword? %) (str "-" (name %)) (str %)))
(into [*bin*])))
(defn create-cmd-string-seq [& argv]
(->> argv
(map #(if (keyword? %) (str "-" (name %)) (str %)))
(into [*bin*])))

(defn bin [] *bin*)
(defn ffmpeg-factory [shell-call-fn]
(fn [& args]
(let [cmd! (apply shell-call-fn (apply create-cmd-string-seq args))
{:keys [exit out err]} cmd!]
(when-not (zero? exit)
(throw
(Exception. err)))
out)))

(defn ffmpeg! [& args]
(let [cmd! (apply sh (apply cmd args))
{:keys [exit out err]} cmd!]
(when-not (zero? exit)
(throw
(Exception. err)))
out))
(def ffmpeg! (ffmpeg-factory sh))

(defn version []
(as-> (ffmpeg! "-version") o
(re-find #"version \S+" o)
(s/split o #" ")
(last o)))
(defn version []
(as-> (ffmpeg! "-version") o
(re-find #"version \S+" o)
(s/split o #" ")
(last o)))
38 changes: 33 additions & 5 deletions test/ffmpeg_clj/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
(ns ffmpeg-clj.core-test
(:require [clojure.test :refer :all]
[ffmpeg-clj.core :refer :all]))
(:require [clojure.test :refer :all]
[ffmpeg-clj.core :refer :all]
[clojure.string :as s]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
(deftest cmd-creates-command-with-only-ffmpeg-when-no-args-provided
(testing "If no args are provided to (cmd) it should create a list with only the command in it (\"ffmpeg\")."
(is (= (create-cmd-string-seq) ["ffmpeg"]))))

(deftest cmd-creates-command-with-one-option-and-one-value
(testing "A ffmpeg call with an option (using a - before) from a keyword and a value from a string should be created."
(is (= (create-cmd-string-seq :codec "h264") ["ffmpeg", "-codec", "h264"]))))

(deftest cmd-combination-of-many-keywords-and-values
(testing "Keywords must be seen as options for ffmpeg and string values as parameters"
(is (= (create-cmd-string-seq "value1" :option1 "value2" "value3" :option2 "value4" :option3 :option4) ["ffmpeg", "value1", "-option1", "value2", "value3", "-option2", "value4", "-option3", "-option4"]))))


(defn mocked-sh
"A mocked sh function that return a map with the result values as in the arguments"
[exit-code stdout err]
(fn [& arguments-ignored-in-this-mock]
{:exit exit-code :out stdout :err err}))

(deftest ffmpeg-clj-when-exit-code-from-sh-call-is-not-zero-throws-err-as-exception
(testing "Should throw the error of the shell call if the exit code is not 0"
(is (thrown? Exception ((ffmpeg-factory (mocked-sh 1 nil "Error")) "irrelevant_value")))))

(deftest ffmpeg-clj-when-exit-code-from-the-sh-is-zero-returns-the-out-value-call
(testing "Should return the output of the shell call if the exit code is 0"
(is (= ((ffmpeg-factory (mocked-sh 0 "Test stdout value" nil)) "irrelevant_value") "Test stdout value"))))

(deftest ffmpeg-version
(testing "Test the version of ffmpeg"
(is (not (s/blank? (version))))))