Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1317024
  • 博文数量: 932
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10208
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-08 12:28
文章分类

全部博文(932)

文章存档

2021年(1)

2020年(134)

2019年(792)

2018年(5)

我的朋友

分类: 架构设计与优化

2019-04-27 08:34:59

比如这个在线视频:

我们可以正常播放,但是找不到下载按钮。

打开Chrome开发者工具,在Network标签页里能看到很多网络传输请求:

随便看一个请求的响应,发现类型为video,大小为500多k。因此,这个在线视频被拆分成了若干500多k的小片段,然后通过浏览器下载到本地进行播放。

这个片段的url:

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上:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

阅读(789) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~