How to get the mp4 url for Youtube videos using Youtube v3 API

天涯浪子 提交于 2019-11-27 11:29:38

i made a very simple API : https://gist.github.com/el3zahaby/9e60f1ae3168c38cc0f0054c15cd6a83

As Example: YouTube Video Link: https://www.youtube.com/watch?v=YGCLs9Bt_KY

now to get the Direct link

you need to call the api , like this :

https://script-url.com/?url=https://www.youtube.com/watch?v=YGCLs9Bt_KY

returns:

[
    {
        "url": "https:\/\/r10---sn-aigllnlr.googlevideo.com\/videoplayback?key=yt6&signature=81D86D3BC3D34D8A3B865464BE7BC54F34C1B0BC.7316033C2DD2F65E4D345CFA890257B63D7FE2A2&mt=1522999783&expire=1523021537&sparams=dur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&requiressl=yes&ei=gSLHWvuxDMOUVYaTqYgB&dur=244.204&pl=22&itag=22&ip=185.27.134.50&lmt=1522960451860848&id=o-AAoaDzyDCVXS404wfqZoCIdolGU-NM3-4yDxC0t868iL&ratebypass=yes&ms=au%2Conr&fvip=2&source=youtube&mv=m&ipbits=0&mm=31%2C26&mn=sn-aigllnlr%2Csn-5hne6nsy&mime=video%2Fmp4&c=WEB&initcwndbps=710000",
        "quality": "hd720",
        "itag": "22",
        "type": "video\/mp4; codecs=\"avc1.64001F, mp4a.40.2\""
    },
    {
        "url": "https:\/\/r10---sn-aigllnlr.googlevideo.com\/videoplayback?key=yt6&mt=1522999783&gir=yes&expire=1523021537&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&itag=43&ratebypass=yes&fvip=2&ipbits=0&mime=video%2Fwebm&initcwndbps=710000&signature=71DC48B9BF4B2E3ED46FE0A4CD36FE027DACF31E.4624B7B4BCB947336CEB029E9958B136F79759EB&clen=24203231&requiressl=yes&dur=0.000&pl=22&ip=185.27.134.50&lmt=1522961642553275&ei=gSLHWvuxDMOUVYaTqYgB&ms=au%2Conr&source=youtube&mv=m&id=o-AAoaDzyDCVXS404wfqZoCIdolGU-NM3-4yDxC0t868iL&mm=31%2C26&mn=sn-aigllnlr%2Csn-5hne6nsy&c=WEB",
        "quality": "medium",
        "itag": "43",
        "type": "video\/webm; codecs=\"vp8.0, vorbis\""
    },
    {
        "url": "https:\/\/r10---sn-aigllnlr.googlevideo.com\/videoplayback?key=yt6&mt=1522999783&gir=yes&expire=1523021537&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&itag=18&ratebypass=yes&fvip=2&ipbits=0&mime=video%2Fmp4&initcwndbps=710000&signature=C83DE33E3DC80981A65DB3FE4E6B3A48BF7500E4.361D0EE6210B30D3D3A80F43228DEF1BD20691A4&clen=15954979&requiressl=yes&dur=244.204&pl=22&ip=185.27.134.50&lmt=1522960340235683&ei=gSLHWvuxDMOUVYaTqYgB&ms=au%2Conr&source=youtube&mv=m&id=o-AAoaDzyDCVXS404wfqZoCIdolGU-NM3-4yDxC0t868iL&mm=31%2C26&mn=sn-aigllnlr%2Csn-5hne6nsy&c=WEB",
        "quality": "medium",
        "itag": "18",
        "type": "video\/mp4; codecs=\"avc1.42001E, mp4a.40.2\""
    },
    {
        "url": "https:\/\/r10---sn-aigllnlr.googlevideo.com\/videoplayback?key=yt6&mt=1522999783&gir=yes&expire=1523021537&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&itag=36&fvip=2&ipbits=0&mime=video%2F3gpp&initcwndbps=710000&signature=3E993D911492DA039A16BB26182ACDC6C6A04FCC.BFB9728C71CD03970B0F15AFD51A7355F9D3F899&clen=6759799&requiressl=yes&dur=244.273&pl=22&ip=185.27.134.50&lmt=1522957367267598&ei=gSLHWvuxDMOUVYaTqYgB&ms=au%2Conr&source=youtube&mv=m&id=o-AAoaDzyDCVXS404wfqZoCIdolGU-NM3-4yDxC0t868iL&mm=31%2C26&mn=sn-aigllnlr%2Csn-5hne6nsy&c=WEB",
        "quality": "small",
        "itag": "36",
        "type": "video\/3gpp; codecs=\"mp4v.20.3, mp4a.40.2\""
    },
    {
        "url": "https:\/\/r10---sn-aigllnlr.googlevideo.com\/videoplayback?key=yt6&mt=1522999783&gir=yes&expire=1523021537&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&itag=17&fvip=2&ipbits=0&mime=video%2F3gpp&initcwndbps=710000&signature=810D13A2C507A4EA220E6DA895B39B237FA22DAF.898D020851087CF3C10BC6E3ED7360736A239904&clen=2443931&requiressl=yes&dur=244.273&pl=22&ip=185.27.134.50&lmt=1522957365473654&ei=gSLHWvuxDMOUVYaTqYgB&ms=au%2Conr&source=youtube&mv=m&id=o-AAoaDzyDCVXS404wfqZoCIdolGU-NM3-4yDxC0t868iL&mm=31%2C26&mn=sn-aigllnlr%2Csn-5hne6nsy&c=WEB",
        "quality": "small",
        "itag": "17",
        "type": "video\/3gpp; codecs=\"mp4v.20.3, mp4a.40.2\""
    }
]

