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

5

36

How do I get the full mp4 url to play the video from its actual location in my application using some other source except Youtube. The gdata/youtube API has been deprecated so I am having trouble. Any help will be appreciated. Thanks.

Anteversion answered 22/7, 2015 at 14:58 Comment(1)
[PHP] this solution Based on the Latest Youtube Website Update (2019). #55078523Retread
M
22

i made a very simple API : https://gist.github.com/egyjs/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 (change example.com to your site) :

https://example.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/egyjs/9e60f1ae3168c38cc0f0054c15cd6a83

Merrilee answered 6/4, 2018 at 7:38 Comment(9)
Why doesn't your api pull 1080 or 4K links?Dewees
I have added android version locally can u check. @abdo answer is the source referenceDylane
nope. this one won't work for example: youtube.com/watch?v=2r6DW88zi4sHellbox
would be nice if you just pasted your code here in the future, rather than link to something with your own adsPrincipled
@DevinSmith If you're seeing ads on GitHub Gist, I may have some bad news about the sorts of software that may be running on your computer.Rafat
@MauricioAriasOlave Unfortunately I do not have a server to upload the code on itMerrilee
@spacerGIF Check line 51/52Veii
Gist is a 404 and website redirects to scamShir
I feel like this is totally unworking apiPestiferous
G
7

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.

Gorski answered 1/5, 2016 at 15:31 Comment(0)
D
4

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();
Dylane answered 9/6, 2018 at 4:31 Comment(2)
lets make it a real project , What do you think :)Merrilee
Hi, are you sure it is working ok? I am getting and index out of bounds exception. Can you place it in a gist ?Spearmint
T
0

That is simple In MSEDGE, get the requestes panel in Network, then type:

videoplayback

You'll get a list of videoplaybacks, go to the red first three ones, get the first one, then copy the URL, go to it, then copy and paste the original URL that appears, and voila! EX: https://www.youtube.com/watch?v=kyCJ17oq6kI is translated into https://rr5---sn-4g5lznek.googlevideo.com/videoplayback?expire=1681055644&ei=PIsyZLKiDf2pxN8Pycma6Ac&ip=197.133.92.36&id=o-AG5C0lZuVpm9QTlY3-bdsnaxXFDmYWZNMfSnHEevldCX&itag=244&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C271%2C278%2C313%2C394%2C395%2C396%2C397%2C398%2C399%2C400%2C401&source=youtube&requiressl=yes&spc=99c5CQGgativKiOByrtN4P3IwKXbF2U&vprv=1&mime=video%2Fwebm&ns=GZQIER86iMv1E06YUDtOUIUM&gir=yes&clen=51126864&dur=930.629&lmt=1661735478649394&keepalive=yes&fexp=24007246&c=WEB_EMBEDDED_PLAYER&txp=5537434&n=60EJG2vTL7kL_A&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAKOA0iAqWrySUp9NpkpcxJNJQ6C1_BWfa6b91zStEu5TAiA80wiRQvGJFEd-4iuq4fjM5r7SAc1zPjr4xv6PweEgxw%3D%3D&alr=yes&cpn=pDl8mDxKkx62PK2k&cver=1.20230402.00.00&range=0-245662&rn=1&rbuf=0&pot=MmQ1aC1VTvzpGTVQZjSBWLyWnbiGJ0bOAtQgoIovIYrGI3nBfAVO60KmMbRKcRxq5ySqI9TAJ4rqBnfcuoXzj7CAgJAA24AmShxWJhrSkAoI0LrSFPC_N6FCyu6xkeOf38wpoFXa&altitags=243%2C242&redirect_counter=1&cm2rm=sn-8vq54voxxb-j5pd7d&req_id=9c5d1ec279bcda59&cms_redirect=yes&cmsv=e&mh=Yy&mm=29&mn=sn-4g5lznek&ms=rdu&mt=1681033985&mv=m&mvi=5&pl=22&lsparams=mh,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRQIgcOPSyxayGZHivxtPhZzhmckywIeotUdp0Ny6sepZS-4CIQD52bhTS6Xu_GOWGg_GCIIl2ZcXXaQpy-mMMzhZaB2Mbg%3D%3D.

Try it.

Taxable answered 9/4, 2023 at 10:39 Comment(0)
F
-6

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.

Fachanan answered 5/2, 2018 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.