Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

186
LINES

< > BotCompany Repo | #1003713 // Try getting YouTube MP4 URL [possibly not working]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2035L/15K/47K).

!752

p {
  S url = getIt("https://www.youtube.com/watch?v=4GuqB1BQVr4");
  print("URL: " + url);
  print("Size: " + getURLSizeByHEAD(url));
}

sclass Meta {
    public String num;
    public String type;
    public String ext;

    Meta(String num, String ext, String type) {
        this.num = num;
        this.ext = ext;
        this.type = type;
    }
}

sclass Video {
    public String ext = "";
    public String type = "";
    public String url = "";

    Video(String ext, String type, String url) {
        this.ext = ext;
        this.type = type;
        this.url = url;
    }
}

static L<Video> getStreamingUrisFromYouTubePage(String ytUrl)
        throws IOException {
    if (ytUrl == null) {
        return null;
    }

    // Remove any query params in query string after the watch?v=<vid> in
    // e.g.
    // http://www.youtube.com/watch?v=0RUPACpf8Vs&feature=youtube_gdata_player
    int andIdx = ytUrl.indexOf('&');
    if (andIdx >= 0) {
        ytUrl = ytUrl.substring(0, andIdx);
    }

    // Get the HTML response
    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1)";
    URL tURL = new URL(ytUrl);
    URLConnection conn = tURL.openConnection();
    conn.setRequestProperty("User-Agent", userAgent);
    loadPage_anonymous = true;
    S html = loadPage(conn, tURL);
    html = html.replace("\\u0026", "&");

    print("[[\n" + html + "\n]]\n");
    
    // Parse the HTML response and extract the streaming URIs
    if (html.contains("verify-age-thumb")) {
        print("YouTube is asking for age verification. We can't handle that sorry.");
        return null;
    }

    if (html.contains("das_captcha")) {
        print("Captcha found, please try with different IP address.");
        return null;
    }

    Pattern p = Pattern.compile("stream_map\": ?\"(.*?)?\"");
    // Pattern p = Pattern.compile("/stream_map=(.[^&]*?)\"/");
    Matcher m = p.matcher(html);
    List<String> matches = new ArrayList<String>();
    while (m.find()) {
        matches.add(m.group());
    }

    if (matches.size() != 1) {
        print("Found zero or too many stream maps (" + l(matches) + ")");
        return null;
    }

    String urls[] = matches.get(0).split(",");
    print("urls found: " + l(urls));
    psl(urls);
    HashMap<String, String> foundArray = new HashMap<String, String>();
    for (String ppUrl : urls) {
        Map<S, S> map = decodeHQuery(ppUrl);
        print("Map: " + structure(map));
        
        S itag = map.get("itag");
        S um = map.get("url");

        if (itag != null && um != null) {
            foundArray.put(itag, um);
        }
    }

    if (foundArray.size() == 0) {
        print("Couldn't find any URLs and corresponding signatures");
        return null;
    }

    HashMap<String, Meta> typeMap = new HashMap<String, Meta>();
    typeMap.put("13", new Meta("13", "3GP", "Low Quality - 176x144"));
    typeMap.put("17", new Meta("17", "3GP", "Medium Quality - 176x144"));
    typeMap.put("36", new Meta("36", "3GP", "High Quality - 320x240"));
    typeMap.put("5", new Meta("5", "FLV", "Low Quality - 400x226"));
    typeMap.put("6", new Meta("6", "FLV", "Medium Quality - 640x360"));
    typeMap.put("34", new Meta("34", "FLV", "Medium Quality - 640x360"));
    typeMap.put("35", new Meta("35", "FLV", "High Quality - 854x480"));
    typeMap.put("43", new Meta("43", "WEBM", "Low Quality - 640x360"));
    typeMap.put("44", new Meta("44", "WEBM", "Medium Quality - 854x480"));
    typeMap.put("45", new Meta("45", "WEBM", "High Quality - 1280x720"));
    typeMap.put("18", new Meta("18", "MP4", "Medium Quality - 480x360"));
    typeMap.put("22", new Meta("22", "MP4", "High Quality - 1280x720"));
    typeMap.put("37", new Meta("37", "MP4", "High Quality - 1920x1080"));
    typeMap.put("33", new Meta("38", "MP4", "High Quality - 4096x230"));

    new L<Video> videos;

    for (String format : typeMap.keySet()) {
        Meta meta = typeMap.get(format);

        if (foundArray.containsKey(format)) {
            Video newVideo = new Video(meta.ext, meta.type,
                    foundArray.get(format));
            videos.add(newVideo);
            print("YouTube Video streaming details: ext:" + newVideo.ext
                    + ", type:" + newVideo.type + ", url:" + newVideo.url);
        }
    }

    return videos;
}

static S getIt(S url) {
        try {
            L<Video> videos = getStreamingUrisFromYouTubePage(url);
            if (videos != null && !videos.isEmpty()) {
                String retVidUrl = null;
                for (Video video : videos) {
                    if (video.ext.toLowerCase().contains("mp4")
                            && video.type.toLowerCase().contains("medium")) {
                        retVidUrl = video.url;
                        break;
                    }
                }
                if (retVidUrl == null) {
                    for (Video video : videos) {
                        if (video.ext.toLowerCase().contains("3gp")
                                && video.type.toLowerCase().contains(
                                        "medium")) {
                            retVidUrl = video.url;
                            break;

                        }
                    }
                }
                if (retVidUrl == null) {

                    for (Video video : videos) {
                        if (video.ext.toLowerCase().contains("mp4")
                                && video.type.toLowerCase().contains("low")) {
                            retVidUrl = video.url;
                            break;

                        }
                    }
                }
                if (retVidUrl == null) {
                    for (Video video : videos) {
                        if (video.ext.toLowerCase().contains("3gp")
                                && video.type.toLowerCase().contains("low")) {
                            retVidUrl = video.url;
                            break;
                        }
                    }
                }

                return retVidUrl;
            }
        } catch (Exception e) {
            fail("Couldn't get YouTube streaming URL", e);
        }
        fail("Couldn't get stream URI for " + url);
    }

Author comment

From: http://stackoverflow.com/questions/15240011/get-the-download-url-for-youtube-video-android-java

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1265 stefan Gets an URL, but that can't be downloaded 2016-07-26 14:42:50

add comment

Snippet ID: #1003713
Snippet name: Try getting YouTube MP4 URL [possibly not working]
Eternal ID of this version: #1003713/2
Text MD5: 91df8fc47141179f7fcde0db8c1f5ad7
Transpilation MD5: 01d3d017cee80f14b4479ce2a8db8adc
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-03 01:22:10
Source code size: 6442 bytes / 186 lines
Pitched / IR pitched: No / No
Views / Downloads: 855 / 887
Version history: 1 change(s)
Referenced in: [show references]