java將m3u8格式轉成視頻文件的方法
這是一次嘗試,android手機將在線的m3u8小電影保存到手機端,手機端把文件復制到電腦端。然后使用小工具合并成可播放的視頻。
/** * 合并視頻文件 * */public class MergeVideos { /** * source為源地址,destination為合并之后的文件地址,videoName為合并后視頻的名字,num為視頻數量 * @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; }
核心代碼如上,再加上一個swing界面,堪稱完美。
目錄選擇方式,可以選擇粘貼,或者文件選擇的方式。
運行完成。合并的文件都好了。
總結
到此這篇關于java將m3u8格式轉成視頻文件的方法的文章就介紹到這了,更多相關java m3u8 視頻內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: