下载索引文件

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

参考:M3U8在线视频文件下载合成MP4视频

Java 下载 HLS (m3u8) 视频的更多相关文章

  1. 如何下载HLS视频到本地(m3u8)

      如何下载HLS视频到本地(m3u8)? CreateTime--2018年3月21日16:07:00 Author:Marydon 一.需求 很多连载的动漫需要VIP会员才能观看,而且有的由于版权 ...

  2. 打造m3u8视频(流视频)下载解密合并器(kotlin)

    本文是对我原创工具m3u8视频下载合并器关键代码解析及软件实现的思路的讲解,想要工具的请跳转链接 1.思路说明 思路挺简单,具体步骤如下: 下载m3u8文件 解析m3u8文件获得ts文件列表 根据文件 ...

  3. 下载m3u8视频

    分两种情况 同时支持m3u8和mp4文件 某些视频同时支持m3u8和mp4视频文件,将m3u8改成mp4后直接: wget -c http://www.xxx.com/xxxx.mp4 只有m3u8视 ...

  4. 下载HLS视频到本地

    现在绝大多数网站播放视频都采用HLS技术,像腾讯优酷爱奇艺等等.本篇博文将介绍如何下载这样的视频到本地. 前言 因疫情影响,上课部分课程采用腾讯课堂上课,腾讯课堂有直播回放功能,但这个功能腾讯显然没有 ...

  5. 基于EasyDSS流媒体RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器解决方案创建视频点播、短视频、视频资源库等视频播放系统

    需求背景 最近有很多用户咨询关于视频点播问题,主要需求集中在如何搭建属于自己的视频点播平台: 实现的功能可以大体归类为:对应自身拥有的视频文件,需要发布到一个网站,其他用户都可以实现点播观看. 针对于 ...

  6. hls流媒体视频防盗实现

    HLS流媒体视频防盗实现 一.Windows安装FFmpeg 1.1 安装版本 1.1.1 网址:https://ffmpeg.org/ 1.1.2 选择Windows版本:https://ffmpe ...

  7. 如何打造一款m3u8视频爬虫

    0.前言 m3u8是一种很常见的网页视频播放器的视频源,比如说中国大学MOOC中课程就是使用了该种视频格式. 随便打开一门课程,就可以发现在网络请求中存在一个m3u8的文件,在preview中预览,它 ...

  8. m3u8视频格式分析

    “ 学习m3u8格式.” 一段时间之前,乘着某美女CEO的东风,学习了一个新的数据格式,即m3u8格式. 经过一段时间的沉淀,美女CEO的热潮大概已经褪去,今天才对这个格式进行分析,嘻嘻. 先介绍下来 ...

  9. 大华海康NVR录像JAVA下载及WEB播放

    近期在处理一个将NVR录像机上的录像下载到服务器并通过浏览器播放的需求. 梳理记录下过程,做个备忘,同时遇到的一些细节问题解决,也供需要的同学参考. 需求比较简单,就是把指定时间段的录像上传到服务器保 ...

随机推荐

  1. Springboot知识点

    1. Spring boot简介 主要用来简化spring开发,快速地创建独立的spring项目,并且与云计算天然集成. 2. @Controller  标记一个类是Controller . 3. @ ...

  2. 使用后台线程BackgroundWorker处理任务的总结

    在一些耗时的操作过程中,在长时间运行时可能会导致用户界面 (UI) 处于停止响应状态,用户在这操作期间无法进行其他的操作,为了不使UI层处于停止响应状态,我们倾向推荐用户使用BackgroundWor ...

  3. 开源组件ELK日志系统配置与管理

    ELK日志系统介绍 开源实时日志分析ELK平台能够完美的解决我们上述的问题,ELK由ElasticSearch.Logstash和Kiabana三个开源工具组成.官方网站:https://www.el ...

  4. 性能调优9:根据WaitType诊断性能

    SQL Server数据库接收到查询请求,从生成计划到执行计划的过程,等待次数和等待时间在一定程度上揭示了系统性能的压力,如果资源严重不足,就会成为性能的瓶颈.因此,对等待的监控非常有助于对系统性能进 ...

  5. Redux与它的中间件:redux-thunk,redux-actions,redux-promise,redux-saga

    序言 这里要讲的就是一个Redux在React中的应用问题,讲一讲Redux,react-redux,redux-thunk,redux-actions,redux-promise,redux-sag ...

  6. js canvas图片压缩

    function preview_picture(pic){ var r=new FileReader(); r.readAsDataURL(pic); r.onload=function(e){ d ...

  7. Sparse Principal Component Analysis via Rotation and Truncation

    目录 对以往一些SPCA算法复杂度的总结 Notation 论文概述 原始问题 问题的变种 算法 固定\(X\),计算\(R\) 固定\(R\),求解\(X\) (\(Z =VR^{\mathrm{T ...

  8. 软件工程练习:模块化,单元测试,回归测试,TDD

    这是<构建之法>实战教学的一部分.适合作为同学们的第二个程序作业. 第一个程序作业: 请看 “概论” 一章的练习,或者老师的题目,例如这个. 作业要求: 软件工程的作业越来越有意思了, 我 ...

  9. 日志之环绕通知(AOP)

    环绕通知:一个完整的try...catch...finally结构 编写环绕通知方法,环绕通知需要携带ProceedingJoinPoint 这个类型的参数,ProceedingJoinPoint类型 ...

  10. iOS UICollectionView 在滚动时停在某个item位置上

    方法一:实现UIScrollView的代理,然后实现下面这个方法 #pragma mark - UIScrollViewDelegate//预计出大概位置,经过精确定位获得准备位置- (void)sc ...