java下载m3u8视频(拉勾视频为例)

2023-10-08 03:50

本文主要是介绍java下载m3u8视频(拉勾视频为例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • java下载m3u8视频
    • 1.创建工具类
    • 2.下载m3u8文件
    • 3.下载key
    • 4.关于ts文件
    • 5.合成视频
    • 6.运行
  • 二、关于ffmpeg
    • 1.简单用法
    • 2.创建工具类

java下载m3u8视频

m3u8视频主要
由 m3u8文件、key、ts文件 三部分组成

本篇以拉勾视频为例

在这里插入图片描述

1.创建工具类

@Slf4j
public class M3U8Factory {private RestTemplate restTemplate = new RestTemplate();//外部输入参数private String title;//视频名字private String dir;//本地文件夹private String uri;//host地址private String m3u8;//m3u8原文件名+后缀private String m3u8Path;//m3u8本地保存路径// 下载拉勾key的请求参数private String cookie;private String code;private String vid;public void setM3u8(String m3u8Url, String title, String outPath) {this.uri = m3u8Url.substring(0, m3u8Url.lastIndexOf("/") + 1);this.m3u8 = m3u8Url.substring(m3u8Url.lastIndexOf("/") + 1);this.title = title;this.dir = outPath;}public void setParam(String cookie, String code, String vid) {this.cookie = cookie;this.vid = vid;this.code = code;}public void build() {init();downloadKey(); //下载keyString m3u8Content = downloadM3U8();//下载m3u8文件downloadTs(m3u8Content); //下载ts//downloadTsThread(m3u8Content); //多线程下载updateM3U8File(); //修改m3u8merge(); //合并}private void init() {File directory = new File(dir);if (directory.exists() == true) {log.info("目录已存在");} else {directory.mkdirs();log.info("目录创建完成");}}
}

2.下载m3u8文件

    /*** 下载m3u8文件,包含key的下载地址(如果有) 和 ts文件名*/public String downloadM3U8() {log.info("-- 开始下载 m3u8 --");ResponseEntity<byte[]> forEntity = restTemplate.getForEntity(uri + m3u8, byte[].class);//log.info("m3u8 内容 = {}", new String(forEntity.getBody()));if (title == null) {m3u8Path = dir + "\\\\" + m3u8;} else {m3u8Path = dir + "\\\\" + title + ".m3u8";}File file = new File(m3u8Path);try {Files.write(file.toPath(), Objects.requireNonNull(forEntity.getBody(), "未获取到文件"));} catch (IOException e) {e.printStackTrace();}return new String(forEntity.getBody());}

3.下载key

    public String downloadKey() {log.info("-- 开始下载 key --");String uri = "https://kaiwu.lagou.com/alikey?code={code}&vid={vid}&appId={appId}";String key = null;restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());// params  如果有的话LinkedHashMap<String, String> param = new LinkedHashMap<>();param.put("code", code);param.put("vid", vid);param.put("appId", "big_course");// herders  如果有的话HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("user-agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Mobile Safari/537.36");httpHeaders.add("cookie", cookie);HttpEntity<Object> request = new HttpEntity<>(httpHeaders);try {ResponseEntity<byte[]> entity = restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class, param);key = new String(entity.getBody());log.info("key = {}", key);File file = new File(dir + "//key.key");Files.write(file.toPath(), Objects.requireNonNull(entity.getBody(), "未获取到文件"));} catch (HttpClientErrorException e) {log.info("非200请求 = {}", e);throw e;} catch (IOException e) {e.printStackTrace();}return key;}

4.关于ts文件

两种处理方式

  • 下载到本地合成
    将 m3u8文件中,每个ts文件加上 本地路径
  • 在线合成
    将 m3u8文件中,每个ts文件加上 uri
    //修改m3u8文件private void updateM3U8File() {String keyPath = dir + "\\\\key.key";log.info("-------------------------------------");log.info("keyPath = {}", keyPath);log.info("修改m3u8文件");File file = new File(m3u8Path);String line = null;StringBuffer stringBuffer = new StringBuffer();BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new FileReader(file));while ((line = br.readLine()) != null) {if (line.startsWith("#EXT-X-KEY")) {line = "#EXT-X-KEY:METHOD=AES-128,URI=\"" + keyPath + "\"";}if (line.endsWith(".ts")) {//修改ts文件地址//line = uri + line;  //在线line = dir + "\\\\" + line; //本地}stringBuffer.append(line + "\n");}//System.out.println("stringBuffer: " + stringBuffer);bw = new BufferedWriter(new FileWriter(file));bw.write(stringBuffer.toString());bw.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (bw != null) {bw.close();}if (br != null) {br.close();}} catch (IOException e) {e.printStackTrace();}}}
    //获取ts文件名public List<String> getTsList(String m3u8Content) {List<String> tsList = new ArrayList<>();BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(m3u8Content.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));String line;try {while ((line = br.readLine()) != null) {if (line.endsWith("ts")) {tsList.add(line);}}} catch (IOException e) {e.printStackTrace();}tsList.forEach(System.out::println);return tsList;}//下载ts文件public void downloadTs(String m3u8Content) {List<String> tsList = getTsList(m3u8Content);long startTime = System.currentTimeMillis();//开始时间for (String name : tsList) {ResponseEntity<byte[]> forEntity = restTemplate.getForEntity((uri + name), byte[].class);//System.out.println("结果 = " + forObject);File file = new File((dir + "//" + name));try {Files.write(file.toPath(), Objects.requireNonNull(forEntity.getBody(), "未获取到文件"));log.info("写入文件 = {}", file.toPath());} catch (IOException e) {e.printStackTrace();}}long endTime = System.currentTimeMillis();//结束时间log.info("ts文件下载耗时:" + (endTime - startTime) + "ms");}

5.合成视频

工具类在下面

    public void merge() {//例子:FfmpegUtils.mergeVedio("D:\\下载m3u8\\index.m3u8","D:\\下载m3u8\\new.flv");log.info("-------------------------------------");log.info("输入地址 = {}", m3u8Path);log.info("输出地址 = {}", dir + "\\\\" + title + ".mp4");log.info("开始合成");FfmpegUtils.mergeVedio(m3u8Path, dir + "\\\\" + title + ".mp4");}

6.运行

public class Start {//爬取拉勾视频,cookie不用变,变的是 m3u8地址 和 key地址(cookie,code,vid,appId)private static String cookie = "填入你的cookie";public static void main(String[] args) {M3U8Factory m3U8Factory = new M3U8Factory();m3U8Factory.setM3u8("https://vod.lagou.com/a6951d113cee4c3380bd3b13f40f1c98/34e9c9e0cc677e60f77e22f6395eda89-sd-encrypt-stream.m3u8", "index", "d://下载视频");m3U8Factory.setParam(cookie, "MTIyMjgzZDctNTY0Yy00MTM4LTg2MGMtYmJlN2U1YzFlNTFkOTl3R0RDNFpLMndsQUtzMlVjclRHYmRKa2wxbCtsbHRBQUFBQUFBQUFBQThXcUgrV0x2Vmt2UXZPWjlDWTVlSDkyZVg1NjV4NVpIWFRGV0RYenk1ZmhmbGRJVVhtU0kr", "a6951d113cee4c3380bd3b13f40f1c98");m3U8Factory.build();}
}

在这里插入图片描述

二、关于ffmpeg

1.简单用法

cmd 运行

  • 本地合成
    ffmpeg -y -allowed_extensions ALL -i D:\下载m3u8\index.m3u8 -c copy D:\下载m3u8\new.MP4
  • 在线合成
    ffmpeg -y -allowed_extensions ALL -protocol_whitelist "file,http,https,crypto,tcp,tls" -i D:\下载m3u8\index.m3u8 -c copy D:\下载m3u8\new.MP4

2.创建工具类

@Slf4j
public class FfmpegUtils {private String inputPath;private String outputPath;private static String ffmpegPath = "D:\\Program Files\\ffmpeg-4.3.1\\bin\\ffmpeg.exe"; // 填你本地的ffmpeg.exe路径public static void mergeVedio(String inputPath, String outputPath) {StringBuffer command = new StringBuffer("");command.append(ffmpegPath);command.append(" -y");command.append(" -allowed_extensions");command.append(" ALL");//command.append(" -protocol_whitelist");//command.append(" file,http,https,crypto,tcp,tls");command.append(" -i");command.append(" " + inputPath);command.append(" -c");command.append(" copy");command.append(" " + outputPath);try {final Process process = Runtime.getRuntime().exec(command.toString());log.info("start run cmd {}", command);//⚠️此处代码是因为如果合并大视频文件会产生大量的日志缓存导致线程阻塞,最终合并失败,所以加两个线程处理日志的缓存,之后再调用waitFor方法,等待执行结果。//打印输出信息new Thread(() -> {BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;try {while ((line = in.readLine()) != null) {System.out.println("output:" + line);}} catch (IOException e) {e.printStackTrace();} finally {try {in.close();} catch (IOException e) {e.printStackTrace();}}}).start();//打印错误信息new Thread(() -> {BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));String line = null;try {while ((line = err.readLine()) != null) {System.out.println("err:" + line);}} catch (IOException e) {e.printStackTrace();} finally {try {err.close();} catch (IOException e) {e.printStackTrace();}}}).start();// 等待命令子线程执行完成process.waitFor();process.destroy();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}

这篇关于java下载m3u8视频(拉勾视频为例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/162504

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有