AudioResourceUtils.java (4805B)
1 /* 2 * _______ _____ _____ _____ 3 * |__ __| | __ \ / ____| __ \ 4 * | | __ _ _ __ ___ ___ ___| | | | (___ | |__) | 5 * | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/ 6 * | | (_| | | \__ \ (_) \__ \ |__| |____) | | 7 * |_|\__,_|_| |___/\___/|___/_____/|_____/|_| 8 * 9 * ------------------------------------------------------------- 10 * 11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent 12 * 13 * ------------------------------------------------------------- 14 * 15 * Info: http://0110.be/tag/TarsosDSP 16 * Github: https://github.com/JorenSix/TarsosDSP 17 * Releases: http://0110.be/releases/TarsosDSP/ 18 * 19 * TarsosDSP includes modified source code by various authors, 20 * for credits and info, see README. 21 * 22 */ 23 24 package be.tarsos.dsp.util; 25 26 import java.io.BufferedReader; 27 import java.io.IOException; 28 import java.io.InputStreamReader; 29 import java.net.MalformedURLException; 30 import java.net.URL; 31 32 /** 33 * Some utility functions to handle audio resources. 34 * 35 * @author Joren Six 36 */ 37 public class AudioResourceUtils { 38 39 private AudioResourceUtils() { 40 } 41 42 /** 43 * Returns a more practical audio resource name. E.g. if 44 * http://stream.com/stream.pls is given, the PLS-file is parsed and the 45 * first audio file is returned. It supports PLS, M3U, AXS and XSPF" 46 * 47 * @param inputResource 48 * The input resource, a file, URL, PLS-file or M3U-file. 49 * 50 * @return A more practical audio resource name. 51 */ 52 public static String sanitizeResource(String inputResource) { 53 if (inputResource.toLowerCase().endsWith("pls")) { 54 inputResource = parsePLS(inputResource); 55 } else if (inputResource.toLowerCase().endsWith("m3u")) { 56 inputResource = parseM3U(inputResource); 57 } else if (inputResource.toLowerCase().endsWith("asx")){ 58 inputResource = parseASX(inputResource); 59 } else if (inputResource.toLowerCase().endsWith("xspf")){ 60 inputResource = parseXSPF(inputResource); 61 } 62 return inputResource; 63 } 64 65 private static String parseXSPF(String inputResource){ 66 String inputFile = ""; 67 try { 68 String contents = readTextFromUrl(new URL(inputResource)); 69 for (String line : contents.split("\n")) { 70 if (line.toLowerCase().contains("href")) { 71 String pattern = "(?i)<location>(.*)</location>.*"; 72 inputFile = line.replaceAll(pattern, "$1"); 73 break; 74 } 75 } 76 } catch (MalformedURLException e) { 77 e.printStackTrace(); 78 } 79 return inputFile; 80 } 81 82 private static String parseASX(String inputResource) { 83 String inputFile = ""; 84 try { 85 String contents = readTextFromUrl(new URL(inputResource)); 86 for (String line : contents.split("\n")) { 87 if (line.toLowerCase().contains("href")) { 88 String pattern = "(?i).*href=\"(.*)\".*"; 89 inputFile = line.replaceAll(pattern, "$1"); 90 break; 91 } 92 } 93 } catch (MalformedURLException e) { 94 e.printStackTrace(); 95 } 96 return inputFile; 97 } 98 99 /** 100 * Parses the PLS file and returns the first file name. 101 * 102 * @param inputUrl 103 * The input PLS file. 104 * @return The first file name in the PLS playlist. 105 */ 106 public static String parsePLS(String inputUrl) { 107 String inputFile = ""; 108 try { 109 String plsContents = readTextFromUrl(new URL(inputUrl)); 110 for (String line : plsContents.split("\n")) { 111 if (line.startsWith("File1=")) { 112 inputFile = line.replace("File1=", "").trim(); 113 break; 114 } 115 } 116 } catch (MalformedURLException e) { 117 e.printStackTrace(); 118 } 119 120 return inputFile; 121 } 122 123 /** 124 * Parses the M3U file and returns the first file name. 125 * 126 * @param inputUrl 127 * The input M3U file. 128 * @return The first file name in the M3U play list. 129 */ 130 public static String parseM3U(String inputUrl) { 131 String inputFile = ""; 132 try { 133 String plsContents = readTextFromUrl(new URL(inputUrl)); 134 for (String line : plsContents.split("\n")) { 135 if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { 136 inputFile = line.trim(); 137 break; 138 } 139 } 140 } catch (MalformedURLException e) { 141 e.printStackTrace(); 142 } 143 return inputFile; 144 } 145 146 /** 147 * Return the text of the file with the given URL. E.g. if 148 * http://test.be/text.txt is given the contents of text.txt is returned. 149 * 150 * @param url 151 * The URL. 152 * @return The contents of the file. 153 */ 154 public static String readTextFromUrl(URL url) { 155 StringBuffer fubber = new StringBuffer(); 156 try { 157 BufferedReader in = new BufferedReader(new InputStreamReader( 158 url.openStream())); 159 String inputLine; 160 while ((inputLine = in.readLine()) != null) { 161 fubber.append(inputLine).append("\n"); 162 } 163 in.close(); 164 } catch (IOException exception) { 165 exception.printStackTrace(); 166 } 167 return fubber.toString(); 168 } 169 170 }