view clojure/com/aurellem/run/final_cut.clj @ 533:60da469773e5

minor fix to create render dir if it does not already exist.
author Robert McIntyre <rlm@mit.edu>
date Mon, 25 Jun 2012 11:58:36 -0500
parents a3bdff5554ad
children 3f2072b8e457
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 _ (.mkdir target-dir)
24 _ (.mkdir ram-map-dir)
25 _ (.mkdir frames-dir)
26 wave-writer (WaveWriter. audio-file)
27 moves (vec moves)]
28 (set-state! initial-state)
29 (dorun
30 (map
31 (fn [index]
32 (run-moves @current-state (vector (moves index)))
34 ;; record audio
35 (.process wave-writer (sound-bytes)
36 gb-sound-format)
38 ;; write screenshot
39 (write-png!
40 @current-state
41 (File. frames-dir (format "%07d.png" index)))
43 ;; write ram-image
44 (write-ram-image!
45 @current-state
46 (File. ram-map-dir (format "%07d.png" index))))
47 (range (count moves))))
48 (.cleanup wave-writer)))
49 ([initial-state moves]
50 (render-files! render-dir initial-state moves))
51 ([moves]
52 (render-files! (root) moves)))
54 (defn file-names [#^File dir]
55 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
57 (defn composite-frames-command
58 [screenshot ram-map target move]
59 (let [actual-keys (set move)
60 keys [:a :b :start :select :u :d :l :r]
61 button-on-name ["-size" "5x5" "xc:red"]
62 button-off-name ["-size" "5x5" "xc:black"]]
63 ["convert"
64 "-size" "318x276" "xc:white"
65 (map
66 (fn [index]
67 (let [position ["-geometry"
68 (str "+" (+ 30 (* index 8))
69 "+" "225") "-composite"]]
70 (if (actual-keys (keys index))
71 [button-on-name position]
72 [button-off-name position])))
73 (range (count keys)))
74 screenshot "-geometry" "+10+10" "-composite"
75 ram-map "-geometry" "+180+10" "-composite"
76 target]))
78 (defn generate-composite-frames!
79 [^File rendered-dir moves]
80 (let [final-frames (File. rendered-dir "final")
81 _ (.mkdir final-frames)
82 screenshots (file-names (File. rendered-dir "frames"))
83 ram-maps (file-names (File. rendered-dir "ram-map"))
84 targets (map #(.getCanonicalPath
85 (File. final-frames (format "%07d.bmp" %)))
86 (range (count screenshots)))]
87 (dorun
88 (map
89 (comp
90 (partial apply clojure.java.shell/sh)
91 flatten
92 (partial apply composite-frames-command)
93 (fn [a b c d] (println c) [a b c d]))
94 screenshots ram-maps targets moves))))
97 (defn final-cut! [^File render-dir]
98 (let [movie (File. render-dir "final.ogg")
99 final (File. render-dir "final")]
100 (.delete movie)
101 (clojure.java.shell/sh
102 "ffmpeg" "-r" "60"
103 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
104 "-i" (.getCanonicalPath (File. render-dir "audio.wav"))
105 "-b:a" "128k"
106 "-b:v" "9000k"
107 "-c:a" "libvorbis"
108 "-c:v" "libtheora"
109 (.getCanonicalPath movie)) nil))
112 (comment
114 ;; step 1
116 (render-files!
117 render-dir (root)
118 (take 9000 (first (control-checkpoint))))
120 ;; step 2
122 (generate-composite-frames!
123 render-dir
124 (take 9000 (first (control-checkpoint))))
126 ;; step 3
127 (final-cut! render-dir)