2

How with apache tika can I detect if file is mp3? I am not looking for just file extension based detection.

I am using:

typeTika = new Tika(new TypeDetector()); 

but when I try detect a type answer is always:

     application/octet stream

(whatever I send: mp3, image etc it's always application/octet stream)

What can I do to determine if file is mp3 or not?

This question is not a duplicate. Here someone use Tika with file extension detection. This is not enough for me. I need to know if file is mp3 or not based on file type and not on a file name. I can't find any info in documentation how to do this.

TypeDetector always return application/octet stream for all files types so I want to know how to use it to get info if file is mp3 or not.

Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54
Krzysztof
  • 1,861
  • 4
  • 22
  • 33

2 Answers2

1

Taken from the Apache Tika examples:

File file = new File("/path/to/file.mp3");

Tika tika = new Tika();
String type = tika.detect(file);
System.out.println(file + " : " + type);

That will detect on both the file contents and the file name. For an MP3 file, you'll get back audio/mpeg

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
0

Apache Tika can detect the MIME Type of each file. These days it's very common to use Multipurpose Internet Mail Extensions file type designators (MIME), and each file format has got its own MIME. In the following some of them are mentioned: (For more visit iana.org or fileformats.archiveteam.org)

  • .mp3 --> audio/mpeg
  • .mp4 --> video/mp4
  • .flac --> audio/x-flac or audio/flac
  • .png --> image/png
  • .jpg --> image/jpeg
  • .pdf --> application/pdf
  • .jar --> application/java-archive

To use Tika in your project add the following maven dependency to your pom file:

<!-- Apache Tika: detects and extracts metadata and text from a variety of files -->
<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>2.0.0</version>
</dependency>

And then use the mentioned code to detect your file's MIME type.

File file = new File("/path/to/music.mp3");
Tika tika = new Tika();
String mimeType = tika.detect(file);
System.out.println(mimeType); // Prints the MIME type of the file
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54