java將m3u8格式轉(zhuǎn)成視頻文件的方法
這是一次嘗試,android手機(jī)將在線的m3u8小電影保存到手機(jī)端,手機(jī)端把文件復(fù)制到電腦端。然后使用小工具合并成可播放的視頻。
/** * 合并視頻文件 * */public class MergeVideos { /** * source為源地址,destination為合并之后的文件地址,videoName為合并后視頻的名字,num為視頻數(shù)量 * @param source * @param destination * @throws IOException */ public static void MergeVideos(File source, String destination) throws IOException{ FileOutputStream out = new FileOutputStream(destination); FileInputStream in = null; File[] files = source.listFiles(); for(File file: files){ in = new FileInputStream(file); byte[] bytes = new byte[1024]; int len = 0; while((len = in.read(bytes)) > 0){ out.write(bytes,0,len); } } in.close(); out.close(); }}public class M3u8Util{ /** * 根目錄 * @param root */ public static void findFile(File root) throws IOException { if(root.exists()){ if(root.isDirectory()){File[] categorys=root.listFiles();for(File cate: categorys){ if(rename(cate)){ MergeVideos.MergeVideos(cate,cate.getName()+'.ts'); }} }else{System.out.println('file name: '+root.getName()); } } } /** * 將0 改成00或者 000 * @param cat */ public static boolean rename(File cat){ if(cat.exists()){ if(cat.isDirectory()){File[] files=cat.listFiles();int len=String.valueOf(files.length).length();String file0=files[0].getName();String pre=file0.substring(0,file0.length()-1);Integer max=pre.length()+len;Arrays.stream(files) .filter(temp->max-temp.getName().length()>0) .forEach(item->{ int fill=max-item.getName().length(); String name=''; for(int i=0;i<fill;i++){name+=0; } String n=item.getAbsolutePath().replace(pre,pre+name); item.renameTo(new File(n)); });return true; }else{System.out.println('file name: '+cat.getName()); } } return false; }
核心代碼如上,再加上一個(gè)swing界面,堪稱完美。
目錄選擇方式,可以選擇粘貼,或者文件選擇的方式。
運(yùn)行完成。合并的文件都好了。
總結(jié)
到此這篇關(guān)于java將m3u8格式轉(zhuǎn)成視頻文件的方法的文章就介紹到這了,更多相關(guān)java m3u8 視頻內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過(guò)python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開(kāi)發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
