Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java和Android上从Youtube下载视频的代码_Java_Android_Youtube - Fatal编程技术网

在Java和Android上从Youtube下载视频的代码

在Java和Android上从Youtube下载视频的代码,java,android,youtube,Java,Android,Youtube,我创建了从Youtube下载视频的代码,但该代码不适用于Wi-fi连接,也不适用于移动连接。我哪里出错了 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.Ma

我创建了从Youtube下载视频的代码,但该代码不适用于Wi-fi连接,也不适用于移动连接。我哪里出错了

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Vector;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class MyActivity extends Activity {

    private class ReceivingDataFromYoutube extends AsyncTask<String, Void, Void> {

        private ProgressDialog dialog = new ProgressDialog(MyActivity.this);
        private String result;

        protected void onPreExecute() {
            dialog.setMessage("Downloading...");
            dialog.show();
        }

        @Override
        protected Void doInBackground(String... arg0) {
            int begin, end;
            String tmpstr = null;
            try {
                URL url=new URL("http://www.youtube.com/watch?v=y12-1miZHLs&nomobile=1");
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                InputStream stream=con.getInputStream();
                InputStreamReader reader=new InputStreamReader(stream);
                StringBuffer buffer=new StringBuffer();
                char[] buf=new char[262144];                
                int chars_read;
                while ((chars_read = reader.read(buf, 0, 262144)) != -1) {
                    buffer.append(buf, 0, chars_read);
                }
                tmpstr=buffer.toString();

                begin  = tmpstr.indexOf("url_encoded_fmt_stream_map=");
                end = tmpstr.indexOf("&", begin + 27);
                if (end == -1) {
                    end = tmpstr.indexOf("\"", begin + 27);
                }
                tmpstr = UtilClass.URLDecode(tmpstr.substring(begin + 27, end));

            } catch (MalformedURLException e) {
                throw new RuntimeException();
            } catch (IOException e) {
                throw new RuntimeException();
            }

            Vector url_encoded_fmt_stream_map = new Vector();
            begin = 0;
            end   = tmpstr.indexOf(",");

            while (end != -1) {
                url_encoded_fmt_stream_map.addElement(tmpstr.substring(begin, end));
                begin = end + 1;
                end   = tmpstr.indexOf(",", begin);
            }

            url_encoded_fmt_stream_map.addElement(tmpstr.substring(begin, tmpstr.length()));
            String result = "";
            Enumeration url_encoded_fmt_stream_map_enum = url_encoded_fmt_stream_map.elements();
            while (url_encoded_fmt_stream_map_enum.hasMoreElements()) {
                tmpstr = (String)url_encoded_fmt_stream_map_enum.nextElement();
                begin = tmpstr.indexOf("itag=");
                if (begin != -1) {
                    end = tmpstr.indexOf("&", begin + 5);

                    if (end == -1) {
                          end = tmpstr.length();
                    }

                    int fmt = Integer.parseInt(tmpstr.substring(begin + 5, end));

                    if (fmt == 35) {
                        begin = tmpstr.indexOf("url=");
                        if (begin != -1) {
                            end = tmpstr.indexOf("&", begin + 4);
                            if (end == -1) {
                               end = tmpstr.length();
                            }
                            result = UtilClass.URLDecode(tmpstr.substring(begin + 4, end));
                            this.result=result;
                            break;
                        }
                    }
                }
            }         
            try {
              URL u = new URL(result);
              HttpURLConnection c = (HttpURLConnection) u.openConnection();
              c.setRequestMethod("GET");
/*              c.setRequestProperty("Youtubedl-no-compression", "True");
              c.setRequestProperty("User-Agent", "YouTube");*/

              c.setDoOutput(true);
              c.connect();

              FileOutputStream f=new FileOutputStream(new File("/sdcard/3.flv"));

              InputStream in=c.getInputStream();
              byte[] buffer=new byte[1024];
              int sz = 0;
              while ( (sz = in.read(buffer)) > 0 ) {
                   f.write(buffer,0, sz);
              }
              f.close();
            } catch (MalformedURLException e) {
                new RuntimeException();
            } catch (IOException e) {
                new RuntimeException();
            }
            return null;
        }

        protected void onPostExecute(Void unused) {
            dialog.dismiss();
        }    

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new ReceivingDataFromYoutube().execute();
    }   
}  
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.Enumeration;
导入java.util.Vector;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.MediaController;
导入android.widget.VideoView;
公共类MyActivity扩展了活动{
从YouTube接收数据的私有类扩展异步任务{
private ProgressDialog=新建ProgressDialog(MyActivity.this);
私有字符串结果;
受保护的void onPreExecute(){
setMessage(“下载…”);
dialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
int开始,结束;
字符串tmpstr=null;
试一试{
URL=新URL(“http://www.youtube.com/watch?v=y12-1最小值和最小值=1“;
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
InputStream=con.getInputStream();
InputStreamReader reader=新的InputStreamReader(流);
StringBuffer=新的StringBuffer();
char[]buf=新字符[262144];
int chars_read;
while((chars_read=reader.read(buf,0262144))!=-1){
追加(buf,0,字符读取);
}
tmpstr=buffer.toString();
begin=tmpstr.indexOf(“url_encoded_fmt_stream_map=”);
end=tmpstr.indexOf(“&”,begin+27);
如果(结束==-1){
end=tmpstr.indexOf(“\”,begin+27);
}
tmpstr=UtilClass.URLDecode(tmpstr.substring(begin+27,end));
}捕获(格式错误){
抛出新的RuntimeException();
}捕获(IOE异常){
抛出新的RuntimeException();
}
矢量url_编码_fmt_流_映射=新矢量();
开始=0;
end=tmpstr.indexOf(“,”);
while(结束!=-1){
url_编码的_fmt_流_map.addElement(tmpstr.substring(begin,end));
开始=结束+1;
end=tmpstr.indexOf(“,”开始);
}
url_encoded_fmt_stream_map.addElement(tmpstr.substring(begin,tmpstr.length());
字符串结果=”;
枚举url_encoded_fmt_stream_map_enum=url_encoded_fmt_stream_map.elements();
while(url\u encoded\u fmt\u stream\u map\u enum.hasMoreElements()){
tmpstr=(字符串)url_encoded_fmt_stream_map_enum.nextElement();
begin=tmpstr.indexOf(“itag=”);
如果(开始!=-1){
end=tmpstr.indexOf(“&”,begin+5);
如果(结束==-1){
end=tmpstr.length();
}
int fmt=Integer.parseInt(tmpstr.substring(begin+5,end));
如果(fmt==35){
begin=tmpstr.indexOf(“url=”);
如果(开始!=-1){
end=tmpstr.indexOf(“&”,begin+4);
如果(结束==-1){
end=tmpstr.length();
}
结果=UtilClass.URLDecode(tmpstr.substring(begin+4,end));
结果=结果;
打破
}
}
}
}         
试一试{
URL u=新URL(结果);
HttpURLConnection c=(HttpURLConnection)u.openConnection();
c、 setRequestMethod(“GET”);
/*c.setRequestProperty(“Youtubedl无压缩”、“真”);
c、 setRequestProperty(“用户代理”、“YouTube”)*/
c、 设置输出(真);
c、 connect();
FileOutputStream f=新的FileOutputStream(新文件(“/sdcard/3.flv”);
InputStream in=c.getInputStream();
字节[]缓冲区=新字节[1024];
int sz=0;
而((sz=in.read(buffer))>0){
f、 写入(缓冲区,0,sz);
}
f、 close();
}捕获(格式错误){
新的RuntimeException();
}捕获(IOE异常){
新的RuntimeException();
}
返回null;
}
受保护的void onPostExecute(未使用的void){
dialog.dismise();
}    
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
从YouTube()接收新数据。执行();
}   
}  
3个步骤:

  • 检查YouTube的源代码(HTML),您将获得如下链接
    http%253A%252F%252Fo-o.preferred.telemar-cnf1.v18.lscache6.c.YouTube.com%252Fvideoplayback…

  • 使用代码对url进行解码(删除代码
    %2B
    %25
    等),并使用函数
    Uri.Decode(url)
    替换无效的转义八位字节

  • 使用代码下载流:

  • URL u=null;
    InputStream=null;
    试一试{
    u=新URL(URL);
    is=u.openStream();
    HttpURLConnection huc=(HttpURLConnection)u.ope
    
     allprojects {
            repositories {
                maven { url "https://jitpack.io" }
            }
        }
    
     compile 'com.github.Commit451.YouTubeExtractor:youtubeextractor:2.1.0'
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String YOUTUBE_ID = "ea4-5mrpGfE";
    
        private final YouTubeExtractor mExtractor = YouTubeExtractor.create();
    
    
        private Callback<YouTubeExtractionResult> mExtractionCallback = new Callback<YouTubeExtractionResult>() {
            @Override
            public void onResponse(Call<YouTubeExtractionResult> call, Response<YouTubeExtractionResult> response) {
                bindVideoResult(response.body());
            }
    
            @Override
            public void onFailure(Call<YouTubeExtractionResult> call, Throwable t) {
                onError(t);
            }
        };
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    //        For android youtube extractor library  com.github.Commit451.YouTubeExtractor:youtubeextractor:2.1.0'
            mExtractor.extract(YOUTUBE_ID).enqueue(mExtractionCallback);
    
        }
    
    
        private void onError(Throwable t) {
            t.printStackTrace();
            Toast.makeText(MainActivity.this, "It failed to extract. So sad", Toast.LENGTH_SHORT).show();
        }
    
    
        private void bindVideoResult(YouTubeExtractionResult result) {
    
    //        Here you can get download url link
            Log.d("OnSuccess", "Got a result with the best url: " + result.getBestAvailableQualityVideoUri());
    
            Toast.makeText(this, "result : " + result.getSd360VideoUri(), Toast.LENGTH_SHORT).show();
        }
    }
    
    repositories {
        maven { url "https://jitpack.io" }
    }
    
    compile 'com.github.HaarigerHarald:android-youtubeExtractor:master-SNAPSHOT'
    
           String youtubeLink = "http://youtube.com/watch?v=xxxx";
    
        YouTubeUriExtractor ytEx = new YouTubeUriExtractor(this) {
            @Override
            public void onUrisAvailable(String videoId, String videoTitle, SparseArray<YtFile> ytFiles) {
                if (ytFiles != null) {
                    int itag = 22;
    // Here you can get download url
                    String downloadUrl = ytFiles.get(itag).getUrl();
                }
            }
        };
    
        ytEx.execute(youtubeLink);
    
    private static final HashMap<String, Meta> typeMap = new HashMap<String, Meta>();
    
    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<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)";*/
       /* HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                userAgent);
        HttpGet request = new HttpGet(ytUrl);
        HttpResponse response = client.execute(request);*/
        String html = "";
        HttpsURLConnection c = (HttpsURLConnection) new URL(ytUrl).openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        InputStream in = c.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line.replace("\\u0026", "&"));
        }
        in.close();
        html = str.toString();
    
        // Parse the HTML response and extract the streaming URIs
        if (html.contains("verify-age-thumb")) {
            Log.e("Downloader", "YouTube is asking for age verification. We can't handle that sorry.");
            return null;
        }
    
        if (html.contains("das_captcha")) {
            Log.e("Downloader", "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) {
            Log.e("Downloader", "Found zero or too many stream maps.");
            return null;
        }
    
        String urls[] = matches.get(0).split(",");
        HashMap<String, String> foundArray = new HashMap<String, String>();
        for (String ppUrl : urls) {
            String url = URLDecoder.decode(ppUrl, "UTF-8");
            Log.e("URL","URL : "+url);
    
            Pattern p1 = Pattern.compile("itag=([0-9]+?)[&]");
            Matcher m1 = p1.matcher(url);
            String itag = null;
            if (m1.find()) {
                itag = m1.group(1);
            }
    
            Pattern p2 = Pattern.compile("signature=(.*?)[&]");
            Matcher m2 = p2.matcher(url);
            String sig = null;
            if (m2.find()) {
                sig = m2.group(1);
            } else {
                Pattern p23 = Pattern.compile("signature&s=(.*?)[&]");
                Matcher m23 = p23.matcher(url);
                if (m23.find()) {
                    sig = m23.group(1);
                }
            }
    
            Pattern p3 = Pattern.compile("url=(.*?)[&]");
            Matcher m3 = p3.matcher(ppUrl);
            String um = null;
            if (m3.find()) {
                um = m3.group(1);
            }
    
            if (itag != null && sig != null && um != null) {
                Log.e("foundArray","Adding Value");
                foundArray.put(itag, URLDecoder.decode(um, "UTF-8") + "&"
                        + "signature=" + sig);
            }
        }
        Log.e("foundArray","Size : "+foundArray.size());
        if (foundArray.size() == 0) {
            Log.e("Downloader", "Couldn't find any URLs and corresponding signatures");
            return null;
        }
    
    
        ArrayList<Video> videos = new ArrayList<Video>();
    
        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);
                Log.d("Downloader", "YouTube Video streaming details: ext:" + newVideo.ext
                        + ", type:" + newVideo.type + ", url:" + newVideo.url);
            }
        }
    
        return videos;
    }
    
    private class YouTubePageStreamUriGetter extends AsyncTask<String, String, ArrayList<Video>> {
        ProgressDialog progressDialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(webViewActivity.this, "",
                    "Connecting to YouTube...", true);
        }
    
        @Override
        protected ArrayList<Video> doInBackground(String... params) {
            ArrayList<Video> fVideos = new ArrayList<>();
            String url = params[0];
            try {
                ArrayList<Video> videos = getStreamingUrisFromYouTubePage(url);
                /*                Log.e("Downloader","Size of Video : "+videos.size());*/
                if (videos != null && !videos.isEmpty()) {
                    for (Video video : videos)
                    {
                        Log.e("Downloader", "ext : " + video.ext);
                        if (video.ext.toLowerCase().contains("mp4") || video.ext.toLowerCase().contains("3gp") || video.ext.toLowerCase().contains("flv") || video.ext.toLowerCase().contains("webm")) {
                            ext = video.ext.toLowerCase();
                            fVideos.add(new Video(video.ext,video.type,video.url));
                        }
                    }
    
    
                    return fVideos;
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Downloader", "Couldn't get YouTube streaming URL", e);
            }
            Log.e("Downloader", "Couldn't get stream URI for " + url);
            return null;
        }
    
        @Override
        protected void onPostExecute(ArrayList<Video> streamingUrl) {
            super.onPostExecute(streamingUrl);
            progressDialog.dismiss();
            if (streamingUrl != null) {
                if (!streamingUrl.isEmpty()) {
                    //Log.e("Steaming Url", "Value : " + streamingUrl);
    
                    for (int i = 0; i < streamingUrl.size(); i++) {
                        Video fX = streamingUrl.get(i);
                        Log.e("Founded Video", "URL : " + fX.url);
                        Log.e("Founded Video", "TYPE : " + fX.type);
                        Log.e("Founded Video", "EXT : " + fX.ext);
                    }
                    //new ProgressBack().execute(new String[]{streamingUrl, filename + "." + ext});
                }
            }
        }
    }
    public void initTypeMap()
    {
        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"));
    }
    
    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]. 
    
    url_encoded_fmt_stream_map // traditional: contains video and audio stream
    adaptive_fmts              // DASH: contains video or audio stream
    
    url  // direct HTTP link to a video
    itag // code specifying the quality
    s    // signature, security measure to counter downloading
    
    unsecured // as expected, you can download these with just the unencoded URL
    s         // see below
    RTMPE     // uses "rtmpe://" protocol, no known method for these
    
    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("")
    }
    
    Access-Control-Allow-Origin: http://www.youtube.com
    
    Access-Control-Allow-Origin: *