Apache Tika - Extracting MP4 file



Example - Extracting Content and Metadata from a MP4 file

Given below is the program to extract content and metadata from a MP4 file.

TikaDemo.java

package com.tutorialspoint.tika;

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("D:/projects/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));
      }
   }  
}

Output

Given below is the snapshot of properties of example.mp4 file.

Passing MP4

After executing the program, you will get the following output.

Contents of the document:  :
Metadata of the document:
xmp:CreatorTool: Lavf58.44.100
xmpDM:audioSampleRate: 44100
X-TIKA:EXCEPTION:warn: End of data reached.
tiff:ImageLength: 1080
dcterms:created: 1904-01-01T00:00:00Z
dcterms:modified: 1904-01-01T00:00:00Z
xmpDM:audioChannelType: Stereo
tiff:ImageWidth: 1920
xmpDM:duration: 5.76
Content-Type: video/mp4
Advertisements