Search code examples
androidhttpsyoutubertsp

How to get RTSP URL from HTTPS URL?


How to convert HTTPS to RTSP URL.

Can anyone provide me solution with proper example of code?

Here is my code :

  MediaController mc = new MediaController(MainActivity.this);
        mc.setAnchorView(videoView);
        Uri video = Uri.parse(newurl);
        videoView.setMediaController(mc);
        videoView.setVideoURI(video);

Solution

  • Try following methods.

    Note: Make sure you use getUrlVideoRTSP method inside any background thread. Use AsyncTask or Thread to perform Network Operation.

    public static String getUrlVideoRTSP(String urlYoutube)
    {
        try
        {
            String gdy = "http://gdata.youtube.com/feeds/api/videos/";
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            String id = extractYoutubeId(urlYoutube);
            URL url = new URL(gdy + id);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            Document doc = documentBuilder.parse(connection.getInputStream());
            Element el = doc.getDocumentElement();
            NodeList list = el.getElementsByTagName("media:content");///media:content
            String cursor = urlYoutube;
            for (int i = 0; i < list.getLength(); i++)
            {
                Node node = list.item(i);
                if (node != null)
                {
                    NamedNodeMap nodeMap = node.getAttributes();
                    HashMap<String, String> maps = new HashMap<String, String>();
                    for (int j = 0; j < nodeMap.getLength(); j++)
                    {
                        Attr att = (Attr) nodeMap.item(j);
                        maps.put(att.getName(), att.getValue());
                    }
                    if (maps.containsKey("yt:format"))
                    {
                        String f = maps.get("yt:format");
                        if (maps.containsKey("url"))
                        {
                            cursor = maps.get("url");
                        }
                        if (f.equals("1"))
                            return cursor;
                    }
                }
            }
            return cursor;
        }
        catch (Exception ex)
        {
            Log.e("Get Url Video RTSP Exception======>>", ex.toString());
        }
        return urlYoutube;
    
    }
    
    protected static String extractYoutubeId(String url) throws MalformedURLException
    {
        String id = null;
        try
        {
            String query = new URL(url).getQuery();
            if (query != null)
            {
                String[] param = query.split("&");
                for (String row : param)
                {
                    String[] param1 = row.split("=");
                    if (param1[0].equals("v"))
                    {
                        id = param1[1];
                    }
                }
            }
            else
            {
                if (url.contains("embed"))
                {
                    id = url.substring(url.lastIndexOf("/") + 1);
                }
            }
        }
        catch (Exception ex)
        {
            Log.e("Exception", ex.toString());
        }
        return id;
    }
    

    Example

    new Thread(new Runnable() {
        @Override
        public void run() {
            String newurl = getUrlVideoRTSP("http://www.youtube.com/watch?v=9fBcrzA-hWY"));
        }
    }).start();
    

    Edit

    Example 1

    Youtube URL => http://www.youtube.com/watch?v=44fZqJOQ_go
    RTSP URL => rtsp://r5---sn-a5m7zu7e.c.youtube.com/CiILENy73wIaGQkK_pCTqNmH4xMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

    Youtube URL => http://www.youtube.com/watch?v=9fBcrzA-hWY
    RTSP URL => rtsp://r3---sn-a5m7zu7k.c.youtube.com/CiILENy73wIaGQlmhT4wr1zw9RMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp