view clojure/com/aurellem/run/final_cut.clj @ 611:d9f991cddad9

spellcheck
author Robert McIntyre <rlm@mit.edu>
date Thu, 22 Nov 2012 11:24:52 -0600
parents 743b7b68e74e
children
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/test"))
19 (defn render-files!
20 ([^File target-dir
21 moves-render-fn
22 render-ram-map?
23 initial-state
24 moves]
25 (write-moves-to-file! moves (File. target-dir "rlm-yellow-hack.vbm"))
26 (let [ram-map-dir (File. target-dir "ram-map")
27 frames-dir (File. target-dir "frames")
28 moves-dir (File. target-dir "moves")
29 audio-file (File. target-dir "audio.wav")
30 _ (.mkdir target-dir)
31 _ (if render-ram-map? (.mkdir ram-map-dir))
32 _ (.mkdir frames-dir)
33 _ (.mkdir moves-dir)
34 wave-writer (WaveWriter. audio-file)
35 moves (vec moves)
36 desired-fps 60
37 seconds-per-frame (/ 1.0 desired-fps)]
38 (set-state! initial-state)
39 ;; clear sound buffer
40 (sound-bytes)
41 (try
42 (dorun
43 (reduce
44 (fn [[total-audio-samples
45 total-video-samples] move]
46 (run-moves @current-state (vector move))
47 (let [sound (sound-bytes)
48 total-audio-samples (+ total-audio-samples
49 (count sound))
50 total-audio-time
51 (*
52 (/ total-audio-samples 4)
53 (/ 44100))
55 total-video-time (* total-video-samples
56 seconds-per-frame)
58 av-diff (- total-audio-time
59 total-video-time)
60 write-video!
61 (fn [index]
62 ;; write screenshot
63 (moves-render-fn
64 move
65 (File. moves-dir (format "%07d.png" index)))
67 (write-png!
68 @current-state
69 (File. frames-dir (format "%07d.png" index)))
71 ;; write ram-image
72 (if render-ram-map?
73 (write-ram-image!
74 @current-state
75 (File. ram-map-dir (format "%07d.png" index)))))]
76 ;;(println "audio-samples:" (count sound))
78 ;; record audio
79 (.process wave-writer sound gb-sound-format)
81 ;; duplicate or drop frames depending on
82 ;; desired-fps
83 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))
84 (if (< 0 av-diff)
85 ;; audio has gone past video, duplicate video.
86 (do (println
87 "duplicating frame, av-diff is" av-diff)
88 (write-video! (+ 1 total-video-samples))
89 (write-video! (+ 2 total-video-samples))
90 [total-audio-samples
91 (+ 2 total-video-samples)])
92 ;; video has gone past audio, drop frame.
93 (do (println
94 "dropping frame, av-diff is" av-diff)
95 [total-audio-samples
96 total-video-samples]))
97 ;; no frame dropping or duplication required.
98 (do ;;(println "all normal")
99 (write-video! (+ 1 total-video-samples))
100 [total-audio-samples
101 (+ 1 total-video-samples)]))))
102 [0 0 0] moves))
103 (finally
104 (do
105 (println "cleanup audio.")
106 (.cleanup wave-writer))))))
107 ([initial-state moves]
108 (render-files! render-dir initial-state moves))
109 ([moves]
110 (render-files! (root) moves)))
112 (defn file-names [#^File dir]
113 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
117 ;; (defn final-cut! [^File render-dir]
118 ;; (let [movie (File. render-dir "final.ogg")
119 ;; final-audio (File. render-dir "final.wav")
120 ;; final (File. render-dir "final")]
121 ;; (.delete movie)
122 ;; (.delete final-audio)
123 ;; (clojure.java.shell/sh
124 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
125 ;; (.getCanonicalPath final-audio))
126 ;; (clojure.java.shell/sh
127 ;; "ffmpeg" "-r" "60" ;; maybe 59.7 ???!
128 ;; "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
129 ;; "-i" (.getCanonicalPath final-audio)
130 ;; "-b:a" "128k"
131 ;; "-b:v" "9000k"
132 ;; "-c:a" "libvorbis"
133 ;; "-f" "webm"
134 ;; "-g" "200"
135 ;; (.getCanonicalPath movie)) nil))
137 (defn final-cut! [^File render-dir]
138 (let [movie-with-buttons (File. render-dir "rlm-yellow-hack.avi")
139 final-audio (File. render-dir "final.mp3")
140 frames-with-buttons (File. render-dir "final")
141 frames (File. render-dir "frames")
142 movie (File. render-dir "rlm-yellow-hack-no-buttons.avi")
144 render-video
145 (fn [frames movie]
146 (.delete movie)
147 (.delete final-audio)
148 (clojure.java.shell/sh
149 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
150 (.getCanonicalPath final-audio))
151 (clojure.java.shell/sh
152 "ffmpeg"
153 "-framerate" "60"
154 "-i" (str (.getCanonicalPath frames) "/" "%07d.png")
155 "-i" (.getCanonicalPath final-audio)
156 "-b:a" "128k"
157 "-b:v" "9000k"
158 "-c:a" "libmp3lame"
159 "-c:v" "mpeg4"
160 "-r" "60"
161 (.getCanonicalPath movie)))]
163 (render-video frames-with-buttons movie-with-buttons)
164 (render-video frames movie)
165 nil))
167 (comment
169 ;; step 1
171 (render-files!
172 render-dir (root)
173 (take 9000 (first (control-checkpoint))))
175 ;; step 2
177 (generate-composite-frames!
178 render-dir
179 (take 9000 (first (control-checkpoint))))
181 ;; step 3
182 (final-cut! render-dir)