annotate clojure/com/aurellem/run/final_cut.clj @ 532:a3bdff5554ad

completed clojure functions to generate final video.
author Robert McIntyre <rlm@mit.edu>
date Mon, 25 Jun 2012 11:15:08 -0500
parents c6ad363e36f7
children 60da469773e5
rev   line source
rlm@530 1 (ns com.aurellem.run.final-cut
rlm@530 2 (:use (com.aurellem.gb saves gb-driver util constants
rlm@530 3 items vbm characters money
rlm@530 4 rlm-assembly))
rlm@530 5 (:use (com.aurellem.run util sound music title save-corruption
rlm@530 6 bootstrap-0 bootstrap-1
rlm@530 7 ram-display
rlm@530 8 ))
rlm@530 9 (:require clojure.string)
rlm@530 10 (:import [com.aurellem.gb.gb_driver SaveState])
rlm@530 11 (:import java.awt.image.BufferedImage)
rlm@530 12 (:import java.io.File)
rlm@530 13 (:import com.aurellem.gb.WaveWriter))
rlm@530 14
rlm@530 15
rlm@530 16 (def render-dir (File. user-home "proj/vba-clojure/render"))
rlm@530 17
rlm@530 18 (defn render-files!
rlm@530 19 ([^File target-dir initial-state moves]
rlm@530 20 (let [ram-map-dir (File. target-dir "ram-map")
rlm@530 21 frames-dir (File. target-dir "frames")
rlm@530 22 audio-file (File. target-dir "audio.wav")
rlm@530 23 _ (.mkdirs ram-map-dir)
rlm@530 24 _ (.mkdirs frames-dir)
rlm@530 25 wave-writer (WaveWriter. audio-file)
rlm@530 26 moves (vec moves)]
rlm@530 27 (set-state! initial-state)
rlm@530 28 (dorun
rlm@530 29 (map
rlm@530 30 (fn [index]
rlm@530 31 (run-moves @current-state (vector (moves index)))
rlm@530 32
rlm@530 33 ;; record audio
rlm@530 34 (.process wave-writer (sound-bytes)
rlm@530 35 gb-sound-format)
rlm@530 36
rlm@530 37 ;; write screenshot
rlm@530 38 (write-png!
rlm@530 39 @current-state
rlm@530 40 (File. frames-dir (format "%07d.png" index)))
rlm@530 41
rlm@530 42 ;; write ram-image
rlm@530 43 (write-ram-image!
rlm@530 44 @current-state
rlm@530 45 (File. ram-map-dir (format "%07d.png" index))))
rlm@530 46 (range (count moves))))
rlm@530 47 (.cleanup wave-writer)))
rlm@530 48 ([initial-state moves]
rlm@530 49 (render-files! render-dir initial-state moves))
rlm@530 50 ([moves]
rlm@530 51 (render-files! (root) moves)))
rlm@530 52
rlm@531 53 (defn file-names [#^File dir]
rlm@531 54 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
rlm@531 55
rlm@531 56 (defn composite-frames-command
rlm@532 57 [screenshot ram-map target move]
rlm@532 58 (let [actual-keys (set move)
rlm@532 59 keys [:a :b :start :select :u :d :l :r]
rlm@532 60 button-on-name ["-size" "5x5" "xc:red"]
rlm@532 61 button-off-name ["-size" "5x5" "xc:black"]]
rlm@532 62 ["convert"
rlm@532 63 "-size" "318x276" "xc:white"
rlm@532 64 (map
rlm@532 65 (fn [index]
rlm@532 66 (let [position ["-geometry"
rlm@532 67 (str "+" (+ 30 (* index 8))
rlm@532 68 "+" "225") "-composite"]]
rlm@532 69 (if (actual-keys (keys index))
rlm@532 70 [button-on-name position]
rlm@532 71 [button-off-name position])))
rlm@532 72 (range (count keys)))
rlm@532 73 screenshot "-geometry" "+10+10" "-composite"
rlm@532 74 ram-map "-geometry" "+180+10" "-composite"
rlm@532 75 target]))
rlm@531 76
rlm@531 77 (defn generate-composite-frames!
rlm@532 78 [^File rendered-dir moves]
rlm@532 79 (let [final-frames (File. rendered-dir "final")
rlm@532 80 _ (.mkdir final-frames)
rlm@532 81 screenshots (file-names (File. rendered-dir "frames"))
rlm@532 82 ram-maps (file-names (File. rendered-dir "ram-map"))
rlm@532 83 targets (map #(.getCanonicalPath
rlm@532 84 (File. final-frames (format "%07d.bmp" %)))
rlm@532 85 (range (count screenshots)))]
rlm@532 86 (dorun
rlm@532 87 (map
rlm@532 88 (comp
rlm@532 89 (partial apply clojure.java.shell/sh)
rlm@532 90 flatten
rlm@532 91 (partial apply composite-frames-command)
rlm@532 92 (fn [a b c d] (println c) [a b c d]))
rlm@532 93 screenshots ram-maps targets moves))))
rlm@531 94
rlm@532 95
rlm@532 96 (defn final-cut! [^File render-dir]
rlm@532 97 (let [movie (File. render-dir "final.ogg")
rlm@532 98 final (File. render-dir "final")]
rlm@532 99 (.delete movie)
rlm@532 100 (clojure.java.shell/sh
rlm@532 101 "ffmpeg" "-r" "60"
rlm@532 102 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
rlm@532 103 "-i" (.getCanonicalPath (File. render-dir "audio.wav"))
rlm@532 104 "-b:a" "128k"
rlm@532 105 "-b:v" "9000k"
rlm@532 106 "-c:a" "libvorbis"
rlm@532 107 "-c:v" "libtheora"
rlm@532 108 (.getCanonicalPath movie)) nil))
rlm@532 109
rlm@531 110
rlm@532 111 (comment
rlm@531 112
rlm@532 113 ;; step 1
rlm@532 114
rlm@532 115 (render-files!
rlm@532 116 render-dir (root)
rlm@532 117 (take 9000 (first (control-checkpoint))))
rlm@531 118
rlm@532 119 ;; step 2
rlm@532 120
rlm@532 121 (generate-composite-frames!
rlm@532 122 render-dir
rlm@532 123 (take 9000 (first (control-checkpoint))))
rlm@532 124
rlm@532 125 ;; step 3
rlm@532 126 (final-cut! render-dir)
rlm@532 127 )