2009-04-04 15:36:12 -04:00
|
|
|
|
|
|
|
package net.sourceforge.filebot.format;
|
|
|
|
|
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
import static net.sourceforge.filebot.MediaTypes.*;
|
2009-05-17 13:22:44 -04:00
|
|
|
import static net.sourceforge.filebot.format.Define.*;
|
2009-08-10 07:46:24 -04:00
|
|
|
import static net.sourceforge.filebot.hash.VerificationUtilities.*;
|
2009-04-04 15:36:12 -04:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2009-04-25 03:59:08 -04:00
|
|
|
import java.util.Scanner;
|
2009-04-04 15:36:12 -04:00
|
|
|
import java.util.zip.CRC32;
|
|
|
|
|
|
|
|
import net.sf.ehcache.Cache;
|
|
|
|
import net.sf.ehcache.CacheManager;
|
|
|
|
import net.sf.ehcache.Element;
|
2009-08-10 07:46:24 -04:00
|
|
|
import net.sourceforge.filebot.hash.HashType;
|
2009-04-04 15:36:12 -04:00
|
|
|
import net.sourceforge.filebot.mediainfo.MediaInfo;
|
|
|
|
import net.sourceforge.filebot.mediainfo.MediaInfo.StreamKind;
|
|
|
|
import net.sourceforge.filebot.web.Episode;
|
2009-05-02 19:34:04 -04:00
|
|
|
import net.sourceforge.tuned.FileUtilities;
|
2009-04-04 15:36:12 -04:00
|
|
|
|
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
public class EpisodeBindingBean {
|
2009-04-04 15:36:12 -04:00
|
|
|
|
|
|
|
private final Episode episode;
|
|
|
|
|
|
|
|
private final File mediaFile;
|
|
|
|
|
|
|
|
private MediaInfo mediaInfo;
|
|
|
|
|
2009-06-13 17:27:01 -04:00
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
public EpisodeBindingBean(Episode episode, File mediaFile) {
|
2009-04-04 15:36:12 -04:00
|
|
|
this.episode = episode;
|
|
|
|
this.mediaFile = mediaFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define(undefined)
|
|
|
|
public String undefined() {
|
|
|
|
// omit expressions that depend on undefined values
|
|
|
|
throw new RuntimeException("undefined");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("n")
|
|
|
|
public String getSeriesName() {
|
2010-02-04 11:22:45 -05:00
|
|
|
return episode.getSeriesName();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("s")
|
|
|
|
public String getSeasonNumber() {
|
2009-05-03 11:21:04 -04:00
|
|
|
return episode.getSeason();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("e")
|
|
|
|
public String getEpisodeNumber() {
|
2009-05-03 11:21:04 -04:00
|
|
|
return episode.getEpisode();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("t")
|
|
|
|
public String getTitle() {
|
2010-02-04 11:22:45 -05:00
|
|
|
return episode.getTitle();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("vc")
|
|
|
|
public String getVideoCodec() {
|
2009-08-23 11:17:32 -04:00
|
|
|
// e.g. XviD, x264, DivX 5, MPEG-4 Visual, AVC, etc.
|
|
|
|
String codec = getMediaInfo(StreamKind.Video, 0, "Encoded_Library/Name", "CodecID/Hint", "Format");
|
|
|
|
|
|
|
|
// get first token (e.g. DivX 5 => DivX)
|
|
|
|
return new Scanner(codec).next();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("ac")
|
|
|
|
public String getAudioCodec() {
|
2009-08-23 11:17:32 -04:00
|
|
|
// e.g. AC-3, DTS, AAC, Vorbis, MP3, etc.
|
|
|
|
String codec = getMediaInfo(StreamKind.Audio, 0, "CodecID/Hint", "Format");
|
|
|
|
|
|
|
|
// remove punctuation (e.g. AC-3 => AC3)
|
|
|
|
return codec.replaceAll("\\p{Punct}", "");
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
@Define("cf")
|
|
|
|
public String getContainerFormat() {
|
2009-08-23 11:17:32 -04:00
|
|
|
// container format extensions (e.g. avi, mkv mka mks, OGG, etc.)
|
|
|
|
String extensions = getMediaInfo(StreamKind.General, 0, "Codec/Extensions", "Format");
|
2009-05-02 19:34:04 -04:00
|
|
|
|
2009-08-23 11:17:32 -04:00
|
|
|
// get first extension
|
|
|
|
return new Scanner(extensions).next().toLowerCase();
|
2009-05-02 19:34:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-23 11:17:32 -04:00
|
|
|
@Define("vf")
|
|
|
|
public String getVideoFormat() {
|
2009-04-04 15:36:12 -04:00
|
|
|
String height = getMediaInfo(StreamKind.Video, 0, "Height");
|
2009-08-23 11:17:32 -04:00
|
|
|
String scanType = getMediaInfo(StreamKind.Video, 0, "ScanType");
|
2009-04-04 15:36:12 -04:00
|
|
|
|
2009-08-23 11:17:32 -04:00
|
|
|
if (height == null || scanType == null)
|
2009-04-04 15:36:12 -04:00
|
|
|
return null;
|
|
|
|
|
|
|
|
// e.g. 720p
|
2009-08-23 11:17:32 -04:00
|
|
|
return height + Character.toLowerCase(scanType.charAt(0));
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("resolution")
|
|
|
|
public String getVideoResolution() {
|
|
|
|
String width = getMediaInfo(StreamKind.Video, 0, "Width");
|
|
|
|
String height = getMediaInfo(StreamKind.Video, 0, "Height");
|
|
|
|
|
|
|
|
if (width == null || height == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// e.g. 1280x720
|
|
|
|
return width + 'x' + height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("crc32")
|
2009-04-26 09:34:22 -04:00
|
|
|
public String getCRC32() throws IOException, InterruptedException {
|
2009-05-26 13:05:05 -04:00
|
|
|
// use inferred media file
|
|
|
|
File inferredMediaFile = getInferredMediaFile();
|
|
|
|
|
2009-05-03 11:21:04 -04:00
|
|
|
// try to get checksum from file name
|
2009-08-10 07:46:24 -04:00
|
|
|
String checksum = getEmbeddedChecksum(inferredMediaFile.getName());
|
2009-05-03 11:21:04 -04:00
|
|
|
|
|
|
|
if (checksum != null)
|
|
|
|
return checksum;
|
|
|
|
|
|
|
|
// try to get checksum from sfv file
|
2009-08-10 07:46:24 -04:00
|
|
|
checksum = getHashFromVerificationFile(inferredMediaFile, HashType.SFV, 3);
|
2009-05-03 11:21:04 -04:00
|
|
|
|
|
|
|
if (checksum != null)
|
|
|
|
return checksum;
|
|
|
|
|
|
|
|
// calculate checksum from file
|
2009-05-26 13:05:05 -04:00
|
|
|
return crc32(inferredMediaFile);
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
@Define("ext")
|
2009-05-03 12:28:39 -04:00
|
|
|
public String getExtension() {
|
2009-05-03 11:21:04 -04:00
|
|
|
// make sure media file is defined
|
|
|
|
checkMediaFile();
|
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
// file extension
|
|
|
|
return FileUtilities.getExtension(mediaFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-03 12:28:39 -04:00
|
|
|
@Define("media")
|
2009-04-06 16:34:33 -04:00
|
|
|
public Object getGeneralMediaInfo() {
|
|
|
|
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.General, 0));
|
2009-04-05 17:03:40 -04:00
|
|
|
}
|
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2009-04-04 15:36:12 -04:00
|
|
|
@Define("video")
|
2009-04-06 16:34:33 -04:00
|
|
|
public Object getVideoInfo() {
|
|
|
|
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.Video, 0));
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Define("audio")
|
2009-04-06 16:34:33 -04:00
|
|
|
public Object getAudioInfo() {
|
|
|
|
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.Audio, 0));
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2009-04-05 17:03:40 -04:00
|
|
|
@Define("text")
|
2009-04-06 16:34:33 -04:00
|
|
|
public Object getTextInfo() {
|
|
|
|
return new AssociativeScriptObject(getMediaInfo().snapshot(StreamKind.Text, 0));
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
2009-04-06 16:34:33 -04:00
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
@Define("episode")
|
2009-04-25 03:59:08 -04:00
|
|
|
public Episode getEpisode() {
|
|
|
|
return episode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-02 19:34:04 -04:00
|
|
|
@Define("file")
|
2009-04-25 03:59:08 -04:00
|
|
|
public File getMediaFile() {
|
|
|
|
return mediaFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
private File getInferredMediaFile() {
|
2009-05-03 13:38:22 -04:00
|
|
|
// make sure media file is defined
|
|
|
|
checkMediaFile();
|
|
|
|
|
2009-05-03 12:28:39 -04:00
|
|
|
if (SUBTITLE_FILES.accept(mediaFile)) {
|
|
|
|
// file is a subtitle
|
|
|
|
String name = FileUtilities.getName(mediaFile);
|
|
|
|
|
|
|
|
// find corresponding movie file
|
2009-05-24 14:38:12 -04:00
|
|
|
for (File movie : mediaFile.getParentFile().listFiles(VIDEO_FILES)) {
|
2009-05-03 12:28:39 -04:00
|
|
|
if (name.startsWith(FileUtilities.getName(movie))) {
|
|
|
|
return movie;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mediaFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-29 16:31:08 -04:00
|
|
|
private void checkMediaFile() throws RuntimeException {
|
|
|
|
// make sure file is not null, and that it is an existing file
|
|
|
|
if (mediaFile == null || !mediaFile.isFile())
|
2009-08-02 07:48:45 -04:00
|
|
|
throw new RuntimeException(String.format("Invalid media file: %s", mediaFile));
|
2009-05-03 11:21:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 07:48:45 -04:00
|
|
|
private synchronized MediaInfo getMediaInfo() {
|
2009-04-04 15:36:12 -04:00
|
|
|
if (mediaInfo == null) {
|
2009-05-03 11:21:04 -04:00
|
|
|
// make sure media file is defined
|
|
|
|
checkMediaFile();
|
|
|
|
|
2009-05-03 12:28:39 -04:00
|
|
|
MediaInfo newMediaInfo = new MediaInfo();
|
2009-04-05 13:43:32 -04:00
|
|
|
|
2009-05-03 13:38:22 -04:00
|
|
|
// use inferred media file (e.g. actual movie file instead of subtitle file)
|
2009-05-03 12:28:39 -04:00
|
|
|
if (!newMediaInfo.open(getInferredMediaFile())) {
|
|
|
|
throw new RuntimeException(String.format("Cannot open media file: %s", mediaFile));
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
2009-05-03 12:28:39 -04:00
|
|
|
|
|
|
|
mediaInfo = newMediaInfo;
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mediaInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getMediaInfo(StreamKind streamKind, int streamNumber, String... keys) {
|
2009-04-04 16:19:03 -04:00
|
|
|
for (String key : keys) {
|
|
|
|
String value = getMediaInfo().get(streamKind, streamNumber, key);
|
|
|
|
|
|
|
|
if (value.length() > 0) {
|
|
|
|
return value;
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-04-26 14:57:27 -04:00
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
private String crc32(File file) throws IOException, InterruptedException {
|
2009-04-04 15:36:12 -04:00
|
|
|
// try to get checksum from cache
|
2009-04-26 14:57:27 -04:00
|
|
|
Cache cache = CacheManager.getInstance().getCache("checksum");
|
|
|
|
|
2009-08-10 07:46:24 -04:00
|
|
|
try {
|
|
|
|
return String.format("%08X", cache.get(file).getValue());
|
|
|
|
} catch (Exception e) {
|
|
|
|
// checksum is not cached
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate checksum
|
|
|
|
InputStream in = new FileInputStream(file);
|
|
|
|
CRC32 crc = new CRC32();
|
|
|
|
|
|
|
|
try {
|
|
|
|
byte[] buffer = new byte[32 * 1024];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while ((len = in.read(buffer)) >= 0) {
|
|
|
|
crc.update(buffer, 0, len);
|
2009-04-26 09:34:22 -04:00
|
|
|
|
|
|
|
// make this long-running operation interruptible
|
|
|
|
if (Thread.interrupted())
|
|
|
|
throw new InterruptedException();
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2009-06-13 17:27:01 -04:00
|
|
|
// cache calculated checksum
|
|
|
|
cache.put(new Element(file, crc.getValue()));
|
2009-04-04 15:36:12 -04:00
|
|
|
|
2009-06-13 17:27:01 -04:00
|
|
|
return String.format("%08X", crc.getValue());
|
2009-04-04 15:36:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|