2015-04-13 23 views
25

Tôi đang cố gắng phát video youtube trong exoplayer nhưng đây là một số nhầm lẫn tôi không biết url DASH là gì, tôi chỉ có url youtube thực như "https://www.youtube.com/watch?v=v1uyQZNg2vE", tôi không biết làm thế nào để tạo url url dạng url thực .Cách phát video youtube trong ExoPlayer trong Android?

Dash Url:

new Sample("Google Glass", 
     "http://www.youtube.com/api/manifest/dash/id/bf5bb2419360daf1/source/youtube?" 
     + "as=fmp4_audio_clear,fmp4_sd_hd_clear&sparams=ip,ipbits,expire,as&ip=0.0.0.0&" 
     + "ipbits=0&expire=19000000000&signature=255F6B3C07C753C88708C07EA31B7A1A10703C8D." 
     + "2D6A28B21F921D0B245CDCF36F7EB54A2B5ABFC2&key=ik0", DemoUtil.TYPE_DASH), 

Bất Url:

https://www.youtube.com/watch?v=v1uyQZNg2vE 
+1

Bạn đã quản lý để tạo ra các url DASH? –

+0

Có cách nào hợp lệ để tạo URL DASH từ ID video – androidXP

Trả lời

6

Bạn sẽ phải nhận được phản ứng HTTP từ URL youtube (trong URL thực trường hợp của bạn) và sau đó tìm kiếm phần " url_encoded_fmt_stream_map ". Trong phần đó, bạn sẽ nhận được một URI cần được giải mã hai lần để nhận được URL DASH mà bạn đang tìm kiếm.

+0

Bạn có thể hiển thị cho chúng tôi URI ví dụ như thế nào không? –

7

tôi đã cùng một vấn đề nhưng tôi cuối cùng đã tìm thấy các giải pháp đơn giản nhất và làm việc của nó rất tốt

  1. Trước tiên, bạn cần phải gọi url này ..

    HTTP GET: https://www.youtube.com/get_video_info?&video_id=[video_id]&el=info&ps=default&eurl=&gl=US&hl=en

và đừng quên thay đổi id cuối cùng bằng id mục tiêu.

  1. bây giờ bạn sẽ nhận được thông báo tải xuống tệp có tên get_video_info không có extesion.
  2. cố gắng mở tệp này bằng notepad và như vậy.
  3. Bây giờ bạn có dữ liệu đúng, nhưng bạn không thể đọc nó vì mã hóa Bạn cần HTML bộ giải mã của nó để sậy sử dụng dữ liệu này này: http://meyerweb.com/eric/tools/dencoder/

-Cũng dán dữ liệu của bạn và nhấn giải mã một vài lần để đảm bảo nó được giải mã cũng

cuối cùng tìm kiếm một phím gọi dashmpd và tận hưởng ur URL

+1

Không có phím nào được gọi là 'dashmpd'. Nó vẫn còn hiệu lực phải không? –

+0

Sử dụng url này https://www.youtube.com/get_video_info?&video_id=[video_id]&el=info&ps=default&eurl=&gl=US&hl=vi thay vì –

9

tôi đã viết một lớp để lấy URL của video trực tuyến YouTube thực tế cho định dạng như DASH và HLS sử dụng http://www.youtube.com/get_video_info?&video_id=[video_id]&el=info&ps=default&eurl=&gl=US&hl=en url có ID video như được mô tả bởi Karim Abdell Salam. Tôi cũng đã thử nghiệm URL trong một ứng dụng mà sử dụng ExoPlayer và nó hoạt động:

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 
import java.net.URLDecoder; 
import java.util.Map; 
import java.util.TreeMap; 

/** 
* Represents youtube video information retriever. 
*/ 
public class YouTubeVideoInfoRetriever 
{ 
    private static final String URL_YOUTUBE_GET_VIDEO_INFO = "http://www.youtube.com/get_video_info?&video_id="; 

    public static final String KEY_DASH_VIDEO = "dashmpd"; 
    public static final String KEY_HLS_VIDEO = "hlsvp"; 

    private TreeMap<String, String> kvpList = new TreeMap<>(); 

