java 下载视频并合并

发布时间:2022-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java 下载视频并合并脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

代码

import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Client {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {

       merge(2,985);

    }

    static void multidownload() throws IOException, ExecutionException, InterruptedException {
        List<CompletableFuture<Integer> > list = new ArrayList<>();
        for (int i = 50; i < 986; i+=10) {
            final int index = i;
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
                try {
                    download(index, Math.max(index + 10,985));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return 1;
            });
            list.add(future);
        }

        for (CompletableFuture<Integer> future : list) {
            future.get();
        }
    }
    static void merge(int start,int end) throws IOException {
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("/Users/liyh/Movies/gjkc/one.ts"));
        byte[] buffer = new byte[1024];

        for (int i = start; i <= end; i ++) {
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("/Users/liyh/Movies/gjkc/gjkc" + String.format("%03d", i) +".ts"));
            int readcount;
            while ((readcount = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, readcount);
            }
            inputStream.close();
        }
        outputStream.close();
        System.out.println("merge suc");
    }
    static RestTemplate restTemplate = new RestTemplate();
    static void download(int start,int end) throws IOException {

        for (int i = start; i < end; i++) {
            String num = String.format("%03d", i);
            String url = "https://m001.m1m2m3u8mp4.com/G0107/gjkc/gjkc" + num + ".ts";

            HttpHeaders headers = new HttpHeaders();
            HttpEntity<Resource> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET,httpEntity, byte[].class);

//得到的是一个二进制的byte[]  将它转成流
            InputStream in = new ByteArrayInputStream(response.getBody());
            Path path = Paths.get("/Users/liyh/Movies/gjkc/gjkc"+num+".ts");
            Files.copy(in,path);
            System.out.println("suc download:" + url);
        }


    }
}

 

脚本宝典总结

以上是脚本宝典为你收集整理的java 下载视频并合并全部内容,希望文章能够帮你解决java 下载视频并合并所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: