Program Resource

Resource libraries for programmers and developers

Youtube movie information can be fetched in xml format. Using this info, you can get title, duration, and other data you want. Below is sample to get title/duration and thumbnail bitmap image.

Code returns title and duration info in string.

public String Youtube_Fetchinfo(Context context,String url,int timeout){
   	String infourl="";
   	if (!url.toLowerCase().contains("youtube"))
   		return null;

	//get video ID
	Pattern p = Pattern.compile(".+v=([^&\"]*)");
	Matcher m = p.matcher(url);
	if (m.find()) {
		infourl = "";
		return null;

   	int responseCode = 0;
	int duration=0;
	String sduration="";
	String title="";
	String movietitle = new String();

	try {
		//Get video information
		String defaultUA = "Mozilla/5.0 (Linux; U; Android 2.1; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3";
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(infourl);
		HttpResponse httpResponse = null;

		httpClient.getParams().setParameter("http.connection.timeout", new Integer(timeout));
		HttpParams params1 = httpClient.getParams();
		HttpConnectionParams.setConnectionTimeout(params1, timeout);
		HttpConnectionParams.setSoTimeout(params1, timeout);
		params1.setParameter(HttpProtocolParams.USER_AGENT, defaultUA);
		httpResponse = httpClient.execute(httpGet);
		responseCode = httpResponse.getStatusLine().getStatusCode();

		if (responseCode == HttpStatus.SC_OK) {
			InputStream istream = httpResponse.getEntity().getContent();
			InputStreamReader reader = new InputStreamReader(istream);
			BufferedReader objBuf = new BufferedReader(reader);
			StringBuilder builder = new StringBuilder();
			String sLine;

			while((sLine = objBuf.readLine()) != null){
			String html = builder.toString();

			//find duration info
			Pattern dp = Pattern.compile("seconds='(\\d+?)'");
			Matcher dm = dp.matcher(html);
			if (dm.find()) {
				try {
						duration = Integer.parseInt(;
				}catch(Exception e){duration=0;}

			//find video title
			Pattern tp = Pattern.compile(&quot;<title.*?>(.*?)</&quot;);
			Matcher tm = tp.matcher(html);
			if (tm.find()) {
				title =;
				if (title.length()>0){
					if (duration > 60)
						movietitle = title+&quot; (&quot;+(duration/60)+&quot;min)&quot;;
						movietitle = title+&quot; (&quot;+duration+&quot;sec)&quot;;
					movietitle = movietitle.replace(&quot;&amp;&quot;,&quot;&&quot;);
					movietitle = movietitle.replace(&quot;&gt;&quot;,&quot;<&quot;);
					movietitle = movietitle.replace(&quot;&lt;&quot;,&quot;>&quot;);
					movietitle = movietitle.replace(&quot;&quot;&quot;,&quot;\&quot;&quot;);
					return movietitle;
		else if (responseCode == 403){ //forbidden
			movietitle = &quot;forbidden&quot;;
			return movietitle;
	} catch (ClientProtocolException e) {
	} catch (IOException e) {
	return null;

Code to get thumbnail image.

public Bitmap VGinfo_Fetchthum(Context context,String url){
	String vid=&quot;&quot;;
	String turl=&quot;&quot;;

	if (!url.toLowerCase().contains(&quot;youtube&quot;))
		return null;

	Pattern p = Pattern.compile(&quot;.+v=([^&\&quot;]*)&quot;);
	Matcher m = p.matcher(url);
	if (m.find()) {
		vid =;
		return null;
	if (vid.length()<2)
    	return null;

	try {
		URL imageUrl;
		imageUrl = new URL(&quot;;+vid+&quot;/1.jpg&quot;);

		InputStream imageIs;
		imageIs = imageUrl.openStream();

		//saving image to SD card (to be used for caching), change location or return data directly as needed
		File saveDir;
		String filepath;
		saveDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+&quot;/simpleweb/videothum/&quot;);
		filepath = Environment.getExternalStorageDirectory().getAbsoluteFile()+&quot;/simpleweb/videothum/&quot;+vid+&quot;.jpg&quot;;
		if(!saveDir.exists()) {
			if (!saveDir.mkdirs()) { //can't save to file, load to memory
				Bitmap image = BitmapFactory.decodeStream(imageIs);
				return image;

		File file = new File(filepath);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(imageIs, 10240);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false), 10240);

		byte buffer[] = new byte[10240];
		int size = 0;
		while(-1 != (size = {
			bufferedOutputStream.write(buffer, 0, size);

		Bitmap bmSource = BitmapFactory.decodeFile(filepath);
		return bmSource;
	} catch (MalformedURLException e) {
		return null;
	} catch (IOException e) {
		return null;
Print Friendly, PDF & Email

This post is also available in: Japanese

Leave a Reply

Your email address will not be published. Required fields are marked *