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

后端 未结 1 1867
春和景丽
春和景丽 2021-02-15 13:34

I had asked the question previously about how to retrieve the embedded url for a video file and have successfully done so. Now I have a different issue. The json response for a

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-15 13:49

    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);
    

    0 讨论(0)
提交回复
热议问题