Java根據URL獲取視訊時長以及大小
阿新 • • 發佈:2018-11-05
import java.io.File; import java.io.IOException; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.InputFormatException; import it.sauronsoftware.jave.MultimediaInfo; /** * 視訊檔案工具類 */ public class VideoFileUtils { /** * 獲取網路檔案,暫存為臨時檔案 * @param url * @return * @throws IOException */ public static File getFileByUrl(String url) throws IOException { File tmpFile = File.createTempFile("temp", ".tmp");//建立臨時檔案 Image2Binary.toBDFile(url, tmpFile.getCanonicalPath()); return tmpFile; } /** * 獲取時長(單位:秒) * * @param url * @return * @throws EncoderException * @throws InputFormatException */ public static long getDuration(File file) throws InputFormatException, EncoderException { MultimediaInfo m = new Encoder().getInfo(file); return m.getDuration() / 1000; } public static void main(String[] args) throws Exception { File file = getFileByUrl("http://gslb.miaopai.com/stream/t~gB32Ha~0TyT3~Uju8bqQ__.mp4"); System.out.println("視訊大小:" + file.length() / 1024 / 1024 + "MB"); System.out.println("視訊時長:" + getDuration(file) / 1000 + "S"); } }
所需工具類如下:把網路檔案轉換為本地檔案
import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.UnknownHostException; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Image2Binary { private static Logger log = LoggerFactory.getLogger(Image2Binary.class); public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out=new ByteArrayOutputStream(); byte[] buffer=new byte[1024*4]; int n=0; while ( (n=in.read(buffer)) !=-1) { out.write(buffer,0,n); } return out.toByteArray(); } /** * 網路檔案轉換為byte二進位制 * @Title: toByteArray * @Description: TODO(這裡用一句話描述這個方法的作用) * @param @param url * @param @return * @param @throws IOException 設定檔案 * @return byte[] 返回型別 * @throws */ public static byte[] toByteArray(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); ByteArrayOutputStream out=new ByteArrayOutputStream(); byte[] buffer=new byte[1024*4]; int n=0; while ( (n=in.read(buffer)) !=-1) { out.write(buffer,0,n); } return out.toByteArray(); } /** * @throws IOException * @throws MalformedURLException * 網路檔案轉換為本地檔案 * @Title: toByteArray * @Description: TODO(這裡用一句話描述這個方法的作用) * @param @param url * @param @return * @param @throws IOException 設定檔案 * @return byte[] 返回型別 * @throws */ public static void toBDFile(String urlStr, String bdUrl) throws IOException,UnknownHostException{ URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); byte[] data=toByteArray(in); in.close(); FileOutputStream out=new FileOutputStream(bdUrl); out.write(data); out.close(); } /** * 直接獲取網路檔案的md5值 * @param urlStr * @return */ public static String getMd5ByUrl(String urlStr){ String md5 = null; try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(conn.getInputStream()); md5 = DigestUtils.md5Hex(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return md5; } /** * 獲取網路檔案的輸入流 * @param urlStr * @return */ public static InputStream getInputStreamByUrl(String urlStr){ DataInputStream in = null; try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); in = new DataInputStream(conn.getInputStream()); } catch (IOException e) { log.error("url轉換輸入流失敗,錯誤資訊{}",e.getMessage()); } return in; } public static void main(String[] args) { try { toBDFile("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496324940814&di=1d70e0de447be6547c372718b9b30ff6&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F204%2F22%2FYMG9CAUWUM15.jpg","E://a.jpg"); } catch (IOException e) { e.printStackTrace(); } // String a = getMd5ByUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1496324940814&di=1d70e0de447be6547c372718b9b30ff6&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F204%2F22%2FYMG9CAUWUM15.jpg"); // System.out.println(a); } }
所需jar:jave-1.0.2.jar 下載附件即可