From e999fb34e92eadd073e4c841da7534da3b4bfa7c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Fri, 20 Dec 2013 05:32:28 +0000 Subject: [PATCH] * my need duration filter in the future, not used yet though --- .../filebot/format/MediaBindingBean.java | 5 ++ .../mediainfo/MediaDurationFilter.java | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 source/net/sourceforge/filebot/mediainfo/MediaDurationFilter.java diff --git a/source/net/sourceforge/filebot/format/MediaBindingBean.java b/source/net/sourceforge/filebot/format/MediaBindingBean.java index 98100e49..e26bf621 100644 --- a/source/net/sourceforge/filebot/format/MediaBindingBean.java +++ b/source/net/sourceforge/filebot/format/MediaBindingBean.java @@ -526,6 +526,11 @@ public class MediaBindingBean { return new Integer(getMediaInfo(StreamKind.General, 0, "Duration")); } + @Define("seconds") + public Integer getSeconds() { + return Math.round(getDuration() / 1000f); + } + @Define("minutes") public Integer getDurationInMinutes() { return Math.round(getDuration() / 60000f); diff --git a/source/net/sourceforge/filebot/mediainfo/MediaDurationFilter.java b/source/net/sourceforge/filebot/mediainfo/MediaDurationFilter.java new file mode 100644 index 00000000..b4a06eff --- /dev/null +++ b/source/net/sourceforge/filebot/mediainfo/MediaDurationFilter.java @@ -0,0 +1,47 @@ +package net.sourceforge.filebot.mediainfo; + +import java.io.File; +import java.io.FileFilter; + +import net.sourceforge.filebot.mediainfo.MediaInfo.StreamKind; + +public class MediaDurationFilter implements FileFilter { + + private final MediaInfo mediaInfo = new MediaInfo(); + + private final long min; + private final long max; + private final boolean acceptByDefault; + + public MediaDurationFilter(long min) { + this(min, Long.MAX_VALUE, false); + } + + public MediaDurationFilter(long min, long max, boolean acceptByDefault) { + this.min = min; + this.max = max; + this.acceptByDefault = acceptByDefault; + } + + public long getDuration(File file) { + synchronized (mediaInfo) { + if (mediaInfo.open(file)) { + try { + return Long.parseLong(mediaInfo.get(StreamKind.General, 0, "Duration")); + } catch (NumberFormatException e) { + // ignore, assume duration couldn't be read + } + } + } + return -1; + } + + @Override + public boolean accept(File file) { + long d = getDuration(file); + if (d >= 0) { + return d >= min && d <= max; + } + return acceptByDefault; + } +}