001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.tools.imagecollection.collection.video; 031 032import java.io.IOException; 033import java.net.MalformedURLException; 034import java.net.URL; 035import java.text.ParseException; 036import java.util.List; 037 038import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException; 039import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig; 040import org.openimaj.video.xuggle.XuggleVideo; 041 042import com.google.gdata.client.youtube.YouTubeService; 043import com.google.gdata.data.youtube.VideoEntry; 044import com.google.gdata.util.ServiceException; 045import com.xuggle.utils.collections.KeyValuePair; 046import com.xuggle.utils.net.URLParams; 047import com.xuggle.utils.net.YouTube; 048 049public class YouTubeVideoImageCollection extends XuggleVideoImageCollection.FromURL{ 050 String developerKey = "AI39si4l2-2ZI94omuJk1U9mk5QvBFoPXbZ0Jsb5LnEtosQDSEOMR0DD5gBjlOG4kmUZ17r6cI-WBejYWvBk7oNm9U409KJjEA"; 051 String gDataURLTemplate = "http://gdata.youtube.com/feeds/api/videos/%s"; 052 private VideoEntry entry; 053 public YouTubeVideoImageCollection() { 054 } 055 public YouTubeVideoImageCollection(String youtubeURLStr) throws ImageCollectionSetupException { 056 String youtubeJSON = String.format("{video:{url:\"%s\"}}",youtubeURLStr); 057 ImageCollectionConfig youtubeConfig = new ImageCollectionConfig(youtubeJSON); 058 this.setup(youtubeConfig); 059 } 060 061 @Override 062 protected XuggleVideo loadXuggleVideo(String videoEntry) { 063 String youtubeId = parseYoutubeID(videoEntry); 064 065 if(youtubeId == null) return null; 066 String youtubeFLV = YouTube.getLocation(youtubeId); 067 068 YouTubeService service = new YouTubeService("thisinthat",developerKey); 069 URL gDataURL; 070 try { 071 gDataURL = new URL(String.format(gDataURLTemplate, youtubeId)); 072 this.entry = service.getEntry(gDataURL, VideoEntry.class); 073 074 } catch (MalformedURLException e) { 075 // TODO Auto-generated catch block 076 e.printStackTrace(); 077 } catch (IOException e) { 078 // TODO Auto-generated catch block 079 e.printStackTrace(); 080 } catch (ServiceException e) { 081 // TODO Auto-generated catch block 082 e.printStackTrace(); 083 } 084 085 086 return new XuggleVideo(youtubeFLV); 087 } 088 089 @Override 090 public int countImages(){ 091 return (int) (this.entry.getMediaGroup().getDuration() * this.video.getFPS()); 092 } 093 094 @Override 095 public int useable(ImageCollectionConfig config){ 096 if(super.useable(config) < 0) return -1; 097 String youtubeID = parseYoutubeID(config); 098 if(youtubeID == null) return -1; 099 return 1000; 100 } 101 102 private String parseYoutubeID(ImageCollectionConfig config) { 103 String urlStr; 104 try { 105 urlStr = config.read(this.videoTag()); 106 } catch (ParseException e1) { 107 return null; 108 } 109 110 return parseYoutubeID(urlStr); 111 112 } 113 114 private String parseYoutubeID(String urlStr) { 115// http://www.youtube.com/watch?v=X4fRYSeIpIQ 116 URL u; 117 try { 118 u = new URL(urlStr); 119 } catch (MalformedURLException e) { 120 return null; 121 } 122 if(!u.getHost().matches(".*[.]youtube.com$")) return null; 123 124 List<KeyValuePair> l = URLParams.parseQueryString(u.getQuery()); 125 String foundID = null; 126 for (KeyValuePair pair : l) { 127 if(pair.getKey().equals("v")){ 128 foundID = pair.getValue(); 129 break; 130 } 131 } 132 return foundID; 133 } 134}