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