Created
June 20, 2012 20:22
-
-
Save jjttjj/2961994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;from http://stackoverflow.com/questions/502753/programmatically-convert-a-video-to-flv | |
(defn readerRecurse | |
"calls .readPacket until there's nothing left to do2" | |
[reader] | |
(if (not (nil? (.readPacket reader))) ; here .readPacket actually does the processing as a side-effect. | |
true ; it returns null when it has MORE ro process, and signals an error when done... | |
(recur reader))) | |
(defn generate-snapshots [filename destination n] | |
(let [vid-stream-idx (atom -1) | |
last-pts-write (atom Global/NO_PTS) | |
container (doto (IContainer/make) | |
(.open filename IContainer$Type/READ nil)) | |
duration (/ (.getDuration container) Global/DEFAULT_PTS_PER_SECOND) | |
seconds-between-frames (long (/ duration n)) | |
microseconds-between-frames (long (* Global/DEFAULT_PTS_PER_SECOND seconds-between-frames)) | |
reader (ToolFactory/makeReader container)] | |
(doto reader | |
(.setBufferedImageTypeToGenerate BufferedImage/TYPE_3BYTE_BGR) | |
(.addListener | |
(proxy [MediaListenerAdapter] [] | |
(onVideoPicture [event] | |
(try | |
(if (not= (.getStreamIndex event) @vid-stream-idx) | |
(if (= -1 @vid-stream-idx) | |
(reset! vid-stream-idx (.getStreamIndex event)))) | |
(if (= @last-pts-write Global/NO_PTS) | |
(reset! last-pts-write (- (.getTimeStamp event) microseconds-between-frames))) | |
(when (>= (- (.getTimeStamp event) @last-pts-write) microseconds-between-frames) | |
(ImageIO/write (.getImage event) "jpg" | |
(File/createTempFile "frame" ".jpg" | |
(File. destination))) | |
(swap! last-pts-write #(+ % microseconds-between-frames))) | |
(catch Exception e (str "caught exception: " (.getMessage e)))))))) | |
(readerRecurse reader))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment