以下是从mp4文件中提取内容和元数据的程序 :
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import org.apache.tika.exception.TikaException;import org.apache.tika.metadata.Metadata;import org.apache.tika.parser.ParseContext;import org.apache.tika.parser.mp4.MP4Parser;import org.apache.tika.sax.BodyContentHandler;import org.xml.sax.SAXException;public class Mp4Parse { public static void main(final String[] args) throws IOException,SAXException, TikaException { //detecting the file type BodyContentHandler handler = new BodyContentHandler(); Metadata metadata = new Metadata(); FileInputStream inputstream = new FileInputStream(new File("example.mp4")); ParseContext pcontext = new ParseContext(); //Html parser MP4Parser MP4Parser = new MP4Parser(); MP4Parser.parse(inputstream, handler, metadata,pcontext); System.out.println("Contents of the document: :" + handler.toString()); System.out.println("Metadata of the document:"); String[] metadataNames = metadata.names(); for(String name : metadataNames) { System.out.println(name + ": " + metadata.get(name)); } } }
将上述代码保存为JpegParse.java,并从命令提示符处编译使用以下命令 :
javac Mp4Parse.javajava Mp4Parse
以下是Example.mp4文件属性的快照.
执行上述程序后,您将得到以下输出 :
输出 :
Contents of the document:Metadata of the document:dcterms:modified: 2014-01-06T12:10:27Zmeta:creation-date: 1904-01-01T00:00:00Zmeta:save-date: 2014-01-06T12:10:27ZLast-Modified: 2014-01-06T12:10:27Zdcterms:created: 1904-01-01T00:00:00Zdate: 2014-01-06T12:10:27Ztiff:ImageLength: 360modified: 2014-01-06T12:10:27ZCreation-Date: 1904-01-01T00:00:00Ztiff:ImageWidth: 640Content-Type: video/mp4Last-Save-Date: 2014-01-06T12:10:27Z