1. 程式人生 > >java 根據URL獲取時長,視訊大小

java 根據URL獲取時長,視訊大小

/**
 * 根據網路路徑獲取時長
 * @author ZhangShaobo
 *
 */
public class VideoInfoTest {
	/**
	 * 獲取網路檔案,暫存為臨時檔案
	 * @param url
	 * @return
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public static File getFileByUrl(String url) throws UnknownHostException, IOException{
		File tmpFile = File.createTempFile("temp", ".tmp");//建立臨時檔案
		Image2Binary.toBDFile(url, tmpFile.getCanonicalPath());
		return tmpFile;
	}
	
	/**
	 * 獲取時長
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws InputFormatException
	 * @throws EncoderException
	 */
	public static long getDuration(File file) throws EncoderException{
		MultimediaInfo m = new Encoder().getInfo(file);
		return m.getDuration();
	}
	
	/**
	 * 獲取http://mp4.res.hunantv.com/new_video/28/7A0B569858C4B58526273897A2321658_20170926_1_1_256.mp4
	 * 時長資訊和大小資訊
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			File file = getFileByUrl("http://mp4.res.hunantv.com/new_video/28/7A0B569858C4B58526273897A2321658_20170926_1_1_256.mp4");
			System.out.println("視訊大小:"+file.length()/1024+"kb");
			System.out.println("視訊時長:"+getDuration(file)/1000 + "s");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

這裡建立的是臨時檔案,如有需要可以直接存成本地檔案!