update :

to see the source code :

GIST: https://gist.github.com/el3zahaby/9e60f1ae3168c38cc0f0054c15cd6a83

Sorry sir, You cannot do this with youtube api v3. you have to use a url of youtube which is not an api, but here you can get all the videos related to this. See http://www.youtube.com/get_video_info?&video_id='. $my_id.'&asv=3&el=detailpage&hl=en_US

Or you can do to get all the videos download link even it is private or not allow for your country 1st: Go to any webpage of youtube videos link https://www.youtube.com/watch?v=9mdJV5-eias

2nd : view source of that page

3rd : 188 or 187 line, where you find source Javascript codes where location of the videos with mp4 format is also available.

You can do the second Idea by simplehtmldom and some php functions. and the first one can get by using curl which is easy but in little bit hard way by php. Thank you hope this will help you.

For Local to Java/Android here is how i achieved this, credit goes to @abdo-el-zahaby i converted his php script to ~equivalent java code it uses okhttp client to get urls

final String videoInfoUrl = "http://www.youtube.com/get_video_info?video_id=some_video_id&el=embedded&ps=default&eurl=&gl=US&hl=en";
                Request request = new Request.Builder()
                        .cacheControl(CacheControl.FORCE_NETWORK)
                        .url(videoInfoUrl)
                        .build();
                final Response response = okHttpClient.newCall(request).execute();
                InputStream inputStream = response.body().byteStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                final StringBuilder contentBuilder = new StringBuilder();
                while ((line = bufferedReader.readLine()) != null) {
                    contentBuilder.append(line);
                }

                final String streamKey = "url_encoded_fmt_stream_map";
                final Map<String, String> map = new HashMap<>();
                final String content = contentBuilder.toString();
                String[] ampSplit = content.split("&");
                for (String s : ampSplit) {
                    printDivider();
                    final String[] equalsPlit = s.split("=");
                    if (equalsPlit.length >= 2) {
                        String key;
                        String value;
                        key = equalsPlit[0];
                        value = equalsPlit[1];
                        map.put(key, value);
                    }
                    printDivider();
                }
                int count = 0;
                if (map.containsKey(streamKey)) {
                    String[] streams = map.get(streamKey).split(",");
                    for (String stream : streams) {

                        String[] streamSplit = stream.split("&");
                        for (String s : streamSplit) {
                            printDivider();
                            final String urlDecoded = URLDecoder.decode(s, "UTF-8");
                            String[] details = urlDecoded.split(",");
                            for (String detail : details) {
                                System.out.println("Detail " + URLDecoder.decode(detail, "UTF-8"));
                                final String urlContent= URLDecoder.decode(detail, "UTF-8");
                                final String url = urlContent.substring(urlContent.indexOf("http"), urlContent.indexOf(";"));
                                mp4Url.put(Integer.toString(count++), url);
                            }

                        }
                        printDivider();
                    }
                }

This is the code i am using to download and store in sdcard/internal memory

Request request = new Request.Builder()
              .cacheControl(CacheControl.FORCE_NETWORK)
              .url(url)
              .build();
          final Response response = okHttpClient.newCall(request).execute();
          InputStream inputStream = response.body().byteStream();
          final File newFile = new File(location);
          boolean created = newFile.createNewFile();
          System.out.println(location + " new file created: " + created);

          byte[] buff = new byte[4096];
          long downloaded = 0;
          long target = response.body().contentLength();

          System.out.println("File size is: " + Long.toString(target));
          OutputStream outStream = new FileOutputStream(newFile);
          while (true) {

            int read = inputStream.read(buff);
            if (read == -1) {
              break;
            }
            outStream.write(buff, 0, read);
            //write buff
            downloaded += read;
          }

          System.out.println("Target: " + target +", Downloaded: " + downloaded);
          outStream.flush();

I don't know how it works, but check out https://podsync.net/.

When you input a youtube playlist link, it does some magic and generates an mp4 link to be used by podcast catchers. Examine the returned xml file and you'll see lines like this for each video in the playlist:

<enclosure url="http://podsync.net/download/[random-letters]/[video-id].mp4" length="x" type="video/mp4"></enclosure>

In my experience, that URL works no matter what video-id you use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!