ca.carleton.gcrc.olkit.multimedia.utils.MimeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nunaliit2-multimedia Show documentation
Show all versions of nunaliit2-multimedia Show documentation
Library that converts multimedia files using command
line tools such as avconv and ImageMagick
package ca.carleton.gcrc.olkit.multimedia.utils;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MimeUtils {
public enum MultimediaClass {
AUDIO("audio")
,VIDEO("video")
,IMAGE("image")
;
private String value;
MultimediaClass(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
static private Pattern patternAudio = Pattern.compile("audio/.*");
static private Pattern patternVideo = Pattern.compile("video/.*");
static private Pattern patternImage = Pattern.compile("image/.*");
static private Set g_audioMimeTypes = null;
static private Set g_videoMimeTypes = null;
static private Set g_imageMimeTypes = null;
static synchronized public Set getKnownAudioMimeTypes() {
if( null == g_audioMimeTypes ) {
g_audioMimeTypes = new HashSet();
g_audioMimeTypes.add("application/ogg");
}
return g_audioMimeTypes;
}
static synchronized public Set getKnownVideoMimeTypes() {
if( null == g_videoMimeTypes ) {
g_videoMimeTypes = new HashSet();
}
return g_videoMimeTypes;
}
static synchronized public Set getKnownImageMimeTypes() {
if( null == g_imageMimeTypes ) {
g_imageMimeTypes = new HashSet();
}
return g_imageMimeTypes;
}
static public void addKnownAudioMimeType(String type) {
Set known = getKnownAudioMimeTypes();
synchronized(known) {
known.add(type);
}
}
static public void addKnownVideoMimeType(String type) {
Set known = getKnownVideoMimeTypes();
synchronized(known) {
known.add(type);
}
}
static public void addKnownImageMimeType(String type) {
Set known = getKnownImageMimeTypes();
synchronized(known) {
known.add(type);
}
}
static public MultimediaClass getMultimediaClassFromMimeType(String mimeType) {
{
Matcher matcher = patternAudio.matcher(mimeType);
if( matcher.matches() ) {
return MultimediaClass.AUDIO;
}
}
{
Matcher matcher = patternVideo.matcher(mimeType);
if( matcher.matches() ) {
return MultimediaClass.VIDEO;
}
}
{
Matcher matcher = patternImage.matcher(mimeType);
if( matcher.matches() ) {
return MultimediaClass.IMAGE;
}
}
{
Set known = getKnownAudioMimeTypes();
synchronized(known) {
if( known.contains(mimeType) ) return MultimediaClass.AUDIO;
}
}
{
Set known = getKnownVideoMimeTypes();
synchronized(known) {
if( known.contains(mimeType) ) return MultimediaClass.VIDEO;
}
}
{
Set known = getKnownImageMimeTypes();
synchronized(known) {
if( known.contains(mimeType) ) return MultimediaClass.IMAGE;
}
}
return null;
}
}