2018-06-03 08:12:05 -04:00
|
|
|
package net.filebot.media;
|
2013-12-20 00:32:28 -05:00
|
|
|
|
2016-03-11 03:16:59 -05:00
|
|
|
import static net.filebot.Logging.*;
|
|
|
|
|
2013-12-20 00:32:28 -05:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
|
|
|
|
public class MediaDurationFilter implements FileFilter {
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-09 14:33:14 -04:00
|
|
|
public long getDuration(File f) {
|
2019-01-28 09:08:24 -05:00
|
|
|
try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) {
|
2018-06-03 08:45:17 -04:00
|
|
|
return mi.getDuration().toMillis();
|
2016-08-09 14:33:14 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
debug.warning(format("Failed to read video duration: %s", e.getMessage()));
|
2013-12-20 00:32:28 -05:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-08-09 14:33:14 -04:00
|
|
|
public boolean accept(File f) {
|
|
|
|
long d = getDuration(f);
|
2013-12-20 00:32:28 -05:00
|
|
|
if (d >= 0) {
|
|
|
|
return d >= min && d <= max;
|
|
|
|
}
|
|
|
|
return acceptByDefault;
|
|
|
|
}
|
|
|
|
}
|