-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
FrameRecorder.java
316 lines (279 loc) · 9.86 KB
/
FrameRecorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright (C) 2009,2010,2011,2012,2013 Samuel Audet
*
* This file is part of JavaCV.
*
* JavaCV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCV. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bytedeco.javacv;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author Samuel Audet
*/
public abstract class FrameRecorder {
public static final List<String> list = new LinkedList<String>(Arrays.asList(new String[] { "FFmpeg", "OpenCV" }));
public static void init() {
for (String name : list) {
try {
Class<? extends FrameRecorder> c = get(name);
c.getMethod("tryLoad").invoke(null);
} catch (Throwable t) { }
}
}
public static Class<? extends FrameRecorder> getDefault() {
// select first frame recorder that can load..
for (String name : list) {
try {
Class<? extends FrameRecorder> c = get(name);
c.getMethod("tryLoad").invoke(null);
return c;
} catch (Throwable t) { }
}
return null;
}
public static Class<? extends FrameRecorder> get(String className) throws Exception {
className = FrameRecorder.class.getPackage().getName() + "." + className;
try {
return Class.forName(className).asSubclass(FrameRecorder.class);
} catch (ClassNotFoundException e) {
String className2 = className + "FrameRecorder";
try {
return Class.forName(className2).asSubclass(FrameRecorder.class);
} catch (ClassNotFoundException ex) {
throw new Exception("Could not get FrameRecorder class for " + className + " or " + className2, e);
}
}
}
public static FrameRecorder create(Class<? extends FrameRecorder> c, Class p, Object o, int w, int h) throws Exception {
Throwable cause = null;
try {
return c.getConstructor(p, int.class, int.class).newInstance(o, w, h);
} catch (InstantiationException ex) {
cause = ex;
} catch (IllegalAccessException ex) {
cause = ex;
} catch (IllegalArgumentException ex) {
cause = ex;
} catch (NoSuchMethodException ex) {
cause = ex;
} catch (InvocationTargetException ex) {
cause = ex.getCause();
}
throw new Exception("Could not create new " + c.getSimpleName() + "(" + o + ", " + w + ", " + h + ")", cause);
}
public static FrameRecorder createDefault(File file, int width, int height) throws Exception {
return create(getDefault(), File.class, file, width, height);
}
public static FrameRecorder createDefault(String filename, int width, int height) throws Exception {
return create(getDefault(), String.class, filename, width, height);
}
public static FrameRecorder create(String className, File file, int width, int height) throws Exception {
return create(get(className), File.class, file, width, height);
}
public static FrameRecorder create(String className, String filename, int width, int height) throws Exception {
return create(get(className), String.class, filename, width, height);
}
protected String format, videoCodecName, audioCodecName;
protected int imageWidth, imageHeight, audioChannels;
protected int pixelFormat, videoCodec, videoBitrate, gopSize = -1;
protected double aspectRatio, frameRate, videoQuality = -1;
protected int sampleFormat, audioCodec, audioBitrate, sampleRate;
protected double audioQuality = -1;
protected boolean interleaved;
protected HashMap<String, String> options = new HashMap<String, String>();
protected HashMap<String, String> videoOptions = new HashMap<String, String>();
protected HashMap<String, String> audioOptions = new HashMap<String, String>();
protected HashMap<String, String> metadata = new HashMap<String, String>();
protected HashMap<String, String> videoMetadata = new HashMap<String, String>();
protected HashMap<String, String> audioMetadata = new HashMap<String, String>();
protected int frameNumber = 0;
protected long timestamp = 0;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getVideoCodecName() {
return videoCodecName;
}
public void setVideoCodecName(String videoCodecName) {
this.videoCodecName = videoCodecName;
}
public String getAudioCodecName() {
return audioCodecName;
}
public void setAudioCodecName(String audioCodecName) {
this.audioCodecName = audioCodecName;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public int getAudioChannels() {
return audioChannels;
}
public void setAudioChannels(int audioChannels) {
this.audioChannels = audioChannels;
}
public int getPixelFormat() {
return pixelFormat;
}
public void setPixelFormat(int pixelFormat) {
this.pixelFormat = pixelFormat;
}
public int getVideoCodec() {
return videoCodec;
}
public void setVideoCodec(int videoCodec) {
this.videoCodec = videoCodec;
}
public int getVideoBitrate() {
return videoBitrate;
}
public void setVideoBitrate(int videoBitrate) {
this.videoBitrate = videoBitrate;
}
public int getGopSize() {
return gopSize;
}
public void setGopSize(int gopSize) {
this.gopSize = gopSize;
}
public double getAspectRatio() {
return aspectRatio;
}
public void setAspectRatio(double aspectRatio) {
this.aspectRatio = aspectRatio;
}
public double getFrameRate() {
return frameRate;
}
public void setFrameRate(double frameRate) {
this.frameRate = frameRate;
}
public double getVideoQuality() {
return videoQuality;
}
public void setVideoQuality(double videoQuality) {
this.videoQuality = videoQuality;
}
public int getSampleFormat() {
return sampleFormat;
}
public void setSampleFormat(int sampleFormat) {
this.sampleFormat = sampleFormat;
}
public int getAudioCodec() {
return audioCodec;
}
public void setAudioCodec(int audioCodec) {
this.audioCodec = audioCodec;
}
public int getAudioBitrate() {
return audioBitrate;
}
public void setAudioBitrate(int audioBitrate) {
this.audioBitrate = audioBitrate;
}
public int getSampleRate() {
return sampleRate;
}
public void setSampleRate(int sampleRate) {
this.sampleRate = sampleRate;
}
public double getAudioQuality() {
return audioQuality;
}
public void setAudioQuality(double audioQuality) {
this.audioQuality = audioQuality;
}
public boolean isInterleaved() {
return interleaved;
}
public void setInterleaved(boolean interleaved) {
this.interleaved = interleaved;
}
public String getOption(String key) {
return options.get(key);
}
public void setOption(String key, String value) {
options.put(key, value);
}
public String getVideoOption(String key) {
return videoOptions.get(key);
}
public void setVideoOption(String key, String value) {
videoOptions.put(key, value);
}
public String getAudioOption(String key) {
return audioOptions.get(key);
}
public void setAudioOption(String key, String value) {
audioOptions.put(key, value);
}
public String getMetadata(String key) {
return metadata.get(key);
}
public void setMetadata(String key, String value) {
metadata.put(key, value);
}
public String getVideoMetadata(String key) {
return videoMetadata.get(key);
}
public void setVideoMetadata(String key, String value) {
videoMetadata.put(key, value);
}
public String getAudioMetadata(String key) {
return audioMetadata.get(key);
}
public void setAudioMetadata(String key, String value) {
audioMetadata.put(key, value);
}
public int getFrameNumber() {
return frameNumber;
}
public void setFrameNumber(int frameNumber) {
this.frameNumber = frameNumber;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public static class Exception extends java.lang.Exception {
public Exception(String message) { super(message); }
public Exception(String message, Throwable cause) { super(message, cause); }
}
public abstract void start() throws Exception;
public abstract void stop() throws Exception;
public abstract void record(Frame frame) throws Exception;
public abstract void release() throws Exception;
}