How to get direct link of remote video from embedded url within a url in Android using JSoup?

江枫思渺然 提交于 2019-12-03 12:43:33

This is how you can GET the file:

(Notice: the Extraction part only works with current html of the site and if that changes, it may not work correctly!)

String url = "https://www.wunderground.com/webcams/cadot1/902/video.html";
int timeout = 100 * 1000;

// Extract video URL
Document doc = Jsoup.connect(url).timeout(timeout).get();
Element script = doc.getElementById("inner-content")
        .getElementsByTag("script").last();
String content = script.data();
int indexOfUrl = content.indexOf("url");
int indexOfComma = content.indexOf(',', indexOfUrl);
String videoUrl = "https:" + content.substring(indexOfUrl + 6, indexOfComma - 1);
System.out.println(videoUrl);

[Output: https://icons.wunderground.com/webcamcurrent/c/a/cadot1/902/current.mp4?e=1481246112]

Now you can get the file by specifying .ignoreContentType(true) in order to avoid org.jsoup.UnsupportedMimeTypeException and .maxBodySize(0) to remove the limit on file size.

// Get video file
byte[] video = Jsoup.connect(videoUrl)
        .ignoreContentType(true).timeout(timeout).maxBodySize(0)
        .execute().bodyAsBytes();

I don't know if you can play it in Android or not but I think you can save it using org.apache.commons.io.FileUtils (I tested it in Java SE but not Android development environment.)

// Save video file
org.apache.commons.io.FileUtils.writeByteArrayToFile(new File("test.mp4"), video);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!