view src/com/aurellem/capture/Capture.java @ 54:6484a820e27d

wrote main documentation.
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 19:15:43 -0600
parents 3dc1f15e1e13
children b05f629fc296
line wrap: on
line source
1 package com.aurellem.capture;
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.concurrent.Callable;
7 import com.aurellem.capture.audio.MultiListener;
8 import com.aurellem.capture.audio.WaveFileWriter;
9 import com.aurellem.capture.video.AVIVideoRecorder;
10 import com.aurellem.capture.video.AbstractVideoRecorder;
11 import com.aurellem.capture.video.FileVideoRecorder;
12 import com.aurellem.capture.video.XuggleVideoRecorder;
13 import com.jme3.app.Application;
14 import com.jme3.audio.AudioRenderer;
15 import com.jme3.renderer.ViewPort;
16 import com.jme3.scene.Spatial;
17 import com.jme3.system.AppSettings;
18 import com.jme3.system.JmeSystem;
20 /**
21 * Use the methods in this class for capturing consistent, high quality video and audio from a
22 * jMonkeyEngine3 application. To capture audio or video do the following:
23 *
24 * 1.) Set your application's timer to an IsoTimer. Create the IsoTimer with the desired video
25 * frames-per-second.
26 * 2.) Call captureAudio and/or captureVideo on the Application as desired.
27 *
28 * @author Robert McIntyre
29 */
31 public class Capture {
33 /**
34 * Use this function to capture video from your application. You specify the framerate at which
35 * the video will be recording by setting the Application's timer to an IsoTimer created with the
36 * desired frames-per-second.
37 *
38 * There are three ways to record and they are selected by the properties of the file that you
39 * specify.
40 *
41 * 1.) (Preferred)
42 * If you supply an empty directory as the file, then the video will
43 * be saved as a sequence of .png files, one file per frame. The files start at
44 * 0000000.png and increment from there. You can then combine the frames into your
45 * preferred container/codec. If the directory is not empty, then writing video frames
46 * to it will fail, and nothing will be written.
47 *
48 * 2.) If the filename ends in ".avi" then the frames will be encoded as a RAW stream
49 * inside an AVI 1.0 container. The resulting file will be quite large and you will
50 * probably want to re-encode it to your preferred container/codec format. Be advised
51 * that some video payers cannot process AVI with a RAW stream, and that AVI 1.0 files
52 * generated by this method that exceed 2.0GB are invalid according to the AVI 1.0 spec
53 * (but many programs can still deal with them.) Thanks to Werner Randelshofer for his
54 * excellent work which made AVI file writer option possible.
55 *
56 * 3.) Any non-directory file ending in anything other than ".avi" will be processed through
57 * Xuggle. Xuggle provides the option to use many codecs/containers, but you will have to
58 * install it on your system yourself in order to use this option. Please visit
59 * http://www.xuggle.com/ to learn how to do this.
60 *
61 * @param app The Application from which you wish to record Video.
62 * @param file The file to which the video will be captured.
63 * @throws IOException
64 */
66 public static void captureVideo(final Application app, final File file) throws IOException{
67 final AbstractVideoRecorder videoRecorder;
69 if (file.getCanonicalPath().endsWith(".avi")){
70 videoRecorder = new AVIVideoRecorder(file);}
71 else if (file.isDirectory()){
72 videoRecorder = new FileVideoRecorder(file);}
73 else { videoRecorder = new XuggleVideoRecorder(file);}
75 Callable<Object> thunk = new Callable<Object>(){
76 public Object call(){
78 ViewPort viewPort =
79 app.getRenderManager()
80 .createPostView("aurellem video record", app.getCamera());
82 viewPort.setClearFlags(false, false, false);
84 // get GUI node stuff
85 for (Spatial s : app.getGuiViewPort().getScenes()){
86 viewPort.attachScene(s);
87 }
89 app.getStateManager().attach(videoRecorder);
90 viewPort.addProcessor(videoRecorder);
91 return null;
92 }
93 };
94 app.enqueue(thunk);
95 }
98 /**
99 * Use this function to capture audio from your application. Audio data will be saved in linear
100 * PCM format at 44,100 hertz in the wav container format to the file that you specify.
101 *
102 * Note that you *have* to use an IsoTimer for your Application's timer while recording audio or
103 * the recording will fail. Also ensure that your IsoTimer obeys the following constraints:
104 *
105 * - The frames-per-second value of the IsoTimer cannot be lower than 10 frames-per-second.
106 * - The frames-per-second value of the IsoTimer must evenly divide 44,100.
107 *
108 * @param app The Application from which you wish to record Audio.
109 * @param file the target file to which you want to record audio data.
110 * @throws IOException
111 */
113 public static void captureAudio(final Application app, final File file) throws IOException{
114 AppSettings settings = null;
115 if (app.getContext() != null){settings = app.getContext().getSettings();}
116 if (settings == null){settings = new AppSettings(true);}
117 settings.setAudioRenderer("Send");
118 app.setSettings(settings);
120 JmeSystem.setSystemDelegate(new AurellemSystemDelegate());
122 final WaveFileWriter writer = new WaveFileWriter(file);
124 Callable<Object> thunk = new Callable<Object>(){
125 public Object call(){
126 AudioRenderer ar = app.getAudioRenderer();
127 if (ar instanceof MultiListener){
128 MultiListener ml = (MultiListener)ar;
129 ml.registerSoundProcessor(writer);
130 }
131 return null;
132 }
133 };
134 app.enqueue(thunk);
135 }
136 }