Android解析短视频无水印链接(精)抖音/快手/微视

近日公司需求,其中一个功能是从短视频APP中复制链接,粘贴到APP里解析无水印地址,下载

查阅很多资料,并未看到相关的博客,终于科学上网看到了一个神奇的网站~

BERRYAPI

Android解析短视频无水印链接(精)抖音/快手/微视_第1张图片

短视频支持以上平台,还有一些其他非短视频主流平台,大家可以自己去网站上了解~

接下来博主用三个平台做例子:抖音,快手,微视

1.读取剪切板内容 

 public void getJianQieBan() {
        ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData data = cm.getPrimaryClip();
        if (data != null) {
            ClipData.Item item = data.getItemAt(0);
            JianQieContent = item.getText().toString();
            String simple = "http";
            Boolean simple_result = JianQieContent.contains(simple);
            if (simple_result) {
                //判断视频类型
                judgeVideoType(JianQieContent);
                //解析视频
                parsingVideo(VideoType);
            } else {
                Toasty.info(getApplicationContext(), "请先去复制链接").show();
            }
        }
    }

读取剪切板内容

2.判断视频类型

 private void judgeVideoType(String url) {
        if (url.indexOf("weishi") != -1) {
            //Type值=微视
            VideoType = 3;
            type = 2;
            VideoUrl = url.substring(url.indexOf("https"));
        } else if (url.indexOf("douyin") != -1) {
            //Type值=抖音
            VideoType = 4;
            String douyin_else;
            type = 0;
            douyin_else = url.substring(url.indexOf("http"));
            if (douyin_else.indexOf(" 复制") != -1) {
                VideoUrl = douyin_else.substring(0, douyin_else.indexOf(" 复制"));
            } else {
                VideoUrl = douyin_else;
            }
        } else {
            //Type值=快手
            VideoType = 2;
            type = 1;
            VideoUrl = url.substring(url.indexOf("http"));
        }
    }

去掉复制来的链接冗余文字标题

3.解析提取出的视频链接

  private void parsingVideo(int Type) {
        new Thread(() -> {
            try {
                JieXiConstant.getVideoJiexiUrl(getApplicationContext(), VideoUrl, Type);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

JieXiConstant中的getVideoJiexiUrl方法

public static void getVideoJiexiUrl(Context context, String Url, int Type) throws Exception {
        if (Type == weishi) {
            URL_HEAD = "https://api.lylares.com/video/weishi/?";
        } else if (Type == kuaishou) {
            URL_HEAD = "https://api.lylares.com/video/kuaishou/?";
        } else if (Type == douyin) {
            URL_HEAD = "https://api.lylares.com/video/douying/?";
        }
        KUAISHOU_URL = URL_HEAD + "AppKey=" + APP_KEY + "&url=" + Url;
        try {
            HttpGet request = new HttpGet(KUAISHOU_URL);
            HttpClient httpClient = getNewHttpClient();
            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                Gson gson = new Gson();
                if (Type == weishi) {
                    weiShiDetail = gson.fromJson(result, WeiShiDetail.class);
                    EventBus.getDefault().postSticky(new VideoUrlMessageEvent(weiShiDetail, 2));
                } else if (Type == kuaishou) {
                    kuaiShouDetail = gson.fromJson(result, KuaiShouDetail.class);
                    EventBus.getDefault().postSticky(new VideoUrlMessageEvent(kuaiShouDetail, 1));
                } else if (Type == douyin) {
                    douYinDetail = gson.fromJson(result, DouYinDetail.class);
                    EventBus.getDefault().postSticky(new VideoUrlMessageEvent(douYinDetail, 0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 public static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
            SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

解析成功,获取到链接下载保存到本地即可

存在问题:Android4.4 HUAWEI P8 测试无法绕开SSL安全证书,解析失败 ,6.0 测试成功

有解决办法的留言,感谢 ~ 

记录于:2018/11/15 14:36

你可能感兴趣的:(Android)