Victor Bu

户外, 旅行, 读书, 生活, 有趣

  • 首页
  • 旅行
  • 户外
  • 读书
  • 急救
  • 挨踢
所有文章 友链 关于我

Victor Bu

户外, 旅行, 读书, 生活, 有趣

  • 首页
  • 旅行
  • 户外
  • 读书
  • 急救
  • 挨踢

Java 下载 HLS (m3u8) 视频

2019-02-02

下载索引文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}

解析索引文件

1
2
3
4
5
6
7
8
9
10
11
12
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;
}

下载视频片段

同步下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
}
}

多线程下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()));
}
}
}
}

视频片段合成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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视频

赏

谢谢你请我吃糖果

支付宝
微信
  • hls
  • m3u8
  • IT

扫一扫,分享到微信

微信分享二维码
C# 获取文件详细备注信息 (如图片、视频实际创建时间)
SQL Server 2008 开启远程连接
© 2021 Victor Bu
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • 海岛
  • 香港
  • 攻略
  • 急救
  • 徒步
  • 泰国
  • 东南亚
  • 柬埔寨
  • 越南
  • 甘肃
  • 深圳
  • 香港文化博物馆
  • 树莓派
  • Raspbian
  • Python
  • Samba
  • CentOS 7
  • Linux
  • Windows
  • Travis CI
  • Hexo
  • GitHub
  • GIS
  • Leaflet
  • VLC
  • SQL Server
  • hls
  • m3u8
  • WindowsAPICodePack-Shell
  • DotNetty
  • Modbus
  • CRC
  • HJ212
  • ngrok
  • js
  • Java
  • Spring
  • Spring Boot
  • Mybatis
  • Spring MVC
  • Netty
  • RESTful API
  • Unit testing
  • PLC
  • JPA
  • MySQL
  • Redis
  • Shell32
  • IDE
  • IDEA
  • MyBatis
  • Microservices
  • Spring Cloud
  • Eureka
  • Spring Security
  • OAuth2
  • JWT
  • Ribbon
  • Feign
  • Hystrix
  • Hystrix Dashboard
  • Turbine
  • Zuul
  • Spring Cloud Config
  • Spring Cloud Sleuth
  • Zipkin
  • Spring Boot Admin
  • UUID
  • Hibernate
  • Swagger
  • snowflake
  • CORS
  • RabbitMQ
  • Elasticsearch
  • Sharding-JDBC
  • MongoDB
  • Tomcat
  • JDK
  • MQTT
  • WebSocket
  • Kafka
  • Alibaba
  • Nacos
  • Spring Cloud Gateway
  • Dubbo
  • Sentinel
  • SkyWalking
  • Seata
  • MyBatis-Plus
  • RestTemplate
  • Jasypt
  • XXL-JOB
  • JJWT
  • Hyper-V
  • Flyway
  • Elastic
  • Kibana
  • Beats
  • Logstash
  • canal
  • MinIO
  • OSS
  • Shell
  • inotify
  • Paho
  • Loki
  • Promtail
  • Grafana
  • Prometheus
  • node exporter
  • Docker
  • Kubernetes
  • Alibaba Cloud
  • Jenkins
  • Maven
  • Git
  • Node.js
  • EMQ X
  • Google Authenticator
  • 医疗

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 友情链接1
  • 友情链接2
  • 友情链接3
  • 友情链接4
  • 友情链接5
  • 友情链接6
很惭愧

只做了一点微小的工作
谢谢大家