m3u8解码和合并工具
Posted at 2019-10-09 笔记 java
12345678910111213141516171819202122232425262728293031
/** * 解密文件 * @param sourcePath 源文件 * @param destinationPath 保存的文件 * @param key 密钥 */public static void decryptFile(String sourcePath,String destinationPath,String key) throws Exception { File outFile = new File(destinationPath); if (!outFile.exists()){ outFile.createNewFile(); } // 待解密的文件输入流 FileInputStream fis = new FileInputStream(sourcePath); FileOutputStream fos = new FileOutputStream(outFile); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] bytes = key.getBytes(); SecretKeySpec secretKey = new SecretKeySpec(bytes,"AES"); IvParameterSpec iv = new IvParameterSpec(key.getBytes()); cipher.init(Cipher.DECRYPT_MODE,secretKey,iv); // 解密流 CipherInputStream cis = new CipherInputStream(fis,cipher); byte[] buffer = new byte[1024]; int n = 0; while ((n = cis.read(buffer)) != -1){ fos.write(buffer,0,n); } System.out.println("解密成功:"); cis.close(); fos.close();}
用来识别m3u8文件
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
public static void main(String[] args) { String sourcePath ="G:\\win下载\\m3u8\\"; String destination = "G:\\win下载\\m3u8\\"; String m3u8 = "G:\\win下载\\m3u8\\m3u8.m3u8"; String key = "tkr87ysao6qehn95"; FileReader fileReader = null; BufferedReader reader = null; ArrayList<String> files = new ArrayList<>(); try{ fileReader = new FileReader(m3u8); reader = new BufferedReader(fileReader); String filename = null; while (( filename = reader.readLine())!=null){ if(filename.charAt(0)!='#'){ files.add(filename); } } if(fileReader!=null){ fileReader.close(); } if(reader!=null){ reader.close(); } }catch (IOException e){ e.printStackTrace(); } int partSize = files.size(); String sourceFile; String destFile; try{ for(int i=0;i < partSize ;i++){ sourceFile = sourcePath + files.get(i); destFile = destination + "e"+files.get(i); EncryFileUtil.decryptFile(sourceFile,destFile,key); } System.out.println("========全部解密成功============"); }catch (Exception e){ e.printStackTrace(); } try { // 合并数据 String mergeFilePath = sourcePath+"merge.ts"; FileOutputStream mergeFile = new FileOutputStream(mergeFilePath); for (int i=0;i<partSize;i++){ String encodeFile = sourcePath +"e"+files.get(i); FileInputStream inputStream = new FileInputStream(encodeFile); byte[] buffer = new byte[1024]; int n=0; while ((n = inputStream.read(buffer))!= -1){ mergeFile.write(buffer,0,n); } inputStream.close(); } System.out.println("合并数据成功"); }catch (IOException e){ e.printStackTrace(); }}
Previous post: book118预览文档下载 Next post: blibli动态9宫格图片下载脚本