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

1  
!752
2  
3  
p {
4  
  S url = getIt("https://www.youtube.com/watch?v=4GuqB1BQVr4");
5  
  print("URL: " + url);
6  
  print("Size: " + getURLSizeByHEAD(url));
7  
}
8  
9  
sclass Meta {
10  
    public String num;
11  
    public String type;
12  
    public String ext;
13  
14  
    Meta(String num, String ext, String type) {
15  
        this.num = num;
16  
        this.ext = ext;
17  
        this.type = type;
18  
    }
19  
}
20  
21  
sclass Video {
22  
    public String ext = "";
23  
    public String type = "";
24  
    public String url = "";
25  
26  
    Video(String ext, String type, String url) {
27  
        this.ext = ext;
28  
        this.type = type;
29  
        this.url = url;
30  
    }
31  
}
32  
33  
static L<Video> getStreamingUrisFromYouTubePage(String ytUrl)
34  
        throws IOException {
35  
    if (ytUrl == null) {
36  
        return null;
37  
    }
38  
39  
    // Remove any query params in query string after the watch?v=<vid> in
40  
    // e.g.
41  
    // http://www.youtube.com/watch?v=0RUPACpf8Vs&feature=youtube_gdata_player
42  
    int andIdx = ytUrl.indexOf('&');
43  
    if (andIdx >= 0) {
44  
        ytUrl = ytUrl.substring(0, andIdx);
45  
    }
46  
47  
    // Get the HTML response
48  
    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1)";
49  
    URL tURL = new URL(ytUrl);
50  
    URLConnection conn = tURL.openConnection();
51  
    conn.setRequestProperty("User-Agent", userAgent);
52  
    loadPage_anonymous = true;
53  
    S html = loadPage(conn, tURL);
54  
    html = html.replace("\\u0026", "&");
55  
56  
    print("[[\n" + html + "\n]]\n");
57  
    
58  
    // Parse the HTML response and extract the streaming URIs
59  
    if (html.contains("verify-age-thumb")) {
60  
        print("YouTube is asking for age verification. We can't handle that sorry.");
61  
        return null;
62  
    }
63  
64  
    if (html.contains("das_captcha")) {
65  
        print("Captcha found, please try with different IP address.");
66  
        return null;
67  
    }
68  
69  
    Pattern p = Pattern.compile("stream_map\": ?\"(.*?)?\"");
70  
    // Pattern p = Pattern.compile("/stream_map=(.[^&]*?)\"/");
71  
    Matcher m = p.matcher(html);
72  
    List<String> matches = new ArrayList<String>();
73  
    while (m.find()) {
74  
        matches.add(m.group());
75  
    }
76  
77  
    if (matches.size() != 1) {
78  
        print("Found zero or too many stream maps (" + l(matches) + ")");
79  
        return null;
80  
    }
81  
82  
    String urls[] = matches.get(0).split(",");
83  
    print("urls found: " + l(urls));
84  
    psl(urls);
85  
    HashMap<String, String> foundArray = new HashMap<String, String>();
86  
    for (String ppUrl : urls) {
87  
        Map<S, S> map = decodeHQuery(ppUrl);
88  
        print("Map: " + structure(map));
89  
        
90  
        S itag = map.get("itag");
91  
        S um = map.get("url");
92  
93  
        if (itag != null && um != null) {
94  
            foundArray.put(itag, um);
95  
        }
96  
    }
97  
98  
    if (foundArray.size() == 0) {
99  
        print("Couldn't find any URLs and corresponding signatures");
100  
        return null;
101  
    }
102  
103  
    HashMap<String, Meta> typeMap = new HashMap<String, Meta>();
104  
    typeMap.put("13", new Meta("13", "3GP", "Low Quality - 176x144"));
105  
    typeMap.put("17", new Meta("17", "3GP", "Medium Quality - 176x144"));
106  
    typeMap.put("36", new Meta("36", "3GP", "High Quality - 320x240"));
107  
    typeMap.put("5", new Meta("5", "FLV", "Low Quality - 400x226"));
108  
    typeMap.put("6", new Meta("6", "FLV", "Medium Quality - 640x360"));
109  
    typeMap.put("34", new Meta("34", "FLV", "Medium Quality - 640x360"));
110  
    typeMap.put("35", new Meta("35", "FLV", "High Quality - 854x480"));
111  
    typeMap.put("43", new Meta("43", "WEBM", "Low Quality - 640x360"));
112  
    typeMap.put("44", new Meta("44", "WEBM", "Medium Quality - 854x480"));
113  
    typeMap.put("45", new Meta("45", "WEBM", "High Quality - 1280x720"));
114  
    typeMap.put("18", new Meta("18", "MP4", "Medium Quality - 480x360"));
115  
    typeMap.put("22", new Meta("22", "MP4", "High Quality - 1280x720"));
116  
    typeMap.put("37", new Meta("37", "MP4", "High Quality - 1920x1080"));
117  
    typeMap.put("33", new Meta("38", "MP4", "High Quality - 4096x230"));
118  
119  
    new L<Video> videos;
120  
121  
    for (String format : typeMap.keySet()) {
122  
        Meta meta = typeMap.get(format);
123  
124  
        if (foundArray.containsKey(format)) {
125  
            Video newVideo = new Video(meta.ext, meta.type,
126  
                    foundArray.get(format));
127  
            videos.add(newVideo);
128  
            print("YouTube Video streaming details: ext:" + newVideo.ext
129  
                    + ", type:" + newVideo.type + ", url:" + newVideo.url);
130  
        }
131  
    }
132  
133  
    return videos;
134  
}
135  
136  
static S getIt(S url) {
137  
        try {
138  
            L<Video> videos = getStreamingUrisFromYouTubePage(url);
139  
            if (videos != null && !videos.isEmpty()) {
140  
                String retVidUrl = null;
141  
                for (Video video : videos) {
142  
                    if (video.ext.toLowerCase().contains("mp4")
143  
                            && video.type.toLowerCase().contains("medium")) {
144  
                        retVidUrl = video.url;
145  
                        break;
146  
                    }
147  
                }
148  
                if (retVidUrl == null) {
149  
                    for (Video video : videos) {
150  
                        if (video.ext.toLowerCase().contains("3gp")
151  
                                && video.type.toLowerCase().contains(
152  
                                        "medium")) {
153  
                            retVidUrl = video.url;
154  
                            break;
155  
156  
                        }
157  
                    }
158  
                }
159  
                if (retVidUrl == null) {
160  
161  
                    for (Video video : videos) {
162  
                        if (video.ext.toLowerCase().contains("mp4")
163  
                                && video.type.toLowerCase().contains("low")) {
164  
                            retVidUrl = video.url;
165  
                            break;
166  
167  
                        }
168  
                    }
169  
                }
170  
                if (retVidUrl == null) {
171  
                    for (Video video : videos) {
172  
                        if (video.ext.toLowerCase().contains("3gp")
173  
                                && video.type.toLowerCase().contains("low")) {
174  
                            retVidUrl = video.url;
175  
                            break;
176  
                        }
177  
                    }
178  
                }
179  
180  
                return retVidUrl;
181  
            }
182  
        } catch (Exception e) {
183  
            fail("Couldn't get YouTube streaming URL", e);
184  
        }
185  
        fail("Couldn't get stream URI for " + url);
186  
    }

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: 950 / 1001
Version history: 1 change(s)
Referenced in: [show references]