下载索引文件

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. 获取数值型数组中大于60的元素个数,给数值型数组中不足60分的加20分。(数组,for循环,if条件判断语句)

    package com.Summer_0420.cn; /** * @author Summer * 获取数值型数组中大于60的元素个数 * 给数值型数组中不足60分的加20分 */ public c ...

  2. 进程同步控制(锁,信号量,事件), 进程通讯(队列和管道,生产者消费者模型) 数据共享(进程池和mutiprocess.Pool模块)

    参考博客 https://www.cnblogs.com/xiao987334176/p/9025072.html#autoid-1-1-0 进程同步(multiprocess.Lock.Semaph ...

  3. Linux内存管理之mmap详解

    转发之:http://blog.chinaunix.net/uid-26669729-id-3077015.html Linux内存管理之mmap详解 一. mmap系统调用 1. mmap系统调用  ...

  4. C# - Span 全面介绍:探索 .NET 新增的重要组成部分

    假设要公开特殊化排序例程,以就地对内存数据执行操作.可能要公开需要使用数组的方法,并提供对相应 T[] 执行操作的实现.如果方法的调用方有数组,且希望对整个数组进行排序,这样做就非常合适.但如果调用方 ...

  5. .Net外包篇:我是如何看待外包的

    前言 从工作至今,我在工作之余大大小小接了六次外包,不多不少,虽然没有为我带来很大收益,但也让我开拓了人脉,接触了不少知识,锻炼了全栈开发能力. 菜鸟时代 第一家客户(成功) 我接的第一个外包是为一家 ...

  6. Webpack+Typescript 简易配置

    教程:https://www.cnblogs.com/yasepix/p/9294499.html http://developer.egret.com/cn/github/egret-docs/ex ...

  7. 微信小程序支付异常:requestPayment:fail no permission

    今天在调试微信小程序支付时碰到了这个问题,支付参数都正常生成了,在调用 wx.requestPayment 进行支付时遇到了这个报错,查了一下发现是开发者工具中 AppID 写错了,用的 AppID ...

  8. js实现活动倒计时

    let startTime = 1527647143949; // 开始时间 var time = new Countdown('timer',startTime); function Countdo ...

  9. DOM操作 JS事件 节点增删改查

    --------------------------习惯是社会的巨大的飞轮和最可贵的维护者.——威·詹姆斯 day 49 [value属性操作] <!DOCTYPE html><ht ...

  10. python 获取lazada菲律宾站地址库

    import urllib3 import requests import ast import time # 因为lazada返回的数据是json类型,通过解码成字符串类型,为了方便数据操作,使用字 ...