Mercurial > vba-clojure
view clojure/com/aurellem/run/final_cut.clj @ 603:b3e5006e23f0
cache advanced moves.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 02 Sep 2012 09:33:40 -0500 |
parents | bc2858781507 |
children | c8cda378e1a0 |
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 moves]25 (write-moves! 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 6037 seconds-per-frame (/ 1.0 desired-fps)]38 (set-state! initial-state)39 ;; clear sound buffer40 (sound-bytes)41 (try42 (dorun43 (reduce44 (fn [[total-audio-samples45 total-video-samples] move]46 (run-moves @current-state (vector move))47 (let [sound (sound-bytes)48 total-audio-samples (+ total-audio-samples49 (count sound))50 total-audio-time51 (*52 (/ total-audio-samples 4)53 (/ 44100))55 total-video-time (* total-video-samples56 seconds-per-frame)58 av-diff (- total-audio-time59 total-video-time)60 write-video!61 (fn [index]62 ;; write screenshot63 (moves-render-fn64 move65 (File. moves-dir (format "%07d.png" index)))67 (write-png!68 @current-state69 (File. frames-dir (format "%07d.png" index)))71 ;; write ram-image72 (if render-ram-map?73 (write-ram-image!74 @current-state75 (File. ram-map-dir (format "%07d.png" index)))))]76 ;;(println "audio-samples:" (count sound))78 ;; record audio79 (.process wave-writer sound gb-sound-format)81 ;; duplicate or drop frames depending on82 ;; desired-fps83 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))84 (if (< 0 av-diff)85 ;; audio has gone past video, duplicate video.86 (do (println87 "duplicating frame, av-diff is" av-diff)88 (write-video! (+ 1 total-video-samples))89 (write-video! (+ 2 total-video-samples))90 [total-audio-samples91 (+ 2 total-video-samples)])92 ;; video has gone past audio, drop frame.93 (do (println94 "dropping frame, av-diff is" av-diff)95 [total-audio-samples96 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-samples101 (+ 1 total-video-samples)]))))102 [0 0 0] moves))103 (finally104 (do105 (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/sh124 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))125 ;; (.getCanonicalPath final-audio))126 ;; (clojure.java.shell/sh127 ;; "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 (File. render-dir "final.ogg")139 final-audio (File. render-dir "final.wav")140 final (File. render-dir "final")]141 (.delete movie)142 (.delete final-audio)143 (clojure.java.shell/sh144 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))145 (.getCanonicalPath final-audio))146 (clojure.java.shell/sh147 "ffmpeg"148 "-framerate" "60"149 "-i" (str (.getCanonicalPath final) "/" "%07d.png")150 "-i" (.getCanonicalPath final-audio)151 "-b:a" "128k"152 "-b:v" "9000k"153 "-c:a" "libvorbis"154 "-c:v" "libtheora"155 "-r" "60"156 (.getCanonicalPath movie)) nil))159 (comment161 ;; step 1163 (render-files!164 render-dir (root)165 (take 9000 (first (control-checkpoint))))167 ;; step 2169 (generate-composite-frames!170 render-dir171 (take 9000 (first (control-checkpoint))))173 ;; step 3174 (final-cut! render-dir)175 )