Files
alfresco-community-repo/source/java/org/alfresco/repo/content/metadata/TikaAudioMetadataExtracter.java
Raluca Munteanu 8674e2bfc8 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 12:48:49 +00:00

165 lines
4.9 KiB
Java

package org.alfresco.repo.content.metadata;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import org.alfresco.repo.content.MimetypeMap;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.XMPDM;
import org.apache.tika.parser.CompositeParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mp4.MP4Parser;
import org.gagravarr.tika.FlacParser;
import org.gagravarr.tika.VorbisParser;
/**
* A Metadata Extractor which makes use of the Apache
* Tika Audio Parsers to extract metadata from your
* media files.
* For backwards compatibility reasons, this doesn't
* handle the MP3 format, which has its own dedicated
* extractor in {@link MP3MetadataExtracter}
* <pre>
* <b>author:</b> -- cm:author
* <b>title:</b> -- cm:title
* <b>created:</b> -- cm:created
* <b>xmpDM:artist</b> -- audio:artist
* <b>xmpDM:composer</b> -- audio:composer
* <b>xmpDM:engineer</b> -- audio:engineer
* <b>xmpDM:genre</b> -- audio:genre
* <b>xmpDM:trackNumber</b> -- audio:trackNumber
* <b>xmpDM:releaseDate</b> -- audio:releaseDate
* </pre>
*
* @since 4.0
* @author Nick Burch
*/
public class TikaAudioMetadataExtracter extends TikaPoweredMetadataExtracter
{
protected static final String KEY_LYRICS = "lyrics";
// The Audio related parsers we use
private static Parser[] parsers = new Parser[] {
new VorbisParser(),
new FlacParser(),
new MP4Parser()
};
// The explicit mimetypes we support (plus any others from the parsers)
public static ArrayList<String> SUPPORTED_MIMETYPES = buildSupportedMimetypes(
new String[] {
MimetypeMap.MIMETYPE_VORBIS, MimetypeMap.MIMETYPE_FLAC,
MimetypeMap.MIMETYPE_AUDIO_MP4,
}, parsers
);
protected TikaConfig tikaConfig;
public void setTikaConfig(TikaConfig tikaConfig)
{
this.tikaConfig = tikaConfig;
}
public TikaAudioMetadataExtracter()
{
this(SUPPORTED_MIMETYPES);
}
public TikaAudioMetadataExtracter(ArrayList<String> supportedMimeTypes)
{
super(supportedMimeTypes);
}
@Override
protected Parser getParser()
{
return new CompositeParser(
tikaConfig.getMediaTypeRegistry(), parsers
);
}
@Override
protected Map<String, Serializable> extractSpecific(Metadata metadata,
Map<String, Serializable> properties, Map<String,String> headers)
{
// Most things can go with the default Tika -> Alfresco Mapping
// Handle the few special cases here
// The description is special
putRawValue(KEY_DESCRIPTION, generateDescription(metadata), properties);
// The release date can be fiddly
Date releaseDate = generateReleaseDate(metadata);
putRawValue(KEY_CREATED, releaseDate, properties);
putRawValue(XMPDM.RELEASE_DATE.getName(), releaseDate, properties);
// TODO Get the Lyrics from the content
//putRawValue(KEY_LYRICS, getLyrics(), properties);
// All done
return properties;
}
/**
* Generates the release date
*/
private Date generateReleaseDate(Metadata metadata)
{
String date = metadata.get(XMPDM.RELEASE_DATE);
if(date == null || date.length() == 0)
{
return null;
}
// Is it just a year?
if(date.matches("\\d\\d\\d\\d"))
{
// Just a year, we need a full date
// Go for the 1st of the 1st
Calendar c = Calendar.getInstance();
c.set(
Integer.parseInt(date), Calendar.JANUARY, 1,
0, 0, 0
);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
// Treat as a normal date
return makeDate(date);
}
/**
* Generate the description
*
* @param metadata the metadata extracted from the file
* @return the description
*/
@SuppressWarnings("deprecation")
private String generateDescription(Metadata metadata)
{
StringBuilder result = new StringBuilder();
if (metadata.get(Metadata.TITLE) != null)
{
result.append(metadata.get(Metadata.TITLE));
if (metadata.get(XMPDM.ALBUM) != null)
{
result
.append(" - ")
.append(metadata.get(XMPDM.ALBUM));
}
if (metadata.get(XMPDM.ARTIST) != null)
{
result
.append(" (")
.append(metadata.get(XMPDM.ARTIST))
.append(")");
}
}
return result.toString();
}
}