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录像机上的录像下载到服务器并通过浏览器播放的需求. 梳理记录下过程,做个备忘,同时遇到的一些细节问题解决,也供需要的同学参考. 需求比较简单,就是把指定时间段的录像上传到服务器保 ...
随机推荐
- 【转】idea 2018注册码(激活码)永久性的
百度的,上一个没用多久就挂了,这次用http://idea.toocruel.net 激活方式:License Server1.将地址 http://active.chinapyg.com/ 或者 h ...
- redis学习(四)——Hash数据类型
一.概述 我们可以将Redis中的Hash类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Age等. ...
- MySQL定时备份数据库(全库备份)
一.MySQL数据备份 1.1. mysqldump命令备份数据 在MySQL中提供了命令行导出数据库数据以及文件的一种方便的工具mysqldump,我们可以通过命令行直接实现数据库内容的导出dump ...
- 【C#复习总结】探究各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字
前言 先普及一下线程安全和类型安全 线程安全: 如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的 ...
- Python-可变参数和关键字参数(*args **kw)
前言: Python的函数具有非常灵活的参数形态,既可以实现简单的调用,又可以传入非常复杂的参数. 可变参数和关键字参数的语法: *args是可变参数,args接收的是一个tuple: **kw是关键 ...
- H5 35-背景平铺属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- react 项目搭建
1.首先运行环境-node是必须的,需要下载安装node的运行环境: 2.安装好了node之后,自然的就有了npm: 3.npm install -g creact-react-app/全局安装cre ...
- hibernate在写cfg配置文件自动创建表时报错org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
在用hibernate框架时,写cfg文件,想自动生成表时,一般写<property name="hibernate.hbm2ddl.auto">create</ ...
- MYSQL: 1292 - Truncated incorrect DOUBLE value: '184B3C0A-C411-47F7-BE45-CE7C0818F420'
MySQL Bugs: #63112: Truncated incorrect DOUBLE valuehttps://bugs.mysql.com/bug.php?id=63112 Error Co ...
- C# foreach内部原理
我们知道使用foreach的一个要求是对象必须继承自IEnumerable接口 这样才可以进行迭代 那内部是怎么实现的呢 这个时候会将对应的foreach语句转换为一个while循环 并且通过Move ...