Code for download video from Youtube on Java, Android

前端 未结 3 913
难免孤独
难免孤独 2020-11-30 17:19

I created code for download video from Youtube, but this code doesn\'t work with Wi-fi connection and work with mobile connection. Where did I have mistake?

         


        
3条回答
  •  旧时难觅i
    2020-11-30 17:53

    Ref : Youtube Video Download (Android/Java)

    private static final HashMap typeMap = new HashMap();
    

    initTypeMap(); call first

    class 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;
        }
    }
    
    class 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;
        }
    }
    
    public ArrayList

    Edit 2:

    Some time This Code Not worked proper

    Same-origin policy

    https://en.wikipedia.org/wiki/Same-origin_policy

    https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

    problem of Same-origin policy. Essentially, you cannot download this file from www.youtube.com because they are different domains. A workaround of this problem is [CORS][1]. 
    

    Ref : https://superuser.com/questions/773719/how-do-all-of-these-save-video-from-youtube-services-work/773998#773998

    url_encoded_fmt_stream_map // traditional: contains video and audio stream
    adaptive_fmts              // DASH: contains video or audio stream
    

    Each of these is a comma separated array of what I would call "stream objects". Each "stream object" will contain values like this

    url  // direct HTTP link to a video
    itag // code specifying the quality
    s    // signature, security measure to counter downloading
    

    Each URL will be encoded so you will need to decode them. Now the tricky part.

    YouTube has at least 3 security levels for their videos

    unsecured // as expected, you can download these with just the unencoded URL
    s         // see below
    RTMPE     // uses "rtmpe://" protocol, no known method for these
    

    The RTMPE videos are typically used on official full length movies, and are protected with SWF Verification Type 2. This has been around since 2011 and has yet to be reverse engineered.

    The type "s" videos are the most difficult that can actually be downloaded. You will typcially see these on VEVO videos and the like. They start with a signature such as

    AA5D05FA7771AD4868BA4C977C3DEAAC620DE020E.0F421820F42978A1F8EAFCDAC4EF507DB5 Then the signature is scrambled with a function like this

    function mo(a) {
      a = a.split("");
      a = lo.rw(a, 1);
      a = lo.rw(a, 32);
      a = lo.IC(a, 1);
      a = lo.wS(a, 77);
      a = lo.IC(a, 3);
      a = lo.wS(a, 77);
      a = lo.IC(a, 3);
      a = lo.wS(a, 44);
      return a.join("")
    }
    

    This function is dynamic, it typically changes every day. To make it more difficult the function is hosted at a URL such as

    http://s.ytimg.com/yts/jsbin/html5player-en_US-vflycBCEX.js

    this introduces the problem of Same-origin policy. Essentially, you cannot download this file from www.youtube.com because they are different domains. A workaround of this problem is CORS. With CORS, s.ytimg.com could add this header

    Access-Control-Allow-Origin: http://www.youtube.com
    

    and it would allow the JavaScript to download from www.youtube.com. Of course they do not do this. A workaround for this workaround is to use a CORS proxy. This is a proxy that responds with the following header to all requests

    Access-Control-Allow-Origin: *
    

    So, now that you have proxied your JS file, and used the function to scramble the signature, you can use that in the querystring to download a video.

提交回复
热议问题