1

I am just starting to learn android application development. And, right now I am in the process of creating a download manager which can download videos from youtube. I would like to know how to decode the URL of the youtube link that appears on your address bar and convert them to a downloadable link. For ex : I tried pasting a URL from youtube video

Say the URL goes like this : https://www.youtube.com/watch?v=ziJrvjjfqSU I am not able to download the video. Then, I tried to go with the embedded URL link of the video .

Say the URL : http://youtu.be/ziJrvjjfqSU Failed to download the video.

But this time I used an online decoder and decoded the URL of the same video.

https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DziJrvjjfqSU

This time , it works. So can anyone pls tell me how to decode any video URL in youtube to download them ? I am so thankful to all the developers out here in this forum for sorting all my earlier issues that I had posted. Now I hope someone would give a solution to my issue this time. Thanks to all those genius.

P.S: I am using download manager in my code to download the videos and parsing the corresponding URL in the program itself. Once this works out to be good for any URL , then I am planning to have an edit text through which one can paste the URL and download the videos.

Krish3090
  • 79
  • 2
  • 14

1 Answers1

0

This is an issue with encoding the URL. There is a conversion taking place e.g. : is converted to %3A and / is %2F Your code is working with the latter values.

From the Android Developers URL Encoder you can use:

The functionality is stated as:

All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') and characters '.', '-', '*', '_' are converted into their hexadecimal value prepended by '%'. For example: '#' -> %23. In addition, spaces are substituted by '+'.

public static String encode (String s, String charsetName)

OR

public static String encode (String s) 

The latter is the equivalent to encode(s, "UTF-8")

Encodes s using the Charset named by charsetName.

This Stack Overflow Question also has a lot of useful details.

Sample code:

try {
    // Your URL:
    String url = "http://www.stackoverflow.com/posts/25483244";
    // Call encode (to UTF-8 for this example)
    String encodedURL = URLEncoder.encode(url,"UTF-8");
    Log.d("TEST", encodedURL);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 

Remember that it is the "encodedURL" String that you want to use for the download, not the original "url" one. (Names from sample code above, obviously)

Community
  • 1
  • 1
RossC
  • 1,200
  • 2
  • 11
  • 24
  • Thanks a lot for answering bro, So you mean to say that, this link : https://www.youtube.com/watch?v=ziJrvjjfqSU should be encoded only after v= ?so only ziJrvjjfqSU is encoded to their respective hexadecimal values.. Please correct me if I'm wrong. – Krish3090 Aug 25 '14 at 10:10
  • If this one `https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DziJrvjjfqSU` works, then just encode it to exactly that using the above URL encoder. – RossC Aug 25 '14 at 10:10
  • From the link that you had attached in your answer post. I saw that and it states only the specific region of the URL is encoded and not the complete one. Is it true ? – Krish3090 Aug 25 '14 at 10:14
  • It encodes the URL. So in your case `: https://www.youtube.com/watch?v=ziJrvjjfqSU` will be changed to `https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DziJrvjjfqSU` which you said works. The Stackoverflow link is a very specific case, but you just need to encode the URL, which is already written into Android for you. – RossC Aug 25 '14 at 10:24
  • Cool... Thanks bro.. Will try that out in my code and let you know if it works. Should I have to give just an uri.encode(myUri) in my programming? – Krish3090 Aug 25 '14 at 10:31
  • Code added to answer now! `public static String encode (String s)` is equivalent to calling `encode(s, "UTF-8")` as per the documents, so try that. Failing that you can call the sample I gave above `public static String encode (String s, String charsetName)` if you need to choose a specific charset, which I don't think you will. Try it, come back and see how it goes. If it works consider accepting this answer and an upvote doesn't hurt either! – RossC Aug 25 '14 at 10:32
  • Thanks a ton bro.. Will come back after executing the code !! Appreciate your timely help bro! :) Definitely will accept the answer and upvote yours if it works good ! – Krish3090 Aug 25 '14 at 10:38
  • Have a small doubt..! Does all youtube videos have charset as UTF-8 ? – Krish3090 Aug 25 '14 at 10:41
  • Nothing to do with YouTube at all, URLs can ONLY be sent using ASCII, and all ASCII can be encoded in UTF8 so that's fine. UTF8 is basically (for the sake of 500 chars here) a super set of ASCII with more stuff in there, so you'll be well covered. [Here's more information for you](http://www.w3schools.com/tags/ref_urlencode.asp) Read the documentation! – RossC Aug 25 '14 at 10:44
  • Fantastic ! Will work out this right away when I reach home..Am at my workplace right now.. Will accept your answer and upvote once I test this on my app.. – Krish3090 Aug 25 '14 at 10:46
  • My bad! Not working bro... Its giving me errors saying that its not able to parse the encoded URL to download the video. – Krish3090 Aug 26 '14 at 06:41
  • But it worked with the other parsed one. This is just a text format issue now, I have no idea what you've written so I can't add any more. You said the parsed one worked last time. I'm at a loss. – RossC Aug 26 '14 at 12:32
  • Sorry dude.. Ill attach my code with the answer and update ! I will also attach the error log report. Or tell me if there is a general way to create a download url from normal youtube video link ? – Krish3090 Aug 26 '14 at 12:43