0
点赞
收藏
分享

微信扫一扫

简单粗暴的java m3u8下载代码

以前干嘛去了 2022-03-24 阅读 29
java

import java.io.*;
import java.net.*;
import java.nio.channels.*;
import java.security.spec.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class Main
{
    public static void main(String[] args)
    {
        try{
            System.out.println("输入.m3u8结尾的下载链接");
            Scanner scanner=new Scanner(System.in);
            String string=scanner.nextLine();
            System.out.println("输入保存的路径,以\"/\"结尾");
            String path=scanner.nextLine();
            System.out.println("输入文件名,不要加后缀");
            String temp_name=scanner.nextLine();
            String name=temp_name;
            File file=new File(path+name);
            int i=1;//防止路径下的文件名重复
            while(file.exists()){
                i++;
                name=temp_name+"-"+i;
                file=new File(path+name);
            }
            file.mkdirs();
            file=new File(path+name+"/未解密片段/");
            file.mkdir();
            file=new File(path+name+"/片段/");
            file.mkdir();
            String key=null;
            i=0;//i置0,在for循环中自增,用于命名ts片段
            System.out.println("下载中,离开此界面将中断");
            boolean b=true;
            while(b){
                b= false;
                URL url=new URL(string);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                InputStream inputStream=httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                for(String line=bufferedReader.readLine();line!=null;line=bufferedReader.readLine()){
                    if(line.endsWith(".ts")){
                        if(i>2){break;}
                        if(line.startsWith("http")){
                        }else if(line.startsWith("/")){
                            line.substring(0,string.indexOf("/",10));
                        }else{
                            line=string.substring(0,string.lastIndexOf("/")+1)+line;
                            
                        }
                        if(key==null){
                            inputStream=new URL(line).openStream();
                            ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
                            FileOutputStream fileOutputStream=new FileOutputStream(path+name+"/片段/"+i);
                            fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
                            readableByteChannel.close();
                            fileOutputStream.close();
                            fileOutputStream.getChannel().close();
                            inputStream.close();
                        }else{
                            inputStream=new URL(line).openStream();
                            ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
                            FileOutputStream fileOutputStream=new FileOutputStream(path+name+"/未解密片段/"+i);
                            fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
                            readableByteChannel.close();
                            fileOutputStream.close();
                            fileOutputStream.getChannel().close();
                            inputStream.close();
                            FileInputStream input = new FileInputStream(path+name+"/未解密片段/"+i);
                            byte[] byt = new byte[input.available()];
                            input.read(byt);
                            FileOutputStream outputStream= new FileOutputStream(path+name+"/片段/"+i);
                            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
                            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("utf-8"), "AES");
                            AlgorithmParameterSpec paramSpec = new IvParameterSpec(new byte[16]);
                            cipher.init(Cipher.DECRYPT_MODE, keySpec, paramSpec);
                            byt= cipher.doFinal(byt);
                            outputStream.write(byt);
                            outputStream.close();
                            input.close();
                        }
                        System.out.println(i+line);
                        i++;
                    }else if(line.startsWith("#EXT-X-KEY")){
                        line=line.substring(line.indexOf("\"")+1,line.lastIndexOf("\""));
                        if(line.contains("http")){
                        }else if(line.startsWith("/")){
                            string.substring(0,string.indexOf("/",10));
                        }else{
                            line=string.substring(0,string.lastIndexOf("/")+1)+line;
                        }
                        URL keyUrl=new URL(line);
                        HttpURLConnection keyHttpURLConnection=(HttpURLConnection)keyUrl.openConnection();
                        InputStreamReader keyInputStreamReader = new InputStreamReader(keyHttpURLConnection.getInputStream(), "UTF-8");
                        BufferedReader keyBufferedReader = new BufferedReader(keyInputStreamReader);
                        line=keyBufferedReader.readLine();
                        key=line.trim();
                    }else if(line.endsWith(".m3u8")){
                        if(line.startsWith("http")){
                        }else if(line.startsWith("/")){
                            string.substring(0,string.indexOf("/",10));
                        }else{
                            string=string.substring(0,string.lastIndexOf("/")+1)+line;
                        }
                        b=true;
                        break;//提前结束for循环,回到while循环
                    }
                }
            }
            FileInputStream fileInputStream;
            FileOutputStream fileOutputStream = new FileOutputStream(path+name+"/"+name+".ts");
            byte by[] = new byte[1024 * 10];
            int len;
            file = new File(path+name+"/片段/");
            if(file.isDirectory()) { 
                String[] fileNames = file.list();
                int lenth=fileNames.length;
                i=0;
                while(i<lenth){
                    fileInputStream=new FileInputStream(path+name+"/片段/"+i);
                    len = 0;
                    while ((len = fileInputStream.read(by))!=-1) {
                        fileOutputStream.write(by, 0, len);
                    }
                    fileInputStream.close();
                    i++;
                }
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            System.out.println("合并完成!");
        }catch (Exception e){
            System.out.println("出错了"+e.toString());
        }
    }
    
    
}

举报

相关推荐

0 条评论