Java代码获取网络和本地视频时长等信息
生活随笔
收集整理的這篇文章主要介紹了
Java代码获取网络和本地视频时长等信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目標
最近項目中需要對上傳的本地視頻和從其他服務器拷貝的視頻進行校驗功能,校驗主要包括視頻的時長,大小,格式等信息,那么如何獲取這些信息呢?思路
本地視頻,從過文件流讀取,再通過FFMpeg.exe獲取到相關視頻信息; 網絡視頻,通過http下載到本地臨時文件,在通過上述本地視頻方法獲取相關信息過程
1.下載ffmpeg.exe,下載地址自行百度; 2.將ffmpeg.exe的絕對路徑,拷貝放進如下代碼中; 3.詳細代碼實現過程如下 public void testVideo(String path) { // File source = new File("D:\\Temp\\gequ.mp4");File source = new File(path); // File source = new File("http:\\10.50.13.102:21\\opt\\wacos\\smpnas\\2018\\06\\2\\39748380.mp4");FFMPEGLocator locator = new FFMPEGLocator() {@Overrideprotected String getFFMPEGExecutablePath() {// <ffmpeg_path>是你的ffmpeg.exe路徑return "D:\\DevelopTools\\ffmpeg-20180602-5ee2030-win64-static\\bin\\ffmpeg.exe";}};Encoder encoder = new Encoder(locator);FileChannel fc = null;String size = "";try {it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);long ls = m.getDuration();System.out.println("此視頻時長為:" + ls / 60000 + "分" + (ls) / 1000 + "秒!");//視頻幀寬高System.out.println("此視頻高度為:" + m.getVideo().getSize().getHeight());System.out.println("此視頻寬度為:" + m.getVideo().getSize().getWidth());System.out.println("此視頻格式為:" + m.getFormat());FileInputStream fis = new FileInputStream(source);fc = fis.getChannel();BigDecimal fileSize = new BigDecimal(fc.size());size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";System.out.println("此視頻大小為" + size);} catch (Exception e) {e.printStackTrace();} finally {if (null != fc) {try {fc.close();} catch (IOException e) {e.printStackTrace();}}}} public File inputstreamtofile(InputStream ins, String ext) throws IOException{File f = new File("d:\\Temp");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;} public InputStream getImgFromUrl(String url) throws IOException{//Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.167.129.102", 8080));URL myUrl = new URL(url);HttpURLConnection con = (HttpURLConnection) myUrl.openConnection();con.setConnectTimeout(5*1000);InputStream is = con.getInputStream();return is;} /*** 遠程文件(視頻/圖片)存儲* @throws IOException*/@Testpublic void insertBlobRemote() throws Exception { // String url = "http://ec4.images-amazon.com/images/I/51XMC7MVcFL._SX258_BO1,204,203,200_.jpg";String url = "http://10.50.13.68:18080/images/luping.mp4";String ext = url.substring(url.lastIndexOf("."));//獲取文件InputStream inputStream = getImgFromUrl(url);//保存成FileFile file = inputstreamtofile(inputStream, ext);FileInputStream fis = new FileInputStream(file);String path = file.getPath();System.out.println("-------臨時文件路徑----------"+path);testVideo(path);fis.close();//刪除臨時文件 // file.deleteOnExit();} 4.運行結果如下圖總結
1.需要下載ffmpeg.exe,如果應用部署到Linux上,也需將此文件上傳上去,并配置正確的文件路徑; 2.網絡視頻需要下載到本地再進行處理,如果視頻較大的話,比較耗時; 3.網絡視頻下載下來,如果怕占用空間的話,獲取到視頻相關信息后需要及時刪掉,即退出時執行file.deleteOnExit();總結
以上是生活随笔為你收集整理的Java代码获取网络和本地视频时长等信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何理解界面陷阱电荷呢(interfac
- 下一篇: moviepy中视频读写与预览