View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.tools.imagecollection.collection.video;
31  
32  import java.io.IOException;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  import java.text.ParseException;
36  import java.util.List;
37  
38  import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException;
39  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
40  import org.openimaj.video.xuggle.XuggleVideo;
41  
42  import com.google.gdata.client.youtube.YouTubeService;
43  import com.google.gdata.data.youtube.VideoEntry;
44  import com.google.gdata.util.ServiceException;
45  import com.xuggle.utils.collections.KeyValuePair;
46  import com.xuggle.utils.net.URLParams;
47  import com.xuggle.utils.net.YouTube;
48  
49  public class YouTubeVideoImageCollection extends XuggleVideoImageCollection.FromURL{
50  	String developerKey = "AI39si4l2-2ZI94omuJk1U9mk5QvBFoPXbZ0Jsb5LnEtosQDSEOMR0DD5gBjlOG4kmUZ17r6cI-WBejYWvBk7oNm9U409KJjEA";
51  	String gDataURLTemplate = "http://gdata.youtube.com/feeds/api/videos/%s";
52  	private VideoEntry entry;
53  	public YouTubeVideoImageCollection() {
54  	}
55  	public YouTubeVideoImageCollection(String youtubeURLStr) throws ImageCollectionSetupException {
56  		String youtubeJSON = String.format("{video:{url:\"%s\"}}",youtubeURLStr);
57  		ImageCollectionConfig youtubeConfig = new ImageCollectionConfig(youtubeJSON);
58  		this.setup(youtubeConfig);
59  	}
60  
61  	@Override
62  	protected XuggleVideo loadXuggleVideo(String videoEntry) {
63  		String youtubeId = parseYoutubeID(videoEntry);
64  		
65  		if(youtubeId == null) return null;
66  		String youtubeFLV = YouTube.getLocation(youtubeId);
67  		
68  		YouTubeService service = new YouTubeService("thisinthat",developerKey);
69  		URL gDataURL;
70  		try {
71  			gDataURL = new URL(String.format(gDataURLTemplate, youtubeId));
72  			this.entry = service.getEntry(gDataURL, VideoEntry.class);
73  			
74  		} catch (MalformedURLException e) {
75  			// TODO Auto-generated catch block
76  			e.printStackTrace();
77  		} catch (IOException e) {
78  			// TODO Auto-generated catch block
79  			e.printStackTrace();
80  		} catch (ServiceException e) {
81  			// TODO Auto-generated catch block
82  			e.printStackTrace();
83  		}
84  		
85  		
86  		return new XuggleVideo(youtubeFLV);
87  	}
88  	
89  	@Override
90  	public int countImages(){
91  		return (int) (this.entry.getMediaGroup().getDuration() * this.video.getFPS());
92  	}
93  	
94  	@Override 
95  	public int useable(ImageCollectionConfig config){
96  		if(super.useable(config) < 0) return -1;
97  		String youtubeID = parseYoutubeID(config);
98  		if(youtubeID == null) return -1;
99  		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 }