liubing 2 vuotta sitten
vanhempi
commit
3bf3aeba37

+ 10 - 11
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/endpoint/statistics/DetectionPlatformEndpoint.java

@ -8,7 +8,6 @@ import com.yihu.jw.care.service.device.NetworkCardService;
import com.yihu.jw.care.service.device.PatientDeviceService;
import com.yihu.jw.care.service.security.SecurityMonitoringOrderService;
import com.yihu.jw.care.service.statistics.DetectionPlatformService;
import com.yihu.jw.care.util.HlsDownloader;
import com.yihu.jw.restmodel.ResponseContant;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
@ -459,16 +458,16 @@ public class DetectionPlatformEndpoint extends EnvelopRestEndpoint {
        }
    }
    @PostMapping(value = "testDownloadVideoUrl")
    @ApiOperation(value = "下载m3u8")
    public ObjEnvelop downM3u8Video(@RequestParam String url) {
        try {
            return platformService.downM3u8Video(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ObjEnvelop.getSuccess("1111","");
    }
//    @PostMapping(value = "testDownloadVideoUrl")
//    @ApiOperation(value = "下载m3u8")
//    public ObjEnvelop downM3u8Video(@RequestParam String url) {
//        try {
//            return platformService.downM3u8Video(url);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return ObjEnvelop.getSuccess("1111","");
//    }
//    public static void main(String[] args) {

+ 45 - 46
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/service/statistics/DetectionPlatformService.java

@ -9,7 +9,6 @@ import com.yihu.jw.care.dao.device.PatientDeviceDao;
import com.yihu.jw.care.service.device.DeviceService;
import com.yihu.jw.care.service.role.RoleService;
import com.yihu.jw.care.service.sign.ServicePackageService;
import com.yihu.jw.care.util.HlsDownloader;
import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.org.BaseOrgDO;
@ -809,49 +808,49 @@ public class DetectionPlatformService  {
        return null;
    }
    /**
     * 将m3u8链接保存到本地的mp4格式
     * @param originUrlpath
     * @return
     */
    public ObjEnvelop downM3u8Video(String originUrlpath){
        //文件存放的路径
        //TODO 替换成linux系统路径
        String rootPath = "E:\\videodir";
        //String originUrlpath = "http://videocdn.renrenjiang.cn/Act-ss-m3u8-sd/1037359_1546064640169/1037359_1546064640169.m3u8";
        String preUrlPath = originUrlpath.substring(0, originUrlpath.lastIndexOf("/")+1);
        String fileName = "";
        HlsDownloader downLoader = new HlsDownloader(originUrlpath, preUrlPath, rootPath);
        //downLoader.setThreadQuantity(10);
        try{
            //是否开启异步下载
            fileName = downLoader.download(true);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        /*String fileName = "111.mp4";
        String rootPath = "D:\\xiao\\Videos";*/
        if(StringUtils.isNotBlank(fileName)){
            System.err.println("下载成功"+fileName);
            System.out.println("完整路径:"+rootPath+"\\"+fileName);
            try {
                //文件上传
                UploadVO uploadVO = fileUploadService.uploadStream(new FileInputStream(rootPath + "\\" + fileName), fileName, fastdfs_file_url);
                return ObjEnvelop.getSuccess("成功",uploadVO);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //TODO 删除文件
            }
        }
        return ObjEnvelop.getError("失败");
    }
    public static void main(String[] args) {
        String sss= "qwe.mp4";
        System.out.println(sss.substring(0,sss.lastIndexOf(".")));
    }
//    /**
//     * 将m3u8链接保存到本地的mp4格式
//     * @param originUrlpath
//     * @return
//     */
//    public ObjEnvelop downM3u8Video(String originUrlpath){
//        //文件存放的路径
//        //TODO 替换成linux系统路径
//        String rootPath = "E:\\videodir";
//        //String originUrlpath = "http://videocdn.renrenjiang.cn/Act-ss-m3u8-sd/1037359_1546064640169/1037359_1546064640169.m3u8";
//        String preUrlPath = originUrlpath.substring(0, originUrlpath.lastIndexOf("/")+1);
//        String fileName = "";
//        HlsDownloader downLoader = new HlsDownloader(originUrlpath, preUrlPath, rootPath);
//        //downLoader.setThreadQuantity(10);
//        try{
//            //是否开启异步下载
//            fileName = downLoader.download(true);
//        }
//        catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        /*String fileName = "111.mp4";
//        String rootPath = "D:\\xiao\\Videos";*/
//        if(StringUtils.isNotBlank(fileName)){
//            System.err.println("下载成功"+fileName);
//            System.out.println("完整路径:"+rootPath+"\\"+fileName);
//
//            try {
//                //文件上传
//                UploadVO uploadVO = fileUploadService.uploadStream(new FileInputStream(rootPath + "\\" + fileName), fileName, fastdfs_file_url);
//                return ObjEnvelop.getSuccess("成功",uploadVO);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }finally {
//                //TODO 删除文件
//            }
//        }
//        return ObjEnvelop.getError("失败");
//    }
//
//    public static void main(String[] args) {
//        String sss= "qwe.mp4";
//        System.out.println(sss.substring(0,sss.lastIndexOf(".")));
//    }
}

+ 213 - 213
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/util/HlsDownloader.java

@ -1,217 +1,217 @@
package com.yihu.jw.care.util;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// https://my.oschina.net/haitaohu/blog/1941179
public class HlsDownloader {

    private String uuid;
    private String originUrlpath;
    private String preUrlPath;
    private String rootPath;
    private String fileName;
    private String folderPath;
    private int threadQuantity = 10;

    public int getThreadQuantity() {
        return threadQuantity;
    }

    public void setThreadQuantity(int threadQuantity) {
        this.threadQuantity = threadQuantity;
    }


    public HlsDownloader(String originUrlpath, String preUrlPath, String rootPath){
        this.uuid = UUID.randomUUID().toString().replaceAll("-","");
        this.originUrlpath = originUrlpath;
        this.preUrlPath = preUrlPath;
        this.rootPath = rootPath;

        this.fileName = uuid+ ".mp4";

        this.folderPath = rootPath + File.separator + uuid;
        File file = new File(folderPath);
        if(!file.exists()) file.mkdirs();
    }

    public String download(boolean isAsync) throws Exception {

        String indexStr = getIndexFile();

        List urlList = analysisIndex(indexStr);

        HashMap<Integer,String> keyFileMap = new HashMap<>();

        if(isAsync){
            downLoadIndexFileAsync(urlList, keyFileMap);

            while (keyFileMap.size()<urlList.size()){
                //System.out.println("当前下载数量"+keyFileMap.size());
                Thread.sleep(3000);
            }
        }else{
            keyFileMap = downLoadIndexFile(urlList);
        }

        return composeFile(keyFileMap);
    }

    /* 下载索引文件 */
    public String getIndexFile() throws Exception{
        URL url = new URL(originUrlpath);

        /*File file = new File("E:\\JKZL\\1.m3u8");
        url = file.toURL();*/
        //下载资源
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));

        String content = "" ;
        String line;
        while ((line = in.readLine()) != null) {
            content += line + "\n";
        }
        in.close();

        return content;
    }

    /* 解析索引文件 */
    public List analysisIndex(String content) throws Exception{
        Pattern pattern = Pattern.compile(".*ts");
        Matcher ma = pattern.matcher(content);

        List<String> list = new ArrayList<String>();

        while(ma.find()){
            list.add(ma.group());
        }

        return list;
    }

    /* 下载视频片段 */
    public HashMap downLoadIndexFile(List<String> urlList){
        HashMap<Integer,String> keyFileMap = new HashMap<>();

        for(int i =0;i<urlList.size();i++){
            String subUrlPath = urlList.get(i);
            String fileOutPath = folderPath + File.separator + i + ".ts";
            keyFileMap.put(i, fileOutPath);
            try{
                downloadNet(preUrlPath + subUrlPath, fileOutPath);

                System.out.println("成功:"+ (i + 1) +"/" + urlList.size());
            }catch (Exception e){
                System.err.println("失败:"+ (i + 1) +"/" + urlList.size());
            }
        }

        return  keyFileMap;
    }

    public void downLoadIndexFileAsync(List<String> urlList, HashMap<Integer,String> keyFileMap) throws Exception{
        int downloadForEveryThread = (urlList.size() + threadQuantity - 1)/threadQuantity;
        if(downloadForEveryThread == 0) downloadForEveryThread = urlList.size();

        for(int i=0; i<urlList.size();i+=downloadForEveryThread){
            int startIndex = i;
            int endIndex = i + downloadForEveryThread - 1;
            if(endIndex >= urlList.size())
                endIndex = urlList.size() - 1;

            new DownloadThread(urlList, startIndex, endIndex, keyFileMap).start();
        }
    }
    /**
     * 视频片段合成
     */
    public String composeFile(HashMap<Integer,String> keyFileMap) throws Exception{

        if(keyFileMap.isEmpty()) return null;

        String fileOutPath = rootPath + File.separator + fileName;
        FileOutputStream fileOutputStream = new FileOutputStream(new File(fileOutPath));
        byte[] bytes = new byte[1024];
        int length = 0;
        for(int i=0; i<keyFileMap.size(); i++){
            String nodePath = keyFileMap.get(i);
            File file = new File(nodePath);
            if(!file.exists())  continue;

            FileInputStream fis = new FileInputStream(file);
            while ((length = fis.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, length);
            }
        }

        return fileName;
    }


    class DownloadThread extends Thread{
        private List<String> urlList;
        private int startIndex;
        private int endIndex;
        private HashMap<Integer,String> keyFileMap;

        public DownloadThread(List<String> urlList, int startIndex, int endIndex, HashMap<Integer,String> keyFileMap){
            this.urlList = urlList;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.keyFileMap = keyFileMap;
        }
        @Override
        public void run(){
            for(int i=startIndex;i<=endIndex;i++){
                String subUrlPath = urlList.get(i);
                String fileOutPath = folderPath + File.separator + i + ".ts";
                keyFileMap.put(i, fileOutPath);
                String message = "%s: 线程 " + (startIndex/(endIndex - startIndex) + 1)
                        + ", "+ (i + 1) +"/" + urlList.size() +", 合计: %d";
                try{
                    downloadNet(preUrlPath + subUrlPath, fileOutPath);

                    System.out.println(String.format(message, "成功", keyFileMap.size()));
                }catch (Exception e){
                    System.err.println(String.format(message, "失败", keyFileMap.size()));
                }
            }
        }
    }

    private void downloadNet(String fullUrlPath, String fileOutPath) throws Exception {
        // 下载网络文件
//        DataInputStream dataInputStream = new DataInputStream(url.openStream());
//package com.yihu.jw.care.util;
//
//import java.io.*;
//import java.net.URL;
//import java.net.URLConnection;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.UUID;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//// https://my.oschina.net/haitaohu/blog/1941179
//public class HlsDownloader {
//
//    private String uuid;
//    private String originUrlpath;
//    private String preUrlPath;
//    private String rootPath;
//    private String fileName;
//    private String folderPath;
//    private int threadQuantity = 10;
//
//    public int getThreadQuantity() {
//        return threadQuantity;
//    }
//
//    public void setThreadQuantity(int threadQuantity) {
//        this.threadQuantity = threadQuantity;
//    }
//
//
//    public HlsDownloader(String originUrlpath, String preUrlPath, String rootPath){
//        this.uuid = UUID.randomUUID().toString().replaceAll("-","");
//        this.originUrlpath = originUrlpath;
//        this.preUrlPath = preUrlPath;
//        this.rootPath = rootPath;
//
//        this.fileName = uuid+ ".mp4";
//
//        this.folderPath = rootPath + File.separator + uuid;
//        File file = new File(folderPath);
//        if(!file.exists()) file.mkdirs();
//    }
//
//    public String download(boolean isAsync) throws Exception {
//
//        String indexStr = getIndexFile();
//
//        List urlList = analysisIndex(indexStr);
//
//        HashMap<Integer,String> keyFileMap = new HashMap<>();
//
//        if(isAsync){
//            downLoadIndexFileAsync(urlList, keyFileMap);
//
//            while (keyFileMap.size()<urlList.size()){
//                //System.out.println("当前下载数量"+keyFileMap.size());
//                Thread.sleep(3000);
//            }
//        }else{
//            keyFileMap = downLoadIndexFile(urlList);
//        }
//
//        return composeFile(keyFileMap);
//    }
//
//    /* 下载索引文件 */
//    public String getIndexFile() throws Exception{
//        URL url = new URL(originUrlpath);
//
//        /*File file = new File("E:\\JKZL\\1.m3u8");
//        url = file.toURL();*/
//        //下载资源
//        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
//
//        String content = "" ;
//        String line;
//        while ((line = in.readLine()) != null) {
//            content += line + "\n";
//        }
//        in.close();
//
//        return content;
//    }
//
//    /* 解析索引文件 */
//    public List analysisIndex(String content) throws Exception{
//        Pattern pattern = Pattern.compile(".*ts");
//        Matcher ma = pattern.matcher(content);
//
//        List<String> list = new ArrayList<String>();
//
//        while(ma.find()){
//            list.add(ma.group());
//        }
//
//        return list;
//    }
//
//    /* 下载视频片段 */
//    public HashMap downLoadIndexFile(List<String> urlList){
//        HashMap<Integer,String> keyFileMap = new HashMap<>();
//
//        for(int i =0;i<urlList.size();i++){
//            String subUrlPath = urlList.get(i);
//            String fileOutPath = folderPath + File.separator + i + ".ts";
//            keyFileMap.put(i, fileOutPath);
//            try{
//                downloadNet(preUrlPath + subUrlPath, fileOutPath);
//
//                System.out.println("成功:"+ (i + 1) +"/" + urlList.size());
//            }catch (Exception e){
//                System.err.println("失败:"+ (i + 1) +"/" + urlList.size());
//            }
//        }
//
//        return  keyFileMap;
//    }
//
//    public void downLoadIndexFileAsync(List<String> urlList, HashMap<Integer,String> keyFileMap) throws Exception{
//        int downloadForEveryThread = (urlList.size() + threadQuantity - 1)/threadQuantity;
//        if(downloadForEveryThread == 0) downloadForEveryThread = urlList.size();
//
//        for(int i=0; i<urlList.size();i+=downloadForEveryThread){
//            int startIndex = i;
//            int endIndex = i + downloadForEveryThread - 1;
//            if(endIndex >= urlList.size())
//                endIndex = urlList.size() - 1;
//
//            new DownloadThread(urlList, startIndex, endIndex, keyFileMap).start();
//        }
//    }
//    /**
//     * 视频片段合成
//     */
//    public String composeFile(HashMap<Integer,String> keyFileMap) throws Exception{
//
//        if(keyFileMap.isEmpty()) return null;
//
//        String fileOutPath = rootPath + File.separator + fileName;
//        FileOutputStream fileOutputStream = new FileOutputStream(new File(fileOutPath));
//        byte[] bytes = new byte[1024];
//        int length = 0;
//        while ((length = dataInputStream.read(bytes)) != -1) {
//            fileOutputStream.write(bytes, 0, length);
//        for(int i=0; i<keyFileMap.size(); i++){
//            String nodePath = keyFileMap.get(i);
//            File file = new File(nodePath);
//            if(!file.exists())  continue;
//
//            FileInputStream fis = new FileInputStream(file);
//            while ((length = fis.read(bytes)) != -1) {
//                fileOutputStream.write(bytes, 0, length);
//            }
//        }
//
//        return fileName;
//    }
//
//
////    class DownloadThread extends Thread{
////        private List<String> urlList;
////        private int startIndex;
////        private int endIndex;
////        private HashMap<Integer,String> keyFileMap;
////
////        public DownloadThread(List<String> urlList, int startIndex, int endIndex, HashMap<Integer,String> keyFileMap){
////            this.urlList = urlList;
////            this.startIndex = startIndex;
////            this.endIndex = endIndex;
////            this.keyFileMap = keyFileMap;
////        }
////        @Override
////        public void run(){
////            for(int i=startIndex;i<=endIndex;i++){
////                String subUrlPath = urlList.get(i);
////                String fileOutPath = folderPath + File.separator + i + ".ts";
////                keyFileMap.put(i, fileOutPath);
////                String message = "%s: 线程 " + (startIndex/(endIndex - startIndex) + 1)
////                        + ", "+ (i + 1) +"/" + urlList.size() +", 合计: %d";
////                try{
////                    downloadNet(preUrlPath + subUrlPath, fileOutPath);
////
////                    System.out.println(String.format(message, "成功", keyFileMap.size()));
////                }catch (Exception e){
////                    System.err.println(String.format(message, "失败", keyFileMap.size()));
////                }
////            }
////        }
////    }
//
//    private void downloadNet(String fullUrlPath, String fileOutPath) throws Exception {
//        // 下载网络文件
////        DataInputStream dataInputStream = new DataInputStream(url.openStream());
////        FileOutputStream fileOutputStream = new FileOutputStream(new File(fileOutPath));
////        byte[] bytes = new byte[1024];
////        int length = 0;
////        while ((length = dataInputStream.read(bytes)) != -1) {
////            fileOutputStream.write(bytes, 0, length);
////        }
////        dataInputStream.close();
//
//        //int bytesum = 0;
//        int byteread = 0;
//
//        URL url = new URL(fullUrlPath);
//        URLConnection conn = url.openConnection();
//        InputStream inStream = conn.getInputStream();
//        FileOutputStream fs = new FileOutputStream(fileOutPath);
//
//        byte[] buffer = new byte[1204];
//        while ((byteread = inStream.read(buffer)) != -1) {
//            //bytesum += byteread;
//            fs.write(buffer, 0, byteread);
//        }
//        dataInputStream.close();

        //int bytesum = 0;
        int byteread = 0;

        URL url = new URL(fullUrlPath);
        URLConnection conn = url.openConnection();
        InputStream inStream = conn.getInputStream();
        FileOutputStream fs = new FileOutputStream(fileOutPath);

        byte[] buffer = new byte[1204];
        while ((byteread = inStream.read(buffer)) != -1) {
            //bytesum += byteread;
            fs.write(buffer, 0, byteread);
        }
    }

}
//    }
//
//}