Skip to content

Instantly share code, notes, and snippets.

@jjttjj
Created June 20, 2012 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjttjj/2961994 to your computer and use it in GitHub Desktop.
Save jjttjj/2961994 to your computer and use it in GitHub Desktop.
;;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