获取音视频的时长
引入:jave-1.0.2.jar? 自行百度如何放到maven私服中,或者自己的倉庫中。
@Testpublic void getMp3Duration() throws EncoderException {File source =new File("c:\\吳晴-過去成向往(原版).mpg");Encoder encoder = new Encoder();MultimediaInfo m = encoder.getInfo(source);long ls = m.getDuration();System.out.println("此視頻時長為:"+ls/60000+"分"+(ls/1000-ls/60000*60)+"秒!");System.out.println("此視頻時長為:"+ls/1000+"秒!");} @Testpublic void getDuration() throws EncoderException, IOException {String url = "http://static.mifanxing.com/yyren/image/week/20171110/1/Snare-1_2inchabove.mp3";String ext = url.substring(url.lastIndexOf("."));InputStream inputStream = getImgFromUrl(url);//保存成FileFile source = inputStreamToFile(inputStream, ext);Encoder encoder = new Encoder();MultimediaInfo m = encoder.getInfo(source);long ls = m.getDuration();System.out.println("此視頻時長為:" + ls / 60000 + "分" + (ls / 1000 - ls / 60000 * 60) + "秒!");System.out.println("此視頻時長為:" + ls / 1000 + "秒!");}private InputStream getImgFromUrl(String url) throws IOException {URL myUrl = new URL(url);HttpURLConnection con = (HttpURLConnection) myUrl.openConnection();con.setConnectTimeout(5 * 1000); // InputStream is = con.getInputStream();return con.getInputStream();}private File inputStreamToFile(InputStream ins, String ext) throws IOException {String source = "";if (System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {File[] roots = File.listRoots();if (roots.length > 0) {source=roots[0].toString().replaceAll("\\\\","/")+"temp/";}} else {source="/tmp/temp/";}File f = new File(source);if (!f.exists()){f.mkdir();}File file = File.createTempFile("temp", ext, f);OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();return file;}?
總結
- 上一篇: 3785. 战舰
- 下一篇: 今日科技联播:美股暴跌引全球股市崩盘;腾