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