annotate clojure/com/aurellem/run/final_cut.clj @ 620:1b52b14868d3 tip

merge.
author rlm <rlm@aurellem.org>
date Sun, 07 Feb 2016 23:16:04 -0800
parents 743b7b68e74e
children
rev   line source
rlm@530 1 (ns com.aurellem.run.final-cut
rlm@530 2 (:use (com.aurellem.gb saves gb-driver util constants
rlm@530 3 items vbm characters money
rlm@530 4 rlm-assembly))
rlm@530 5 (:use (com.aurellem.run util sound music title save-corruption
rlm@530 6 bootstrap-0 bootstrap-1
rlm@530 7 ram-display
rlm@530 8 ))
rlm@530 9 (:require clojure.string)
rlm@530 10 (:import [com.aurellem.gb.gb_driver SaveState])
rlm@530 11 (:import java.awt.image.BufferedImage)
rlm@530 12 (:import java.io.File)
rlm@530 13 (:import com.aurellem.gb.WaveWriter))
rlm@530 14
rlm@530 15
rlm@542 16 (def render-dir (File. user-home "proj/vba-clojure/render/test"))
rlm@530 17
rlm@544 18
rlm@530 19 (defn render-files!
rlm@599 20 ([^File target-dir
rlm@599 21 moves-render-fn
rlm@599 22 render-ram-map?
rlm@599 23 initial-state
rlm@601 24 moves]
rlm@604 25 (write-moves-to-file! moves (File. target-dir "rlm-yellow-hack.vbm"))
rlm@530 26 (let [ram-map-dir (File. target-dir "ram-map")
rlm@530 27 frames-dir (File. target-dir "frames")
rlm@546 28 moves-dir (File. target-dir "moves")
rlm@530 29 audio-file (File. target-dir "audio.wav")
rlm@533 30 _ (.mkdir target-dir)
rlm@601 31 _ (if render-ram-map? (.mkdir ram-map-dir))
rlm@533 32 _ (.mkdir frames-dir)
rlm@546 33 _ (.mkdir moves-dir)
rlm@530 34 wave-writer (WaveWriter. audio-file)
rlm@544 35 moves (vec moves)
rlm@544 36 desired-fps 60
rlm@547 37 seconds-per-frame (/ 1.0 desired-fps)]
rlm@530 38 (set-state! initial-state)
rlm@544 39 ;; clear sound buffer
rlm@544 40 (sound-bytes)
rlm@544 41 (try
rlm@544 42 (dorun
rlm@544 43 (reduce
rlm@544 44 (fn [[total-audio-samples
rlm@544 45 total-video-samples] move]
rlm@544 46 (run-moves @current-state (vector move))
rlm@544 47 (let [sound (sound-bytes)
rlm@544 48 total-audio-samples (+ total-audio-samples
rlm@544 49 (count sound))
rlm@544 50 total-audio-time
rlm@544 51 (*
rlm@544 52 (/ total-audio-samples 4)
rlm@544 53 (/ 44100))
rlm@544 54
rlm@544 55 total-video-time (* total-video-samples
rlm@544 56 seconds-per-frame)
rlm@530 57
rlm@544 58 av-diff (- total-audio-time
rlm@544 59 total-video-time)
rlm@544 60 write-video!
rlm@544 61 (fn [index]
rlm@544 62 ;; write screenshot
rlm@599 63 (moves-render-fn
rlm@546 64 move
rlm@546 65 (File. moves-dir (format "%07d.png" index)))
rlm@544 66
rlm@544 67 (write-png!
rlm@544 68 @current-state
rlm@544 69 (File. frames-dir (format "%07d.png" index)))
rlm@544 70
rlm@544 71 ;; write ram-image
rlm@599 72 (if render-ram-map?
rlm@599 73 (write-ram-image!
rlm@599 74 @current-state
rlm@599 75 (File. ram-map-dir (format "%07d.png" index)))))]
rlm@544 76 ;;(println "audio-samples:" (count sound))
rlm@544 77
rlm@544 78 ;; record audio
rlm@544 79 (.process wave-writer sound gb-sound-format)
rlm@544 80
rlm@544 81 ;; duplicate or drop frames depending on
rlm@544 82 ;; desired-fps
rlm@544 83 (if (> (Math/abs av-diff) (* 2 seconds-per-frame))
rlm@544 84 (if (< 0 av-diff)
rlm@544 85 ;; audio has gone past video, duplicate video.
rlm@544 86 (do (println
rlm@544 87 "duplicating frame, av-diff is" av-diff)
rlm@544 88 (write-video! (+ 1 total-video-samples))
rlm@544 89 (write-video! (+ 2 total-video-samples))
rlm@544 90 [total-audio-samples
rlm@544 91 (+ 2 total-video-samples)])
rlm@544 92 ;; video has gone past audio, drop frame.
rlm@544 93 (do (println
rlm@544 94 "dropping frame, av-diff is" av-diff)
rlm@544 95 [total-audio-samples
rlm@544 96 total-video-samples]))
rlm@544 97 ;; no frame dropping or duplication required.
rlm@544 98 (do ;;(println "all normal")
rlm@544 99 (write-video! (+ 1 total-video-samples))
rlm@544 100 [total-audio-samples
rlm@544 101 (+ 1 total-video-samples)]))))
rlm@544 102 [0 0 0] moves))
rlm@544 103 (finally
rlm@544 104 (do
rlm@544 105 (println "cleanup audio.")
rlm@544 106 (.cleanup wave-writer))))))
rlm@544 107 ([initial-state moves]
rlm@544 108 (render-files! render-dir initial-state moves))
rlm@544 109 ([moves]
rlm@544 110 (render-files! (root) moves)))
rlm@530 111
rlm@531 112 (defn file-names [#^File dir]
rlm@531 113 (mapv #(.getCanonicalPath %) (next (sort (file-seq dir)))))
rlm@531 114
rlm@531 115
rlm@532 116
rlm@549 117 ;; (defn final-cut! [^File render-dir]
rlm@549 118 ;; (let [movie (File. render-dir "final.ogg")
rlm@549 119 ;; final-audio (File. render-dir "final.wav")
rlm@549 120 ;; final (File. render-dir "final")]
rlm@549 121 ;; (.delete movie)
rlm@549 122 ;; (.delete final-audio)
rlm@549 123 ;; (clojure.java.shell/sh
rlm@549 124 ;; "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
rlm@549 125 ;; (.getCanonicalPath final-audio))
rlm@549 126 ;; (clojure.java.shell/sh
rlm@549 127 ;; "ffmpeg" "-r" "60" ;; maybe 59.7 ???!
rlm@549 128 ;; "-i" (str (.getCanonicalPath final) "/" "%07d.bmp")
rlm@549 129 ;; "-i" (.getCanonicalPath final-audio)
rlm@549 130 ;; "-b:a" "128k"
rlm@549 131 ;; "-b:v" "9000k"
rlm@549 132 ;; "-c:a" "libvorbis"
rlm@549 133 ;; "-f" "webm"
rlm@549 134 ;; "-g" "200"
rlm@549 135 ;; (.getCanonicalPath movie)) nil))
rlm@549 136
rlm@532 137 (defn final-cut! [^File render-dir]
rlm@607 138 (let [movie-with-buttons (File. render-dir "rlm-yellow-hack.avi")
rlm@605 139 final-audio (File. render-dir "final.mp3")
rlm@607 140 frames-with-buttons (File. render-dir "final")
rlm@607 141 frames (File. render-dir "frames")
rlm@607 142 movie (File. render-dir "rlm-yellow-hack-no-buttons.avi")
rlm@549 143
rlm@607 144 render-video
rlm@607 145 (fn [frames movie]
rlm@607 146 (.delete movie)
rlm@607 147 (.delete final-audio)
rlm@607 148 (clojure.java.shell/sh
rlm@607 149 "sox" (.getCanonicalPath (File. render-dir "audio.wav"))
rlm@607 150 (.getCanonicalPath final-audio))
rlm@607 151 (clojure.java.shell/sh
rlm@607 152 "ffmpeg"
rlm@607 153 "-framerate" "60"
rlm@607 154 "-i" (str (.getCanonicalPath frames) "/" "%07d.png")
rlm@607 155 "-i" (.getCanonicalPath final-audio)
rlm@607 156 "-b:a" "128k"
rlm@607 157 "-b:v" "9000k"
rlm@607 158 "-c:a" "libmp3lame"
rlm@607 159 "-c:v" "mpeg4"
rlm@607 160 "-r" "60"
rlm@607 161 (.getCanonicalPath movie)))]
rlm@607 162
rlm@607 163 (render-video frames-with-buttons movie-with-buttons)
rlm@607 164 (render-video frames movie)
rlm@607 165 nil))
rlm@531 166
rlm@532 167 (comment
rlm@531 168
rlm@532 169 ;; step 1
rlm@532 170
rlm@532 171 (render-files!
rlm@532 172 render-dir (root)
rlm@532 173 (take 9000 (first (control-checkpoint))))
rlm@531 174
rlm@532 175 ;; step 2
rlm@532 176
rlm@532 177 (generate-composite-frames!
rlm@532 178 render-dir
rlm@532 179 (take 9000 (first (control-checkpoint))))
rlm@532 180
rlm@532 181 ;; step 3
rlm@532 182 (final-cut! render-dir)
rlm@532 183 )