Java 下载 HLS (m3u8) 视频
下载索引文件
public String getIndexFile() throws Exception{
URL url = new URL(originUrlpath);
//下载资源
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;
}
private void downloadNet(String fullUrlPath, String fileOutPath) throws Exception {
//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);
}
}
多线程下载
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();
}
}
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()));
}
}
}
}
视频片段合成
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;
}
开源地址:hlsdownloader
Java 下载 HLS (m3u8) 视频的更多相关文章
- 如何下载HLS视频到本地(m3u8)
如何下载HLS视频到本地(m3u8)? CreateTime--2018年3月21日16:07:00 Author:Marydon 一.需求 很多连载的动漫需要VIP会员才能观看,而且有的由于版权 ...
- 打造m3u8视频(流视频)下载解密合并器(kotlin)
本文是对我原创工具m3u8视频下载合并器关键代码解析及软件实现的思路的讲解,想要工具的请跳转链接 1.思路说明 思路挺简单,具体步骤如下: 下载m3u8文件 解析m3u8文件获得ts文件列表 根据文件 ...
- 下载m3u8视频
分两种情况 同时支持m3u8和mp4文件 某些视频同时支持m3u8和mp4视频文件,将m3u8改成mp4后直接: wget -c http://www.xxx.com/xxxx.mp4 只有m3u8视 ...
- 下载HLS视频到本地
现在绝大多数网站播放视频都采用HLS技术,像腾讯优酷爱奇艺等等.本篇博文将介绍如何下载这样的视频到本地. 前言 因疫情影响,上课部分课程采用腾讯课堂上课,腾讯课堂有直播回放功能,但这个功能腾讯显然没有 ...
- 基于EasyDSS流媒体RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器解决方案创建视频点播、短视频、视频资源库等视频播放系统
需求背景 最近有很多用户咨询关于视频点播问题,主要需求集中在如何搭建属于自己的视频点播平台: 实现的功能可以大体归类为:对应自身拥有的视频文件,需要发布到一个网站,其他用户都可以实现点播观看. 针对于 ...
- hls流媒体视频防盗实现
HLS流媒体视频防盗实现 一.Windows安装FFmpeg 1.1 安装版本 1.1.1 网址:https://ffmpeg.org/ 1.1.2 选择Windows版本:https://ffmpe ...
- 如何打造一款m3u8视频爬虫
0.前言 m3u8是一种很常见的网页视频播放器的视频源,比如说中国大学MOOC中课程就是使用了该种视频格式. 随便打开一门课程,就可以发现在网络请求中存在一个m3u8的文件,在preview中预览,它 ...
- m3u8视频格式分析
“ 学习m3u8格式.” 一段时间之前,乘着某美女CEO的东风,学习了一个新的数据格式,即m3u8格式. 经过一段时间的沉淀,美女CEO的热潮大概已经褪去,今天才对这个格式进行分析,嘻嘻. 先介绍下来 ...
- 大华海康NVR录像JAVA下载及WEB播放
近期在处理一个将NVR录像机上的录像下载到服务器并通过浏览器播放的需求. 梳理记录下过程,做个备忘,同时遇到的一些细节问题解决,也供需要的同学参考. 需求比较简单,就是把指定时间段的录像上传到服务器保 ...
随机推荐
- 初学Python——RabbitMQ的安装
记录踩坑之路,本篇文章主要摘抄自CSDN博客https://blog.csdn.net/weixin_39735923/article/details/79288578 Windows10环境下安装R ...
- 面试笔记--Fast-Fail(快速失败)机制
1.解决: fail-fast机制,是一种错误检测机制.它只能被用来检测错误,因为JDK并不保证fail-fast机制一定会发生.若在多线程环境下使用fail-fast机制的集合,建议使用“java. ...
- FIFO队列算法的C程序实现
头文件:Queue.h #ifndef _Queue_H #define _Queue_H typedef struct QueueDef_ //队列对象定义 { u16 front; //队列头部 ...
- 蓝牙BLE设备断线回连分析
在 文章中分析了Hogp的连接的流程 ,这里分析一下回连的流程. 在使用ble设备的过程中,我们发现当设备和主机配对之后,如果没有解除配对,那么即便设备和主机断开,那么也是可以重新连接而不需要重新走配 ...
- 你分得清楚Maven的聚合和继承吗?
用了 Maven 好几年了,许多人还是只懂得简单的依赖坐标.对于 Maven 的聚合和继承还是一知半解,甚至很多人以为是同一个东西.但其实聚合是用于快速构建项目,是表示项目与子项目之间的关系.而继承则 ...
- 朱晔和你聊Spring系列S1E7:简单好用的Spring Boot Actuator
阅读PDF版本 本文会来看一下Spring Boot Actuator提供给我们的监控端点Endpoint.健康检查Health和打点指标Metrics等所谓的Production-ready(生产环 ...
- java.util.Stack类中 empty() 和 isEmpty() 方法的作用
最近在学习算法和数据结构,用到Java里的Stack类,但程序运行结果一直和我预料的不一样,网上也没查清楚,最后查了API,才搞明白. java.util.Stack继承类 java.util.Vec ...
- 简单的词法设计——DFA模拟程序
实验一.简单的词法设计--DFA模拟程序 一.实验目的 通过实验教学,加深学生对所学的关于编译的理论知识的理解,增强学生对所学知识的综合应用能力,并通过实践达到对所学的知识进行验证.通过对 DFA 模 ...
- 使用 Emmet 生成 HTML 的语法详解
生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 “!” 就可以生成一个 HTML5 的标准文档 ...
- Random()种子数
Random rand =new Random(25); int i; i=rand.nextInt(100); 初始化时25并没有起直接作用,rand.nextInt(100);中的100是随机数的 ...