FFMPEGDownloader.java (3407B)
1 package be.tarsos.dsp.util; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 import java.nio.channels.Channels; 9 import java.nio.channels.ReadableByteChannel; 10 import java.util.logging.Logger; 11 12 /** 13 * Downloads a static ffmpeg binary for several platforms: 14 * Windows x64 and x32 15 * Max OS X x64 16 * Linux x32 and x64 17 * The code tries to determine the correct platform and downloads it to 18 * the temporary directory <code>System.getProperty("java.io.tmpdir")</code>. 19 * 20 * After downloading it makes the binary executable. 21 * The location of the downloaded binary is returned by <code>ffmpegBinary();</code> 22 * 23 * @author Joren Six 24 */ 25 public class FFMPEGDownloader { 26 27 private static String url = "https://0110.be/releases/TarsosDSP/TarsosDSP-static-ffmpeg/"; 28 29 private final String ffmpegBinary; 30 31 private final static Logger LOG = Logger.getLogger(FFMPEGDownloader.class.getName()); 32 33 public FFMPEGDownloader(){ 34 String filename = operatingSystemName() + "_" + processorArchitecture() + "_ffmpeg" + suffix(); 35 url = url + filename; 36 37 String tempDirectory = System.getProperty("java.io.tmpdir"); 38 String saveTo = new File(tempDirectory,filename).getAbsolutePath(); 39 40 if(new File(saveTo).exists()){ 41 LOG.info("Found an already download ffmpeg static binary: " + saveTo); 42 ffmpegBinary = saveTo; 43 }else{ 44 LOG.info("Started downloading an ffmpeg static binary from " + url); 45 downloadExecutable(saveTo); 46 47 if(new File(saveTo).exists()){ 48 LOG.info("Downloaded an ffmpeg static binary. Stored at: " + saveTo); 49 //make it executable 50 new File(saveTo).setExecutable(true); 51 ffmpegBinary = saveTo; 52 }else{ 53 //Unable to download or unknown architecture 54 LOG.warning("Unable to find or download an ffmpeg static binary. " + filename); 55 ffmpegBinary = null; 56 } 57 } 58 } 59 60 public String ffmpegBinary(){ 61 if(ffmpegBinary!=null){ 62 return ffmpegBinary.replace(suffix(), ""); 63 } 64 return null; 65 } 66 67 private void downloadExecutable(String saveTo){ 68 try{ 69 URL website = new URL(url); 70 ReadableByteChannel rbc = Channels.newChannel(website.openStream()); 71 FileOutputStream fos = new FileOutputStream(saveTo); 72 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 73 fos.close(); 74 }catch(MalformedURLException e){ 75 e.printStackTrace(); 76 } catch (IOException e) { 77 78 e.printStackTrace(); 79 } 80 } 81 82 private String operatingSystemName(){ 83 String name; 84 String operatingSystem = System.getProperty("os.name").toLowerCase(); 85 if(operatingSystem.indexOf("indows") > 0 ){ 86 name = "windows"; 87 }else if(operatingSystem.indexOf("nux") >= 0){ 88 name="linux"; 89 }else if(operatingSystem.indexOf("mac") >= 0){ 90 name="mac_os_x"; 91 }else{ 92 name = null; 93 } 94 return name; 95 } 96 97 private String processorArchitecture(){ 98 boolean is64bit = false; 99 if (System.getProperty("os.name").contains("Windows")) { 100 is64bit = (System.getenv("ProgramFiles(x86)") != null); 101 } else { 102 is64bit = (System.getProperty("os.arch").indexOf("64") != -1); 103 } 104 if(is64bit){ 105 return "64_bits"; 106 }else{ 107 return "32_bits"; 108 } 109 } 110 111 private String suffix(){ 112 String suffix = ""; 113 if (System.getProperty("os.name").contains("Windows")) { 114 suffix = ".exe"; 115 } 116 return suffix; 117 } 118 119 public static void main(String...strings){ 120 new FFMPEGDownloader(); 121 } 122 }