Refactored to remove code duplication.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / FormatsRegistry.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.encoder;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import eu.svjatoslav.meviz.encoder.converters.Ffmpeg2theora;
16 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
17 import eu.svjatoslav.meviz.encoder.converters.AvconvVideo;
18 import eu.svjatoslav.meviz.encoder.converters.Ffmpeg;
19 import eu.svjatoslav.meviz.encoder.converters.AvconvAudio;
20 import eu.svjatoslav.meviz.encoder.converters.Ogg2Wav;
21 import eu.svjatoslav.meviz.encoder.converters.Convert;
22 import eu.svjatoslav.meviz.encoder.converters.Flac;
23
24 public class FormatsRegistry {
25
26         public ArrayList<AbstractConverter> encoders = new ArrayList<AbstractConverter>();
27
28         public FormatsRegistry() {
29                 // video conversion
30                 registerEncoder(new Ffmpeg2theora());
31                 registerEncoder(new Ffmpeg());
32                 registerEncoder(new AvconvVideo());
33
34                 // image conversion
35                 registerEncoder(new Convert());
36
37                 // audio conversion
38                 registerEncoder(new Ogg2Wav());
39                 registerEncoder(new Flac());
40                 registerEncoder(new AvconvAudio());
41         }
42
43         public List<AbstractConverter> getEncoders(final String sourceFormat,
44                         final String targetFormat) {
45
46                 final String sourceFormatLowerCase = sourceFormat.toLowerCase();
47                 final String targetFormatLowerCase = targetFormat.toLowerCase();
48
49                 final ArrayList<AbstractConverter> encoders = new ArrayList<AbstractConverter>();
50
51                 for (final AbstractConverter encoder : this.encoders)
52                         if (encoder.supportsSource(sourceFormatLowerCase))
53                                 if (encoder.supportsTarget(targetFormatLowerCase)) {
54                                         encoders.add(encoder);
55                                         return encoders;
56                                 }
57
58                 return encoders;
59         }
60
61         public void registerEncoder(final AbstractConverter encoder) {
62                 encoders.add(encoder);
63         }
64
65 }