[Java] 用 vlcj 取得影片的 metadata

vlcj 能對影片做許多操作
這裡只說明取得 metadata 的部分
https://github.com/caprica/vlcj
https://capricasoftware.co.uk/projects/vlcj-4/tutorials

Maven

<!-- https://mvnrepository.com/artifact/uk.co.caprica/vlcj -->
<dependency>
    <groupId>uk.co.caprica</groupId>
    <artifactId>vlcj</artifactId>
    <version>4.7.1</version>
</dependency>

import

import uk.co.caprica.vlcj.media.*;
import uk.co.caprica.vlcj.player.component.*;

取得 metadata

EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
mediaPlayerComponent.mediaPlayer().media().prepare("videoPath");
mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
    @Override
    public void mediaParsedChanged(Media media, MediaParsedStatus status) {
        if(status != MediaParsedStatus.DONE) {
            return;
        }
        String dateString = media.meta().get(Meta.DATE);
    }
});
mediaPlayerComponent.mediaPlayer().media().parsing().parse();
  • 先 listen 才能 parse
  • 每個 event 都是一個 Thread

取得影片日期的 API

public static String getVideoDateTime(String file) throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    ArrayList<String> result = new ArrayList<String>();
    result.add("");
    EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
    mediaPlayerComponent.mediaPlayer().media().prepare(file);
    mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
        @Override
        public void mediaParsedChanged(Media media, MediaParsedStatus status) {
            if(status != MediaParsedStatus.DONE) {
                countDownLatch.countDown();
                return;
            }
            String dateString = media.meta().get(Meta.DATE);
            if (dateString == null) {
                countDownLatch.countDown();
                return;
            }

            //2018-01-13T15:09:12+0800
            String y = dateString.substring(0, 4);
            String m = dateString.substring(5, 7);
            String d = dateString.substring(8, 10);
            String h = dateString.substring(11, 13);
            String mm = dateString.substring(14, 16);
            String s = dateString.substring(17, 19);
            
            result.set(0, y + m + d + "_" + h + mm + s);
            countDownLatch.countDown();
        }
    });
    mediaPlayerComponent.mediaPlayer().media().parsing().parse();
    countDownLatch.await();
    return result.get(0);
}