/
Configuration.java
323 lines (301 loc) · 10.1 KB
/
Configuration.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
317
318
319
320
321
322
323
/*******************************************************************************
* Copyright (c) 2008, 2010 Xuggle Inc. All rights reserved.
*
* This file is part of Xuggle-Xuggler-Main.
*
* Xuggle-Xuggler-Main is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xuggle-Xuggler-Main 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Xuggle-Xuggler-Main. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package com.xuggle.xuggler;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A global configuration class for Xuggler.
* <p>
* This object can help print setting options
* on {@link IConfigurable} classes, and also
* provides a convenient method {@link #configure(Properties, IConfigurable)}
* that lets you configure {@link IConfigurable} objects from
* a Java properties or FFmpeg preset file.
* </p>
* @see #configure(Properties, IConfigurable)
* @see IConfigurable
*
* @author aclarke
*
*/
public class Configuration
{
static private final Logger log = LoggerFactory.getLogger(Configuration.class);
/** don't allow construction */
private Configuration() {}
/**
* A main program that just prints information on all avaiable
* Xuggler options.
* @param args ignored
*/
public static void main(String[] args)
{
printHelp(System.out);
}
/**
* Print out help about this Xuggler installation.
*
* Specifically all installed and supported container formats, installed
* and supported codecs, all {@link IContainer} options,
* all {@link IStreamCoder} options and all {@link IVideoResampler}
* options.
* @param stream stream to print to
*/
public static void printHelp(java.io.PrintStream stream)
{
printSupportedContainerFormats(stream);
printSupportedCodecs(stream);
printSupportedContainerProperties(stream);
printSupportedStreamCoderProperties(stream);
printSupportedVideoResamplerProperties(stream);
}
/**
* Print out all installed and supported container formats on this system.
* @param stream stream to print to
*/
public static void printSupportedContainerFormats(java.io.PrintStream stream)
{
stream.println("=======================================");
stream.println(" Demuxable Formats");
stream.println("=======================================");
Collection<IContainerFormat> inputFormats = IContainerFormat.getInstalledInputFormats();
for(IContainerFormat inputFormat : inputFormats)
{
stream.printf(" \"%s\": %s\n", inputFormat.getInputFormatShortName(),
inputFormat.getInputFormatLongName());
}
stream.println("=======================================");
stream.println(" Muxable Formats");
stream.println("=======================================");
Collection<IContainerFormat> outputFormats = IContainerFormat.getInstalledOutputFormats();
for(IContainerFormat outputFormat : outputFormats)
{
stream.printf(" \"%s\": %s\n", outputFormat.getOutputFormatShortName(),
outputFormat.getOutputFormatLongName());
}
}
/**
* Print out all installed and supported codecs on this system.
* @param stream stream to print to
*/
public static void printSupportedCodecs(java.io.PrintStream stream)
{
Collection<ICodec> codecs = ICodec.getInstalledCodecs();
stream.println("=======================================");
stream.println(" Decodeable Codecs");
stream.println("=======================================");
for(ICodec codec : codecs)
{
if (codec.canDecode())
{
stream.printf("%s %s (%s): %s\n",
codec.getType(),
codec.getID(),
codec.getName(),
codec.getLongName());
}
}
stream.println("=======================================");
stream.println(" Encodeable Codecs");
stream.println("=======================================");
for(ICodec codec : codecs)
{
if (codec.canEncode())
{
stream.printf("%s %s (%s): %s\n",
codec.getType(),
codec.getID(),
codec.getName(),
codec.getLongName());
}
}
}
/**
* Print out all {@link IVideoResampler} supported properties, if
* that option is supported in this version of Xuggler.
*
* @param stream stream to print to.
*/
public static void printSupportedVideoResamplerProperties(java.io.PrintStream stream)
{
if (IVideoResampler.isSupported(IVideoResampler.Feature.FEATURE_COLORSPACECONVERSION))
printConfigurable(stream, IVideoResampler.make(100,
100,
IPixelFormat.Type.YUV420P,
200,
200,
IPixelFormat.Type.RGB24));
}
/**
* Print out all {@link IStreamCoder} supported properties.
*
* @param stream stream to print to.
*/
public static void printSupportedStreamCoderProperties(java.io.PrintStream stream)
{
printConfigurable(stream, IStreamCoder.make(IStreamCoder.Direction.ENCODING, (ICodec)null));
}
/**
* Print out all {@link IContainer} supported properties.
*
* @param stream stream to print to.
*/
public static void printSupportedContainerProperties(java.io.PrintStream stream)
{
printConfigurable(stream, IContainer.make());
}
/**
* Print out all configurable options on the {@link IConfigurable} object.
*
* @param stream stream to print to
* @param configObj {@link IConfigurable} object.
*/
public static void printConfigurable(java.io.PrintStream stream,
IConfigurable configObj)
{
stream.println("=======================================");
stream.println(" " + configObj.getClass().getName() + " Properties");
stream.println("=======================================");
int numOptions = configObj.getNumProperties();
for(int i = 0; i < numOptions; i++)
{
IProperty prop = configObj.getPropertyMetaData(i);
printOption(stream, configObj, prop);
}
}
/**
* Print information about the property on the configurable object.
*
* @param stream stream to print to
* @param configObj configurable object
* @param prop property on object
*/
public static void printOption(java.io.PrintStream stream,
IConfigurable configObj, IProperty prop)
{
if (prop.getType() != IProperty.Type.PROPERTY_FLAGS)
{
stream.printf(" %s; default= %s; type=%s;\n",
prop.getName(),
configObj.getPropertyAsString(prop.getName()),
prop.getType());
} else {
// it's a flag
stream.printf(" %s; default= %d; valid values=(",
prop.getName(),
configObj.getPropertyAsLong(prop.getName()));
int numSettings = prop.getNumFlagSettings();
long value = configObj.getPropertyAsLong(prop.getName());
for(int i = 0; i < numSettings; i++)
{
IProperty fprop = prop.getFlagConstant(i);
long flagMask = fprop.getDefault();
boolean isSet = (value & flagMask)>0;
stream.printf("%s%s; ",
isSet ? "+" : "-",
fprop.getName());
}
stream.printf("); type=%s;\n", prop.getType());
}
stream.printf(" help for %s: %s\n",
prop.getName(),
prop.getHelp() == null ? "no help available" : prop.getHelp());
}
/**
* Configures an {@link IConfigurable} from a set of Java {@link Properties}.
* <p>
* Here's some sample code that shows configuring a IStreamCoder
* from a FFmpeg preset file:
* </p>
* <pre>
* Properties props = new Properties();
* props.load(new FileInputStream(file));
* IStreamCoder coder = IStreamCoder.make(Direction.ENCODING);
* int retval = Configuration.configure(props, coder);
* if (retval < 0)
* throw new RuntimeException("Could not configure object");
* </pre>
* @param properties The properties to use.
* @param config The item to configure.
* @return <0 on error; >= 0 on success.
* @since 3.4
*/
@SuppressWarnings("unchecked")
public static int configure(final Properties properties, final IConfigurable config)
{
for (
final Enumeration<String> names = (Enumeration<String>) properties.propertyNames();
names.hasMoreElements();
)
{
final String name = names.nextElement();
final String value = properties.getProperty(name);
if (value != null) {
final int retval = config.setProperty(name, value);
if (retval < 0) {
log.warn("Invalid property on object {}; name=\"{}\"; value=\"{}\"",
new Object[]{
config,
name,
value
});
return retval;
}
}
}
return 0;
}
/**
* Configures an {@link IConfigurable} from a file.
* <p>
* This is a handy way to configure a Xuggler {@link IConfigurable}
* object, and will also work with FFmpeg preset files.
* </p>
* @param file A filename for the properties file.
* @param config The item to configure.
* @return <0 on error; >= 0 on success.
* @since 3.4
*/
@SuppressWarnings("unchecked")
public static int configure(final String file, final IConfigurable config)
{
Properties props = new Properties();
try
{
props.load(new FileInputStream(file));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return -1;
}
catch (IOException e)
{
e.printStackTrace();
return -1;
}
return Configuration.configure(props, config);
}
}