LoginSignup
1
0

More than 3 years have passed since last update.

【Java】Apathce Tikaでファイルからメタデータを取得し、メタデータから画像・動画のwidthとheightを取得する【Kotlin】

Last updated at Posted at 2019-08-04

やりたいこと

画像・動画のフレームサイズ(widthとheight)を取得します。

この記事では、Apache Tikaでメタデータを取得し、そのメタデータからフレームサイズを取得する方法で行います。

お詫び

上記公式ドキュメントを読む限り、この記事で紹介するやり方ではpng、gif、bmpなど、Javaプラットフォームでサポートされている画像形式(+ 試した限りはjpeg)と、flv、mp4形式の動画にしか対応できません。
後述する実行結果内に登場するmovなど、Content-typevideo/mp4扱いされるような内容であってもこれは変わりません。

調査した感じ、ほぼ確実にフレームサイズを取得する方法で、Java内で完結するものは今の所無いように見えました(有れば教えていただけるとありがたいです)。
確実にやるのであれば、ffmpegをJavaから呼ぶ形がいいのかなと思います。

導入方法

MavenからApache Tika Parsersを導入します。
検証には1.21を使いました。

Apache Tika Parsers

検証用コード

雑ですが、src/main/resources配下のファイルすべてについてフレームサイズを取得するサンプルです。
拡張子別にソートして出力しています。

org.apache.tika.metadata.Metadata型はKotlinのMetadata型と名前が被るため、型に名前を付けています。

import org.apache.tika.Tika
import org.apache.tika.io.TikaInputStream
import java.io.File
import org.apache.tika.metadata.Metadata as TikaMetadata

fun main() {
    val tika = Tika()

    File(System.getProperty("user.dir") + "/src/main/resources").listFiles().filter {
        it.name != ".gitkeep"
    }.map {
        val metaData = TikaMetadata() // 1ファイルごとに宣言しなければ、前回判定したデータが延々残ってしまう
        val tikaStream = TikaInputStream.get(it.toURI(), metaData)

        tika.parse(tikaStream, metaData)

        val fileInfo = """
            Extension: ${metaData.get("resourceName").split(".").last().toLowerCase()}
            Content-Type = ${metaData.get("Content-Type")}
        """.trimIndent()

        val width = metaData.get("tiff:ImageWidth") ?.let { w -> Integer.parseInt(w) }
        val height = metaData.get("tiff:ImageLength") ?.let { h -> Integer.parseInt(h) }

        """
            #$fileInfo
            #width: ${width?: "nothing" }
            #height: ${height?: "nothing"}
        """.trimMargin("#")
    }.sorted().forEach {
        println(it)
        println("----------------------")
    }
}

実行結果

テキトーに拾ってきたデータを入れた結果です。

長いので折りたたんでます
Extension: 7z
Content-Type = application/x-7z-compressed
width: nothing
height: nothing
----------------------
Extension: avi
Content-Type = video/x-msvideo
width: nothing
height: nothing
----------------------
Extension: docx
Content-Type = application/vnd.openxmlformats-officedocument.wordprocessingml.document
width: nothing
height: nothing
----------------------
Extension: exe
Content-Type = application/x-msdownload
width: nothing
height: nothing
----------------------
Extension: flv
Content-Type = video/x-flv
width: nothing
height: nothing
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: gif
Content-Type = image/gif
width: 300
height: 250
----------------------
Extension: html
Content-Type = text/html; charset=Shift_JIS
width: nothing
height: nothing
----------------------
Extension: jpg
Content-Type = image/jpeg
width: 4608
height: 2592
----------------------
Extension: jpg
Content-Type = image/jpeg
width: 759
height: 1074
----------------------
Extension: m3u
Content-Type = text/plain; charset=windows-1252
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mkv
Content-Type = video/x-matroska
width: nothing
height: nothing
----------------------
Extension: mov
Content-Type = application/mp4
width: 0
height: 0
----------------------
Extension: mov
Content-Type = application/mp4
width: 0
height: 0
----------------------
Extension: mov
Content-Type = video/quicktime
width: 0
height: 0
----------------------
Extension: mov
Content-Type = video/quicktime
width: 320
height: 240
----------------------
Extension: mp3
Content-Type = audio/mpeg
width: nothing
height: nothing
----------------------
Extension: mp4
Content-Type = application/mp4
width: 320
height: 240
----------------------
Extension: mp4
Content-Type = application/mp4
width: 640
height: 360
----------------------
Extension: mp4
Content-Type = application/mp4
width: 725
height: 408
----------------------
Extension: mp4
Content-Type = application/mp4
width: 725
height: 408
----------------------
Extension: mp4
Content-Type = video/mp4
width: 0
height: 0
----------------------
Extension: mp4
Content-Type = video/mp4
width: 0
height: 0
----------------------
Extension: mp4
Content-Type = video/mp4
width: 718
height: 404
----------------------
Extension: mp4
Content-Type = video/mp4
width: 720
height: 408
----------------------
Extension: mp4
Content-Type = video/mp4
width: 854
height: 480
----------------------
Extension: mp4
Content-Type = video/x-m4v
width: 725
height: 408
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: mpg
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: msi
Content-Type = application/x-ms-installer
width: nothing
height: nothing
----------------------
Extension: pdf
Content-Type = application/pdf
width: nothing
height: nothing
----------------------
Extension: png
Content-Type = image/png
width: 600
height: 371
----------------------
Extension: pptx
Content-Type = application/vnd.openxmlformats-officedocument.presentationml.presentation
width: nothing
height: nothing
----------------------
Extension: svg
Content-Type = image/svg+xml
width: nothing
height: nothing
----------------------
Extension: ts
Content-Type = application/octet-stream
width: nothing
height: nothing
----------------------
Extension: vcmf
Content-Type = application/octet-stream
width: nothing
height: nothing
----------------------
Extension: vob
Content-Type = video/mpeg
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: webm
Content-Type = video/webm
width: nothing
height: nothing
----------------------
Extension: wmv
Content-Type = video/x-ms-wmv
width: nothing
height: nothing
----------------------
Extension: zip
Content-Type = application/zip
width: nothing
height: nothing
----------------------

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0