自己开发的在线视频下载工具,基于Java多线程
比如这个在线视频:
我们可以正常播放,但是找不到下载按钮。
打开Chrome开发者工具,在Network标签页里能看到很多网络传输请求:
随便看一个请求的响应,发现类型为video,大小为500多k。因此,这个在线视频被拆分成了若干500多k的小片段,然后通过浏览器下载到本地进行播放。
这个片段的url:
http://d2vvqvds83fsd.cloudfront.net/vin02/vsmedia/definst/smil:event/18/36/06/3/rt/1/resources/180919_PID_Intelligent_Enterprise_Gruenewald_720p-5F92.smil/media_b433000_10.ts
那么这个片段一共有多少个片段呢?在所有片段开始下载之前,有这样一个请求:chunklist即是视频片段的清单。
通过这个清单我们知道这个视频一共分为55个片段,序号从0开始。
了解了原理,我们就可以开始编程了。
1. 首先实现视频片段的下载逻辑,新建一个类,实现Runnable接口。
2. 使用JDK自带的多线程库 ExecutorService多线程下载这些片段。ExecutorService实际是一个线程池。第15行可以指定线程池里工作线程(Working thread)的个数。
private void download(){
URL task = null;
String path = DownloadLauncher.LOCALPATH + this.mIndex +
DownloadLauncher.POSTFIX;
String url = this.mTask;
try {
task = new URL(url);
DataInputStream dataInputStream = new DataInputStream(task.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
System.out.println("File: " + this.mIndex + " downloaded ok");
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
下载完成后,能在Eclipse的console控制台看到这些输出:
下载成功的视频片段:
3. Merger负责把这些片段合并成一个大文件。
private static void run() throws IOException{
FileInputStream in = null;
String destFile = DownloadLauncher.LOCALPATH +
DownloadLauncher.MERGED;
FileOutputStream out = new FileOutputStream(destFile,true);
for( int i = 0; i <= DownloadLauncher.LAST; i++){
byte[] buf = new byte[1024];
int len = 0;
String sourceFile = DownloadLauncher.LOCALPATH + i +
DownloadLauncher.POSTFIX;
in = new FileInputStream(sourceFile);
while( (len = in.read(buf)) != -1 ){
out.write(buf,0,len);
}
}
out.close();
}
public static void main(String[] args) {
try {
run();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Merged ok!");
}
完整的代码在我的github上:
https://github.com/i042416/JavaTwoPlusTwoEquals5/tree/master/src/flick
要获取更多Jerry的原创文章,请关注公众号"汪子熙":

自己开发的在线视频下载工具,基于Java多线程的更多相关文章
- 7个好用的在线YouTube视频下载工具
title: 7个好用的在线YouTube视频下载工具 toc: false date: 2018-10-10 15:11:00 categories: methods tags: youtube C ...
- You-Get,多网站视频下载工具,非常方便
You-Get是一个非常优秀的网站视频下载工具.使用You-Get可以很轻松的下载到网络上的视频.图片及音乐. 按Win+R键打开运行,输入cmd,再输入命令 pip install you-get, ...
- 在线视频下载利器——youtube-dl
youtube-dl是谷歌出品的在线视频下载利器,可以用来下载youtube视频(前提是你得能上youtube). 使用方法很简单,只需要在cmd下执行youtube-de.exe +视频页面网址,程 ...
- YouTube 视频下载工具
YouTube 视频下载工具 我不生产视频,只是优秀视频的搬运工! YouTube-dl https://github.com/search?q=youtube-dl https://github.c ...
- 命令行视频下载工具 you-get 和 youtube-dl
you-get 和 youtube-dl 都是基于 Python 的命令行媒体文件下载工具,完全开源免费跨平台.用户只需使用简单命令并提供在线视频的网页地址即可让程序自动进行嗅探.下载.合并.命名和清 ...
- Cesium中文网——如何开发一款地图下载工具[一]
Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ Cesium中文网的朋友们的其中一个主题是:自己独立开发一款地图 ...
- python实现的视频下载工具you-get,支持多个国内外主流视频平台
RT,you-get 是一个视频离线下载工具, https://github.com/soimort/you-get 另一个同类工具 youtube-dl 也是python 实现,虽然名为 youtu ...
- You-Get 视频下载工具 Python命令行下载工具
You-Get 是一个命令行工具, 用来下载各大视频网站的视频, 是我目前知道的命令行下载工具中最好的一个, 之前使用过 youtube-dl, 但是 youtube-dl 吧, 下载好的视频是分段的 ...
- Downie for Mac最强视频下载工具(支持B站优酷土豆腾讯等)
我搜集到的一款简单拖放链接到Downie,它就会下载该网站上的视频.理论可以下载各种视频网站上的视频! 应用介绍 Downie 是一款Mac平台上的优秀视频下载软件,使用非常简单,只需将下载链接放置D ...
随机推荐
- c++ primer 5th学习时间轴[ 100% ]
学习参考: 1.Mooophy/Cpp-Primer. GitHub上star最多的一个答案,英文版,但是编程用到的单词也不多,查查就懂了.但是到第IV部分,很多题目的没有答案,或者不完整. 2.hu ...
- Asset Catalog Help (八)---Customizing Image Sets for Devices
Customizing Image Sets for Devices Add images to a set that are customized for display on the device ...
- java集合框架之ArrayList与LinkedList的区别
参考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere ArrayList和LinkedL ...
- 3-3if-else条件结构 & 3-4 & 3-5
新建类: ConditionDemo2 package com.imooc.operator; public class ConditionDemo2 { public static void mai ...
- 解决At least one JAR was scanned for TLDs yet contained no TLDs. 问题
启动tomcat运行项目时,总是提示: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug loggin ...
- roguelike地牢生成算法
文章原地址 上一个地图生成算法,这一次是一个地牢的生成算法,是一个国外的人写的算法,用dart语言写,我把它改成了unity-c#. 原作者博客地址:Rooms and Mazes: A Proced ...
- 线程通讯--BlockingQueue
Producer线程 package com.thread.communication.blockingqueue; import java.util.concurrent.BlockingQueue ...
- vue中excel导入导出组件
vue中导入导出excel,并根据后台返回类型进行判断,导入到数据库中 功能:实现js导入导出excel,并且对导入的excel进行展示,当excel标题名称和数据库的名称标题匹配时,则对应列导入的数 ...
- 2016四川省赛A,C【写了1w个if的水题】
A题: #include <iostream> #include <stdio.h> #include <string.h> #include <algori ...
- java日期时间处理集合
本文主要介绍java中日期时间的处理,包括获取时间,时间相加减,格式化等操作. 持续更新中... 时间格式化 //时间格式化 SimpleDateFormat dateFormat = new Sim ...