    public void retrieve(String videoId) throws IOException 
    { 
     String targetUrl = URL_YOUTUBE_GET_VIDEO_INFO + videoId+"&el=info&ps=default&eurl=&gl=US&hl=en"; 
     SimpleHttpClient client = new SimpleHttpClient(); 
     String output = client.execute(targetUrl, SimpleHttpClient.HTTP_GET, SimpleHttpClient.DEFAULT_TIMEOUT); 
     parse(output); 
    } 

    public String getInfo(String key) 
    { 
     return kvpList.get(key); 
    } 

    public void printAll() 
    { 
     System.out.println("TOTAL VARIABLES=" + kvpList.size()); 

     for(Map.Entry<String, String> entry : kvpList.entrySet()) 
     { 
      System.out.print("" + entry.getKey() + "="); 
      System.out.println("" + entry.getValue() + ""); 
     } 
    } 

    private void parse(String data) throws UnsupportedEncodingException 
    { 
     String[] splits = data.split("&"); 
     String kvpStr = ""; 

     if(splits.length < 1) 
     { 
      return; 
     } 

     kvpList.clear(); 

     for(int i = 0; i < splits.length; ++i) 
     { 
      kvpStr = splits[i]; 

      try 
      { 
       // Data is encoded multiple times 
       kvpStr = URLDecoder.decode(kvpStr, SimpleHttpClient.ENCODING_UTF_8); 
       kvpStr = URLDecoder.decode(kvpStr, SimpleHttpClient.ENCODING_UTF_8); 

       String[] kvpSplits = kvpStr.split("=", 2); 

       if(kvpSplits.length == 2) 
       { 
        kvpList.put(kvpSplits[0], kvpSplits[1]); 
       } 
       else if(kvpSplits.length == 1) 
       { 
        kvpList.put(kvpSplits[0], ""); 
       } 
      } 
      catch (UnsupportedEncodingException ex) 
      { 
       throw ex; 
      } 
     } 
    } 

    public static class SimpleHttpClient 
    { 
     public static final String ENCODING_UTF_8 = "UTF-8"; 
     public static final int DEFAULT_TIMEOUT = 10000; 

     public static final String HTTP_GET = "GET"; 

     public String execute(String urlStr, String httpMethod, int timeout) throws IOException 
     { 
      URL url = null; 
      HttpURLConnection conn = null; 
      InputStream inStream = null; 
      OutputStream outStream = null; 
      String response = null; 

      try 
      { 
       url = new URL(urlStr); 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setConnectTimeout(timeout); 
       conn.setRequestMethod(httpMethod); 

       inStream = new BufferedInputStream(conn.getInputStream()); 
       response = getInput(inStream); 
      } 
      finally 
      { 
       if(conn != null && conn.getErrorStream() != null) 
       { 
        String errorResponse = " : "; 
        errorResponse = errorResponse + getInput(conn.getErrorStream()); 
        response = response + errorResponse; 
       } 

       if (conn != null) 
       { 
        conn.disconnect(); 
       } 
      } 

      return response; 
     } 

     private String getInput(InputStream in) throws IOException 
     { 
      StringBuilder sb = new StringBuilder(8192); 
      byte[] b = new byte[1024]; 
      int bytesRead = 0; 

      while (true) 
      { 
       bytesRead = in.read(b); 
       if (bytesRead < 0) 
       { 
        break; 
       } 
       String s = new String(b, 0, bytesRead, ENCODING_UTF_8); 
       sb.append(s); 
      } 

      return sb.toString(); 
     } 

    } 
} 

Đây là mã kiểm tra:

public static void main(String[] args) 
{ 
    String youTubeVideoID = "v1uyQZNg2vE"; 

    YouTubeVideoInfoRetriever retriever = new YouTubeVideoInfoRetriever(); 

    try 
    { 
     retriever.retrieve(youTubeVideoID); 
     System.out.println(retriever.getInfo(YouTubeVideoInfoRetriever.KEY_DASH_VIDEO)); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

Câu trả lời hay! Cảm ơn bạn! – user486312

+0

Công việc tuyệt vời! Thaaanx –

+0

@ MARK002-MAB, tôi đã thử ở trên mã công việc của nó chỉ tạm thời. Không thành công sau một thời gian –

Các vấn đề liên quan