1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
76 e.printStackTrace();
77 } catch (IOException e) {
78
79 e.printStackTrace();
80 } catch (ServiceException e) {
81
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
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 }