栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单粗暴的java m3u8下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单粗暴的java m3u8下载

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 fileInputStream = new FileInputStream(path+name+"/未解密片段/"+i);
                            byte[] by = new byte[fileInputStream.available()];
                            fileInputStream.read(by);
                            fileOutputStream= new FileOutputStream(path+name+"/片段/"+i);
                            Cipher cipher = Cipher.getInstance("AES");
                            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("utf-8"), "AES");
                            AlgorithmParameterSpec algorithmParameterSpec = new IvParameterSpec(new byte[16]);
                            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, algorithmParameterSpec);
                            by= cipher.doFinal(by);
                            fileOutputStream.write(by);
                            fileOutputStream.close();
                            fileInputStream.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                     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());
        }
    }
    
    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778532.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号