{"id":2271,"date":"2013-05-22T23:08:26","date_gmt":"2013-05-22T14:12:20","guid":{"rendered":"https:\/\/programresource.net\/?p=2271"},"modified":"2013-05-22T23:12:20","modified_gmt":"2013-05-22T14:12:20","slug":"get-title-duration-and-thumbnail-image-from-youtube-url","status":"publish","type":"post","link":"https:\/\/programresource.net\/en\/2013\/05\/22\/2271.html","title":{"rendered":"Get Title, duration, and thumbnail image from Youtube URL"},"content":{"rendered":"<p>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.<\/p>\n<p>Code returns title and duration info in string.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public String Youtube_Fetchinfo(Context context,String url,int timeout){\r\n   \tString infourl=&amp;quot;&amp;quot;;\r\n   \tif (!url.toLowerCase().contains(&amp;quot;youtube&amp;quot;))\r\n   \t\treturn null;\r\n\r\n\t\/\/get video ID\r\n\tPattern p = Pattern.compile(&amp;quot;.+v=([^&amp;\\&amp;quot;]*)&amp;quot;);\r\n\tMatcher m = p.matcher(url);\r\n\tif (m.find()) {\r\n\t\tinfourl = &amp;quot;http:\/\/gdata.youtube.com\/feeds\/api\/videos\/&amp;quot;+m.group(1);\r\n\t}else\r\n\t\treturn null;\r\n\t}\r\n\r\n   \tint responseCode = 0;\r\n\tint duration=0;\r\n\tString sduration=&amp;quot;&amp;quot;;\r\n\tString title=&amp;quot;&amp;quot;;\r\n\tString movietitle = new String();\r\n\r\n\ttry {\r\n\t\t\/\/Get video information\r\n\t\tString defaultUA = &amp;quot;Mozilla\/5.0 (Linux; U; Android 2.1; en-us) AppleWebKit\/522+ (KHTML, like Gecko) Safari\/419.3&amp;quot;;\r\n\t\tHttpClient httpClient = new DefaultHttpClient();\r\n\t\tHttpGet httpGet = new HttpGet(infourl);\r\n\t\tHttpResponse httpResponse = null;\r\n\r\n\t\thttpClient.getParams().setParameter(&amp;quot;http.connection.timeout&amp;quot;, new Integer(timeout));\r\n\t\tHttpParams params1 = httpClient.getParams();\r\n\t\tHttpConnectionParams.setConnectionTimeout(params1, timeout);\r\n\t\tHttpConnectionParams.setSoTimeout(params1, timeout);\r\n\t\tparams1.setParameter(HttpProtocolParams.USER_AGENT, defaultUA);\r\n\t\thttpResponse = httpClient.execute(httpGet);\r\n\t\tresponseCode = httpResponse.getStatusLine().getStatusCode();\r\n\r\n\t\tif (responseCode == HttpStatus.SC_OK) {\r\n\t\t\tInputStream istream = httpResponse.getEntity().getContent();\r\n\t\t\tInputStreamReader reader = new InputStreamReader(istream);\r\n\t\t\tBufferedReader objBuf = new BufferedReader(reader);\r\n\t\t\tStringBuilder builder = new StringBuilder();\r\n\t\t\tString sLine;\r\n\r\n\t\t\twhile((sLine = objBuf.readLine()) != null){\r\n\t\t\t\tbuilder.append(sLine);}\r\n\t\t\tString html = builder.toString();\r\n\t\t\tistream.close();\r\n\r\n\t\t\t\/\/find duration info\r\n\t\t\tPattern dp = Pattern.compile(&amp;quot;seconds='(\\\\d+?)'&amp;quot;);\r\n\t\t\tMatcher dm = dp.matcher(html);\r\n\t\t\tif (dm.find()) {\r\n\t\t\t\ttry {\r\n\t\t\t\t\t\tduration = Integer.parseInt(dm.group(1));\r\n\t\t\t\t}catch(Exception e){duration=0;}\r\n\t\t\t}\r\n\r\n\t\t\t\/\/find video title\r\n\t\t\tPattern tp = Pattern.compile(&amp;quot;&lt;title.*?&gt;(.*?)&lt;\/&amp;quot;);\r\n\t\t\tMatcher tm = tp.matcher(html);\r\n\t\t\tif (tm.find()) {\r\n\t\t\t\ttitle = tm.group(1);\r\n\t\t\t\tif (title.length()&gt;0){\r\n\t\t\t\t\tif (duration &gt; 60)\r\n\t\t\t\t\t\tmovietitle = title+&amp;quot; (&amp;quot;+(duration\/60)+&amp;quot;min)&amp;quot;;\r\n\t\t\t\t\telse\r\n\t\t\t\t\t\tmovietitle = title+&amp;quot; (&amp;quot;+duration+&amp;quot;sec)&amp;quot;;\r\n\t\t\t\t\tmovietitle = movietitle.replace(&amp;quot;&amp;amp;&amp;quot;,&amp;quot;&amp;&amp;quot;);\r\n\t\t\t\t\tmovietitle = movietitle.replace(&amp;quot;&amp;gt;&amp;quot;,&amp;quot;&lt;&amp;quot;);\r\n\t\t\t\t\tmovietitle = movietitle.replace(&amp;quot;&amp;lt;&amp;quot;,&amp;quot;&gt;&amp;quot;);\r\n\t\t\t\t\tmovietitle = movietitle.replace(&amp;quot;&amp;quot;&amp;quot;,&amp;quot;\\&amp;quot;&amp;quot;);\r\n\t\t\t\t\treturn movietitle;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\telse if (responseCode == 403){ \/\/forbidden\r\n\t\t\tmovietitle = &amp;quot;forbidden&amp;quot;;\r\n\t\t\treturn movietitle;\r\n\t\t}\r\n\t} catch (ClientProtocolException e) {\r\n\t} catch (IOException e) {\r\n\treturn null;\r\n}<\/pre>\n<p>Code to get thumbnail image.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public Bitmap VGinfo_Fetchthum(Context context,String url){\r\n\tString vid=&amp;quot;&amp;quot;;\r\n\tString turl=&amp;quot;&amp;quot;;\r\n\r\n\tif (!url.toLowerCase().contains(&amp;quot;youtube&amp;quot;))\r\n\t\treturn null;\r\n\r\n\tPattern p = Pattern.compile(&amp;quot;.+v=([^&amp;\\&amp;quot;]*)&amp;quot;);\r\n\tMatcher m = p.matcher(url);\r\n\tif (m.find()) {\r\n\t\tvid = m.group(1);\r\n\t}else\r\n\t\treturn null;\r\n\tif (vid.length()&lt;2)\r\n    \treturn null;\r\n\r\n\ttry {\r\n\t\tURL imageUrl;\r\n\t\timageUrl = new URL(&amp;quot;http:\/\/i.ytimg.com\/vi\/&amp;quot;+vid+&amp;quot;\/1.jpg&amp;quot;);\r\n\r\n\t\tInputStream imageIs;\r\n\t\timageIs = imageUrl.openStream();\r\n\r\n\t\t\/\/saving image to SD card (to be used for caching), change location or return data directly as needed\r\n\t\tFile saveDir;\r\n\t\tString filepath;\r\n\t\tsaveDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+&amp;quot;\/simpleweb\/videothum\/&amp;quot;);\r\n\t\tfilepath = Environment.getExternalStorageDirectory().getAbsoluteFile()+&amp;quot;\/simpleweb\/videothum\/&amp;quot;+vid+&amp;quot;.jpg&amp;quot;;\r\n\t\tif(!saveDir.exists()) {\r\n\t\t\tif (!saveDir.mkdirs()) { \/\/can't save to file, load to memory\r\n\t\t\t\tBitmap image = BitmapFactory.decodeStream(imageIs);\r\n\t\t\t\treturn image;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tFile file = new File(filepath);\r\n\t\tBufferedInputStream bufferedInputStream = new BufferedInputStream(imageIs, 10240);\r\n\t\tBufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, false), 10240);\r\n\r\n\t\tbyte buffer[] = new byte[10240];\r\n\t\tint size = 0;\r\n\t\twhile(-1 != (size = bufferedInputStream.read(buffer))) {\r\n\t\t\tbufferedOutputStream.write(buffer, 0, size);\r\n\t\t}\r\n\t\tbufferedOutputStream.flush();\r\n\t\tbufferedOutputStream.close();\r\n\t\tbufferedInputStream.close();\r\n\r\n\t\tBitmap bmSource = BitmapFactory.decodeFile(filepath);\r\n\t\treturn bmSource;\r\n\t} catch (MalformedURLException e) {\r\n\t\treturn null;\r\n\t} catch (IOException e) {\r\n\t\treturn null;\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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. Code to get thumbnail image.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[330],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3pJyQ-AD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2271"}],"collection":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/comments?post=2271"}],"version-history":[{"count":1,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2271\/revisions"}],"predecessor-version":[{"id":2272,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/2271\/revisions\/2272"}],"wp:attachment":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media?parent=2271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/categories?post=2271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/tags?post=2271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}