List of usage examples for org.apache.commons.lang3.math NumberUtils toFloat
public static float toFloat(final String str, final float defaultValue)
Convert a String
to a float
, returning a default value if the conversion fails.
If the string str
is null
, the default value is returned.
NumberUtils.toFloat(null, 1.1f) = 1.0f NumberUtils.toFloat("", 1.1f) = 1.1f NumberUtils.toFloat("1.5", 0.0f) = 1.5f
From source file:com.moviejukebox.scanner.MediaInfoScanner.java
/** * Update the movie information from the media info * * @param movie/*from ww w . jav a2s .co m*/ * @param infosGeneral * @param infosVideo * @param infosAudio * @param infosText * @param infosMultiPart */ public void updateMovieInfo(Movie movie, Map<String, String> infosGeneral, List<Map<String, String>> infosVideo, List<Map<String, String>> infosAudio, List<Map<String, String>> infosText, Map<String, String> infosMultiPart) { String infoValue; // update movie with meta tags if present and required if (ENABLE_METADATA) { processMetaData(movie, infosGeneral); } // enableMetaData // get Container from General Section if (OverrideTools.checkOverwriteContainer(movie, MEDIAINFO_PLUGIN_ID)) { infoValue = infosGeneral.get("Format"); movie.setContainer(infoValue, MEDIAINFO_PLUGIN_ID); } if (OverrideTools.checkOverwriteRuntime(movie, MEDIAINFO_PLUGIN_ID)) { int duration = getDuration(infosGeneral, infosVideo); duration = duration + getMultiPartDuration(infosMultiPart); if (duration > 0) { // if (duration > 900000) { // 15 minutes in milliseconds duration = duration / 1000; /* } else if (duration > 900) { // 15 minutes in seconds // No change required } else { // probably in minutes duration = duration * 60; }*/ // Duration is returned in minutes, convert it to seconds movie.setRuntime(DateTimeTools.formatDuration(duration), MEDIAINFO_PLUGIN_ID); } } // get Info from first Video Stream // - can evolve to get info from longest Video Stream if (!infosVideo.isEmpty()) { // At this point there is only a codec pulled from the filename, so we can clear that now movie.getCodecs().clear(); Map<String, String> infosMainVideo = infosVideo.get(0); // Add the video codec to the list Codec codecToAdd = getCodecInfo(CodecType.VIDEO, infosMainVideo); if (MI_OVERALL_BITRATE && StringTools.isNotValidString(codecToAdd.getCodecBitRate())) { infoValue = infosGeneral.get(Codec.MI_CODEC_OVERALL_BITRATE); if (StringUtils.isNotBlank(infoValue) && infoValue.length() > 3) { infoValue = infoValue.substring(0, infoValue.length() - 3); codecToAdd.setCodecBitRate(infoValue); } } movie.addCodec(codecToAdd); if (OverrideTools.checkOverwriteResolution(movie, MEDIAINFO_PLUGIN_ID)) { String width = infosMainVideo.get("Width"); String height = infosMainVideo.get("Height"); movie.setResolution(width, height, MEDIAINFO_PLUGIN_ID); } // Frames per second if (OverrideTools.checkOverwriteFPS(movie, MEDIAINFO_PLUGIN_ID)) { infoValue = infosMainVideo.get("Frame rate"); if (infoValue == null) { // use original frame rate infoValue = infosMainVideo.get("Original frame rate"); } if (infoValue != null) { int inxDiv = infoValue.indexOf(Movie.SPACE_SLASH_SPACE); if (inxDiv > -1) { infoValue = infoValue.substring(0, inxDiv); } movie.setFps(NumberUtils.toFloat(infoValue, 0.0F), MEDIAINFO_PLUGIN_ID); } } // Save the aspect ratio for the video if (OverrideTools.checkOverwriteAspectRatio(movie, MEDIAINFO_PLUGIN_ID)) { infoValue = infosMainVideo.get("Display aspect ratio"); if (infoValue != null) { movie.setAspectRatio(ASPECT_TOOLS.cleanAspectRatio(infoValue), MEDIAINFO_PLUGIN_ID); } } if (OverrideTools.checkOverwriteVideoOutput(movie, MEDIAINFO_PLUGIN_ID)) { // Guessing Video Output (Issue 988) if (movie.isHD()) { StringBuilder normeHD = new StringBuilder(); if (movie.isHD1080()) { normeHD.append("1080"); } else { normeHD.append("720"); } infoValue = infosMainVideo.get("Scan type"); if (infoValue != null) { if ("Progressive".equals(infoValue)) { normeHD.append("p"); } else { normeHD.append("i"); } } normeHD.append(" ").append(Math.round(movie.getFps())).append("Hz"); movie.setVideoOutput(normeHD.toString(), MEDIAINFO_PLUGIN_ID); } else { StringBuilder videoOutput = new StringBuilder(); switch (Math.round(movie.getFps())) { case 24: videoOutput.append("24"); break; case 25: videoOutput.append("PAL 25"); break; case 30: videoOutput.append("NTSC 30"); break; case 50: videoOutput.append("PAL 50"); break; case 60: videoOutput.append("NTSC 60"); break; default: videoOutput.append("NTSC"); break; } infoValue = infosMainVideo.get("Scan type"); if (infoValue != null) { if ("Progressive".equals(infoValue)) { videoOutput.append("p"); } else { videoOutput.append("i"); } } movie.setVideoOutput(videoOutput.toString(), MEDIAINFO_PLUGIN_ID); } } if (OverrideTools.checkOverwriteVideoSource(movie, MEDIAINFO_PLUGIN_ID)) { infoValue = infosMainVideo.get("MultiView_Count"); if ("2".equals(infoValue)) { movie.setVideoSource("3D", MEDIAINFO_PLUGIN_ID); } } } // Cycle through Audio Streams // boolean previousAudioCodec = !movie.getAudioCodec().equals(Movie.UNKNOWN); // Do we have AudioCodec already? // boolean previousAudioChannels = !movie.getAudioChannels().equals(Movie.UNKNOWN); // Do we have AudioChannels already? Set<String> foundLanguages = new HashSet<>(); for (Map<String, String> infosCurAudio : infosAudio) { infoValue = infosCurAudio.get("Language"); if (infoValue != null) { // Issue 1227 - Make some clean up in mediainfo datas. if (infoValue.contains("/")) { infoValue = infoValue.substring(0, infoValue.indexOf('/')).trim(); // In this case, language are "doubled", just take the first one. } // Add determination of language. String determineLanguage = MovieFilenameScanner.determineLanguage(infoValue); foundLanguages.add(determineLanguage); } // Add the audio codec to the list Codec codecToAdd = getCodecInfo(CodecType.AUDIO, infosCurAudio); movie.addCodec(codecToAdd); } if (OverrideTools.checkOverwriteLanguage(movie, MEDIAINFO_PLUGIN_ID)) { StringBuilder movieLanguage = new StringBuilder(); for (String language : foundLanguages) { if (movieLanguage.length() > 0) { movieLanguage.append(LANG_DELIM); } movieLanguage.append(language); } if (StringTools.isValidString(movieLanguage.toString())) { movie.setLanguage(movieLanguage.toString(), MEDIAINFO_PLUGIN_ID); } else if (StringTools.isValidString(AUDIO_LANG_UNKNOWN)) { String determineLanguage = MovieFilenameScanner.determineLanguage(AUDIO_LANG_UNKNOWN); movie.setLanguage(determineLanguage, MEDIAINFO_PLUGIN_ID); } } // Cycle through Text Streams for (Map<String, String> infosCurText : infosText) { String infoLanguage = ""; infoValue = infosCurText.get("Language"); // Issue 1450 - If we are here, we have subtitles, but didn't have the language, setting an value of "UNDEFINED" to make it appear if (StringTools.isNotValidString(infoValue)) { infoValue = "UNDEFINED"; } if (StringTools.isValidString(infoValue)) { // Issue 1227 - Make some clean up in mediainfo datas. if (infoValue.contains("/")) { infoValue = infoValue.substring(0, infoValue.indexOf('/')).trim(); // In this case, languages are "doubled", just take the first one. } infoLanguage = MovieFilenameScanner.determineLanguage(infoValue); } String infoFormat = ""; infoValue = infosCurText.get("Format"); if (StringTools.isValidString(infoValue)) { infoFormat = infoValue; } else { // Issue 1450 - If we are here, we have subtitles, but didn't have the language // Take care of label for "Format" in mediaInfo 0.6.1.1 infoValue = infosCurText.get("Codec"); if (StringTools.isValidString(infoValue)) { infoFormat = infoValue; } } // Make sure we have a codec & language before continuing if (StringTools.isValidString(infoFormat) && StringTools.isValidString(infoLanguage)) { if ("SRT".equalsIgnoreCase(infoFormat) || "UTF-8".equalsIgnoreCase(infoFormat) || "RLE".equalsIgnoreCase(infoFormat) || "PGS".equalsIgnoreCase(infoFormat) || "ASS".equalsIgnoreCase(infoFormat) || "VobSub".equalsIgnoreCase(infoFormat)) { SubtitleTools.addMovieSubtitle(movie, infoLanguage); } else { LOG.debug("Subtitle format skipped: {}", infoFormat); } } } }
From source file:com.moviejukebox.plugin.SratimPlugin.java
public void downloadSubtitle(Movie movie, MovieFile mf) throws IOException { if (!subtitleDownload) { mf.setSubtitlesExchange(true);//ww w. jav a2s.c om return; } // Get the file base name String path = mf.getFile().getName().toUpperCase(); int lindex = path.lastIndexOf('.'); if (lindex == -1) { return; } String basename = path.substring(0, lindex); // Check if this is a bluray file boolean bluRay = false; if (path.endsWith(".M2TS") && path.startsWith("0")) { bluRay = true; } if (movie.isExtra()) { mf.setSubtitlesExchange(true); return; } // Check if this movie already have subtitles for it (.srt and .sub) if (hasExistingSubtitles(mf, bluRay)) { mf.setSubtitlesExchange(true); return; } basename = basename.replace('.', ' ').replace('-', ' ').replace('_', ' '); LOG.debug("Download Subtitle: {}", mf.getFile().getAbsolutePath()); LOG.debug("Basename : {}", basename); LOG.debug("BluRay : {}", bluRay); int bestFPSCount = 0; int bestBlurayCount = 0; int bestBlurayFPSCount = 0; String bestFPSID = ""; String bestBlurayID = ""; String bestBlurayFPSID = ""; String bestFileID = ""; String bestSimilar = ""; // retrieve subtitles page String subID = movie.getId(SRATIM_PLUGIN_SUBTITLE_ID); String mainXML = httpClient.request("http://www.sratim.co.il/subtitles.php?mid=" + subID); int index = 0; int endIndex; // find the end of hebrew subtitles section, to prevent downloading non-hebrew ones int endHebrewSubsIndex = findEndOfHebrewSubtitlesSection(mainXML); // Check that hebrew subtitle exist String hebrewSub = HTMLTools.getTextAfterElem(mainXML, "<img src=\"images/Flags/1.png"); LOG.debug("hebrewSub: {}", hebrewSub); // Check that there is no 0 hebrew sub if (Movie.UNKNOWN.equals(hebrewSub)) { LOG.debug("No Hebrew subtitles"); return; } double maxMatch = 0.0; double matchThreshold = PropertiesUtil.getFloatProperty("sratim.textMatchSimilarity", 0.8f); while (index < endHebrewSubsIndex) { // // scanID // index = mainXML.indexOf("href=\"downloadsubtitle.php?id=", index); if (index == -1) { break; } index += 30; endIndex = mainXML.indexOf('\"', index); if (endIndex == -1) { break; } String scanID = mainXML.substring(index, endIndex); // // scanDiscs // index = mainXML.indexOf("src=\"images/cds/cd", index); if (index == -1) { break; } index += 18; endIndex = mainXML.indexOf('.', index); if (endIndex == -1) { break; } String scanDiscs = mainXML.substring(index, endIndex); // // scanFileName // index = mainXML.indexOf("subtitle_title\" style=\"direction:ltr;\" title=\"", index); if (index == -1) { break; } index += 46; endIndex = mainXML.indexOf('\"', index); if (endIndex == -1) { break; } String scanFileName = mainXML.substring(index, endIndex).toUpperCase().replace('.', ' '); // removing all characters causing metric to hang. scanFileName = scanFileName.replaceAll("-|\u00A0", " ").replaceAll(" ++", " "); // // scanFormat // index = mainXML.indexOf("\u05e4\u05d5\u05e8\u05de\u05d8", index); // the hebrew letters for the word "format" if (index == -1) { break; } index += 6; endIndex = mainXML.indexOf(',', index); if (endIndex == -1) { break; } String scanFormat = mainXML.substring(index, endIndex); // // scanFPS // index = mainXML.indexOf("\u05dc\u05e9\u05e0\u0027\u003a", index); // the hebrew letters for the word "for sec':" lamed shin nun ' : if (index == -1) { break; } index += 5; endIndex = mainXML.indexOf('<', index); if (endIndex == -1) { break; } String scanFPS = mainXML.substring(index, endIndex); // // scanCount // index = mainXML.indexOf("subt_date\"><span class=\"smGray\">", index); if (index == -1) { break; } index += 32; endIndex = mainXML.indexOf(' ', index); if (endIndex == -1) { break; } String scanCount = mainXML.substring(index, endIndex); // Check for best text similarity double result = StringUtils.getJaroWinklerDistance(basename, scanFileName); if (result > maxMatch) { maxMatch = result; bestSimilar = scanID; } LOG.debug( "scanFileName: {} scanFPS: {} scanID: {} scanCount: {} scanDiscs: {} scanFormat: {} similarity: {}", scanFileName, scanFPS, scanID, scanCount, scanDiscs, scanFormat, result); // Check if movie parts matches int nDiscs = movie.getMovieFiles().size(); if (!String.valueOf(nDiscs).equals(scanDiscs)) { continue; } // Check for exact file name if (scanFileName.equals(basename)) { bestFileID = scanID; break; } int scanCountInt = NumberUtils.toInt(scanCount, 0); float scanFPSFloat = NumberUtils.toFloat(scanFPS, 0F); LOG.debug("FPS: {} scanFPS: {}", movie.getFps(), scanFPSFloat); if (bluRay && ((scanFileName.contains("BRRIP")) || (scanFileName.contains("BDRIP")) || (scanFileName.contains("BLURAY")) || (scanFileName.contains("BLU-RAY")) || (scanFileName.contains("HDDVD")))) { if ((Float.compare(scanFPSFloat, 0F) == 0) && (scanCountInt > bestBlurayCount)) { bestBlurayCount = scanCountInt; bestBlurayID = scanID; } if ((Float.compare(movie.getFps(), scanFPSFloat) == 0) && (scanCountInt > bestBlurayFPSCount)) { bestBlurayFPSCount = scanCountInt; bestBlurayFPSID = scanID; } } if ((Float.compare(movie.getFps(), scanFPSFloat) == 0) && (scanCountInt > bestFPSCount)) { bestFPSCount = scanCountInt; bestFPSID = scanID; } } // Select the best subtitles ID String bestID; // Check for exact file name match if (StringUtils.isNotBlank(bestFileID)) { LOG.debug("Best Filename"); bestID = bestFileID; } else if (maxMatch >= matchThreshold) { // Check for text similarity match, similarity threshold takes precedence over FPS check LOG.debug("Best Text Similarity threshold"); bestID = bestSimilar; } else if (StringUtils.isNotBlank(bestBlurayFPSID)) { // Check for bluray match LOG.debug("Best Bluray FPS"); bestID = bestBlurayFPSID; } else if (StringUtils.isNotBlank(bestBlurayID)) { // Check for bluray match LOG.debug("Best Bluray"); bestID = bestBlurayID; } else if (StringUtils.isNotBlank(bestFPSID)) { // Check for fps match LOG.debug("Best FPS"); bestID = bestFPSID; } else if (maxMatch > 0) { // Check for text match, now just choose the best similar name LOG.debug("Best Similar"); bestID = bestSimilar; } else { LOG.debug("No subtitle found"); return; } LOG.debug("bestID: {}", bestID); // reconstruct movie filename with full path String orgName = mf.getFile().getAbsolutePath(); File subtitleFile = new File(orgName.substring(0, orgName.lastIndexOf('.'))); if (!downloadSubtitleZip(movie, "http://www.sratim.co.il/downloadsubtitle.php?id=" + bestID, subtitleFile, bluRay)) { LOG.error("Error - Subtitle download failed"); return; } mf.setSubtitlesExchange(true); SubtitleTools.addMovieSubtitle(movie, "YES"); }
From source file:com.moviejukebox.reader.MovieNFOReader.java
/** * Parse the rating from the passed string and normalise it * * @param ratingString//from ww w .j av a 2 s . c o m * @param movie * @return true if the rating was successfully parsed. */ private static int parseRating(String ratingString) { if (StringTools.isNotValidString(ratingString)) { // Rating isn't valid, so skip it return -1; } float rating = NumberUtils.toFloat(ratingString, 0.0f); if (rating > 0.0f) { if (rating <= 10.0f) { return Math.round(rating * 10f); } return Math.round(rating * 1f); } // negative or zero, so return zero return 0; }
From source file:com.moviejukebox.reader.MovieJukeboxXMLReader.java
private void parseOverridableFramesPerSecond(Movie movie, Element element) { OverridableValue ov = new OverridableValue(element, "fps"); float fps = NumberUtils.toFloat(ov.getValue(), 0.0f); movie.setFps(fps, ov.getSource());/*w ww . j a va2s. co m*/ }
From source file:org.yamj.core.tools.MetadataTools.java
/** * Parse a string value and convert it into an integer rating * * The rating should be between 0 and 10 inclusive.<br/> * Invalid values or values less than 0 will return -1 * * @param rating the converted rating or -1 if there was an error * @return/*from www. ja va 2 s . c om*/ */ public static int parseRating(String rating) { if (StringUtils.isBlank(rating)) { // Rating isn't valid, so skip it return -1; } return parseRating(NumberUtils.toFloat(rating.replace(',', '.'), -1)); }