Created
December 15, 2009 15:00
-
-
Save tjennings/256999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require ['clojure.contrib.shell-out :as 'shell]) | |
(require ['clojure.contrib.str-utils2 :as 'stru]) | |
(use 'clojure.contrib.seq-utils) | |
; Vorbis comment to mp3 tags functions | |
(defn- vorbis-comments | |
"Retuns a seq of seq containing key/value pairs (('ARTIST' 'foo') ('ALBUM' 'bar')) for a given file name" | |
[file] | |
(let [comments (shell/sh "vorbiscomment" file)] | |
(seq (map #(stru/split % #"=") (stru/split comments #"\n"))))) | |
(def tag-comment-map {"ARTIST" "--ta", "ALBUM" "--tl", "TITLE" "--tt", "TRACKNUMBER" "--tn"}) | |
(defn- to-mp3-tags | |
"Just a function used to map vorbis comment seq to mp3 tag seq" | |
[comment] | |
[(tag-comment-map (first comment)) (second comment)]) | |
(defn mp3-tags [file] | |
(let [comments (vorbis-comments file)] | |
(flatten (map to-mp3-tags comments)))) | |
; Decoding / encoding | |
(defn- base-name [fname] (.replaceFirst fname "\\..{3}$" "")) | |
(defn- wav-file [fname] (str (base-name fname) ".wav")) | |
(defn- mp3-file [fname] (str (base-name fname) ".mp3")) | |
(defn- ogg-2-wav [file] | |
(let [out-file (wav-file file)] | |
(shell/sh "oggdec" "-Q" "-o" out-file file))) | |
(defn- wav-2-mp3 [file tags] () | |
(let [out-file (mp3-file file) | |
sh-args (flatten ["lame" "--silent" "--vbr-new" tags file out-file])] | |
(apply shell/sh sh-args) | |
(println "Done " out-file))) | |
(defn- rm [file] (shell/sh "rm" file)) | |
(defn- process-file [file] | |
(ogg-2-wav file) | |
(wav-2-mp3 (wav-file file) (mp3-tags file)) | |
(rm (wav-file file))) | |
(defn process-files [files] | |
(println "Processing " (count files) " files") | |
(dorun (pmap process-file files))) | |
; File lookup functions | |
(def dir (second *command-line-args*)) | |
(defn find-by-name [d n] (stru/split (shell/sh "find" "." "-name" n :dir d) #"\n")) | |
(defn oggs [] (find-by-name dir "*.ogg")) | |
; Run it! | |
(process-files (oggs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment