Java下载映客主播视频回放到电脑硬盘

2017-02-01· 3771 次浏览
使用Java下载映客回放到电脑硬盘 使用方法:在映客app播放回放视频时,分享到QQ,就可以得到url,其中的liveid属性就是视频ID。 源代码由 [**海思教育**](http://edu.itshidu.com/) 提供,是一个实用性很强的教学案例。 代码: ```java package com.itshidu.demo.inke;   import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List;   /**  * 下载映客回放视频  * 使用方法:在映客app播放回放视频时,分享到QQ,就可以得到如下所示的url,其中的liveid属性就是视频ID  * 下载之后使用暴风影音等播放器直接打开即可播放  * @author Master.Xia Create:2017年2月1日20:40:36  */ public class DownloadVideo {       public static void main(String[] args) throws IOException {         //视频链接         String url = "http://mlive11.inke.cn/share/live.html?uid=10451773&liveid=1485698466633577&ctime=1485698466&share_uid=69909828&share_from=qq&share_time=1485942139";         //保存目录         File savePath = new File("E:/download");                   //从URL中提取liveid         String liveid = url.substring(url.indexOf("liveid=")+7,url.indexOf("&ctime="));         System.out.println("准备下载:liveid="+liveid);           //如果存储目录不存在则创建目录         if(!savePath.exists()){             savePath.mkdirs();         }                   //创建一个文件输出流         OutputStream out = new FileOutputStream(new File(savePath,liveid+".ts"));         //最关键就是这里了,只要提供liveid和输出流,就可以下载,上面的都是准备工作         execute(liveid,out);         //结束之后记得关闭输出流         out.close();                   System.out.println("恭喜,下载完成!");     }           /**      * 下载映客视频(录像的id和保存文件的输出流,此方法不会关闭out)      * @param liveid      * @param out      * @throws IOException      */     public static void execute(String liveid,OutputStream out) throws IOException{         //获取文件列表         List<String> tsnames = getFileNameList(liveid);         //遍历下载         for(int i=0;i<tsnames.size();i++){             System.out.println(String.format("%s ( %d/%d )", tsnames.get(i),i+1,tsnames.size()));             String httpUrl = "http://record2.inke.cn/record_"+liveid+"/"+tsnames.get(i);             InputStream in = httpInputStream(httpUrl);             byte[] buffer = new byte[1024*8];             for(int len;(len=in.read(buffer))!=-1;){                 out.write(buffer, 0, len);             }             in.close();         }                         }                 /**      * 根据liveid获取文件名称列表      * @param liveid      * @return      */     public static List<String> getFileNameList(String liveid){         List<String> tsnames = new ArrayList<String>();         try {             String mu = "http://record2.inke.cn/record_"+liveid+"/"+liveid+".m3u8";             InputStream in = httpInputStream(mu);             BufferedReader bfr = new BufferedReader(new InputStreamReader(in));             for(String line=null;(line=bfr.readLine())!=null;){                 if(line.endsWith(".ts")){                     tsnames.add(line);                 }             }             bfr.close();             return tsnames;         } catch (Exception e) {             System.out.println(e);         }         return null;     }           /**      * 从http路径获取输入流      * @param httpUrl      * @return      */     public static InputStream httpInputStream(String httpUrl){         try {               URL url = new URL(httpUrl);             URLConnection conn = url.openConnection();               InputStream in = conn.getInputStream();             return in;         } catch (Exception e1) {               e1.printStackTrace();           }         return null;     } } ``` Good Luck!