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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. webpack高級配置與優(yōu)化詳解2. XML 增、刪、改和查示例3. ASP常用日期格式化函數(shù) FormatDate()4. SXNA RSS Blog 聚合器程序5. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera6. PHP設(shè)計(jì)模式中工廠模式深入詳解7. XML入門的常見問題(四)8. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法
