Mercurial > vba-clojure
view clojure/com/aurellem/run/final_cut.clj @ 599:4503899241e6
going to render the advanced choreo.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 02 Sep 2012 09:18:43 -0500 |
parents | 6ce1d5fd4911 |
children | 31128a10ee28 |
line wrap: on
line source
1 (ns com.aurellem.run.final-cut2 (:use (com.aurellem.gb saves gb-driver util constants3 items vbm characters money4 rlm-assembly))5 (:use (com.aurellem.run util sound music title save-corruption6 bootstrap-0 bootstrap-17 ram-display8 ))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-dir21 moves-render-fn22 render-ram-map?23 initial-state24 moves26 ]27 (let [ram-map-dir (File. target-dir "ram-map")28 frames-dir (File. target-dir "frames")29 moves-dir (File. target-dir "moves")30 audio-file (File. target-dir "audio.wav")31 _ (.mkdir target-dir)32 _ (.mkdir ram-map-dir)33 _ (.mkdir frames-dir)34 _ (.mkdir moves-dir)35 wave-writer (WaveWriter. audio-file)36 moves (vec moves)37 desired-fps 6038 seconds-per-frame (/ 1.0 desired-fps)]39 (set-state! initial-state)40 ;; clear sound buffer41 (sound-bytes)42 (try43 (dorun44 (reduce45 (fn [[total-audio-samples46 total-video-samples] move]47 (run-moves @current-state (vector move))48 (let [sound (sound-bytes)49 total-audio-samples (+ total-audio-samples50 (count sound))51 total-audio-time52 (*53 (/ total-audio-samples 4)54 (/ 44100))56 total-video-time (* total-video-samples57 seconds-per-frame)59 av-diff (- total-audio-time60 total-video-time)61 write-video!62 (fn [index]63 ;; write screenshot64 (moves-render-fn65 move66 (File. moves-dir (format "%07d.png" index)))68 (write-png!69 @current-state70 (File. frames-dir (format "%07d.png" index)))72 ;; write ram-image73 (if render-ram-map?74 (write-ram-image!75 @current-state76 (File. ram-map-dir (format "%07d.png" index)))))]77 ;;(println "audio-samples:" (count sound))79 ;; record audio80 (.process wave-writer sound gb-sound-format)82 ;; duplicate or drop frames depending on83 ;; desired-fps84 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))85 (if (< 0 av-diff)86 ;; audio has gone past video, duplicate video.87 (do (println88 "duplicating frame, av-diff is" av-diff)89 (write-video! (+ 1 total-video-samples))90 (write-video! (+ 2 total-video-samples))91 [total-audio-samples92 (+ 2 total-video-samples)])93 ;; video has gone past audio, drop frame.94 (do (println95 "dropping frame, av-diff is" av-diff)96 [total-audio-samples97 total-video-samples]))98 ;; no frame dropping or duplication required.99 (do ;;(println "all normal")100 (write-video! (+ 1 total-video-samples))101 [total-audio-samples102 (+ 1 total-video-samples)]))))103 [0 0 0] moves))104 (finally105 (do106 (println "cleanup audio.")107 (.cleanup wave-writer))))))108 ([initial-state moves]109 (render-files! render-dir initial-state moves))110 ([moves]111 (render-files! (root) moves)))113 (defn file-names [#^File dir]114 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))118 ;; (defn final-cut! [^File render-dir]119 ;; (let [movie (File. render-dir "final.ogg")120 ;; final-audio (File. render-dir "final.wav")121 ;; final (File. render-dir "final")]122 ;; (.delete movie)123 ;; (.delete final-audio)124 ;; (clojure.java.shell/sh125 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))126 ;; (.getCanonicalPath final-audio))127 ;; (clojure.java.shell/sh128 ;; "ffmpeg" "-r" "60" ;; maybe 59.7 ???!129 ;; "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")130 ;; "-i" (.getCanonicalPath final-audio)131 ;; "-b:a" "128k"132 ;; "-b:v" "9000k"133 ;; "-c:a" "libvorbis"134 ;; "-f" "webm"135 ;; "-g" "200"136 ;; (.getCanonicalPath movie)) nil))138 (defn final-cut! [^File render-dir]139 (let [movie (File. render-dir "final.ogg")140 final-audio (File. render-dir "final.wav")141 final (File. render-dir "final")]142 (.delete movie)143 (.delete final-audio)144 (clojure.java.shell/sh145 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))146 (.getCanonicalPath final-audio))147 (clojure.java.shell/sh148 "ffmpeg"149 "-framerate" "60"150 "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")151 "-i" (.getCanonicalPath final-audio)152 "-b:a" "128k"153 "-b:v" "9000k"154 "-c:a" "libvorbis"155 "-c:v" "libtheora"156 "-r" "60"157 (.getCanonicalPath movie)) nil))160 (comment162 ;; step 1164 (render-files!165 render-dir (root)166 (take 9000 (first (control-checkpoint))))168 ;; step 2170 (generate-composite-frames!171 render-dir172 (take 9000 (first (control-checkpoint))))174 ;; step 3175 (final-cut! render-dir)176 )