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.File;
033import java.net.MalformedURLException;
034import java.net.URL;
035import java.text.ParseException;
036import java.util.ArrayList;
037import java.util.Iterator;
038import java.util.List;
039
040import org.openimaj.image.MBFImage;
041import org.openimaj.tools.imagecollection.collection.ImageCollection;
042import org.openimaj.tools.imagecollection.collection.ImageCollectionEntry;
043import org.openimaj.tools.imagecollection.collection.ImageCollectionEntrySelection;
044import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException;
045import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
046import org.openimaj.tools.imagecollection.collection.video.selection.XuggleVideoFrameSelection;
047import org.openimaj.video.xuggle.XuggleVideo;
048
049public abstract class XuggleVideoImageCollection implements ImageCollection<MBFImage>{
050        public XuggleVideo video;
051        private XuggleVideoFrameSelection frameStyle;
052
053        @Override
054        public Iterator<ImageCollectionEntry<MBFImage>> iterator() {
055                frameStyle.init(video);
056                return new MetadataVideoIterator<MBFImage>(frameStyle,video);
057        }
058        
059        @Override
060        public void setEntrySelection(ImageCollectionEntrySelection<MBFImage> selection){
061                if(selection instanceof XuggleVideoFrameSelection)
062                        frameStyle = (XuggleVideoFrameSelection) selection;
063                else
064                        frameStyle = new XuggleVideoFrameSelection.Proxy(selection);
065        }
066
067        @Override
068        public void setup(ImageCollectionConfig config) throws ImageCollectionSetupException {
069                if(useable(config) < 0) throw new ImageCollectionSetupException("Can't use config as collection");
070                String videoEntry;
071                try {
072                        videoEntry = config.read(videoTag());
073                } catch (ParseException e) {
074                        throw new ImageCollectionSetupException(e);
075                }
076                this.video = loadXuggleVideo(videoEntry);
077                if(this.video == null){
078                        throw new ImageCollectionSetupException("Failed to load youtube video");
079                }
080                
081                if(!config.containsValid("video.framestyle")){
082                        frameStyle = new XuggleVideoFrameSelection.All(config);
083                }
084                else{
085                        String name;
086                        try {
087                                name = config.read("video.framestyle");
088                                frameStyle = XuggleVideoFrameSelection.byName(name,config);
089                        } catch (ParseException e) {
090                                throw new ImageCollectionSetupException("Failed to set framestyle");
091                        }
092                }
093        }
094        
095        @Override
096        public int countImages(){
097                return (int) this.video.countFrames();
098        }
099
100        protected abstract XuggleVideo loadXuggleVideo(String object);
101        protected abstract String videoTag();
102
103        @Override 
104        public int useable(ImageCollectionConfig config){
105                if(config.containsValid(videoTag()))
106                        return 0;
107                else return -1;
108        }
109
110
111        @Override
112        public List<ImageCollectionEntry<MBFImage>> getAll() {
113                List<ImageCollectionEntry<MBFImage>> allFrames = new ArrayList<ImageCollectionEntry<MBFImage>>();
114                for(ImageCollectionEntry<MBFImage> image : this){
115                        allFrames.add(image);
116                }
117                return allFrames;
118        }
119        
120        public static class FromFile extends XuggleVideoImageCollection{
121                @Override
122                protected XuggleVideo loadXuggleVideo(String videoEntry) {
123                        File videoFile = new File(videoEntry);
124                        return new XuggleVideo(videoFile);
125                }
126
127                @Override
128                protected String videoTag() {
129                        return "video.file";
130                }
131
132                @Override
133                public int useable(String rawInput) {
134                        File f = new File(rawInput);
135                        if(f.exists()) return 0;
136                        else return -1;
137                }
138
139                @Override
140                public ImageCollectionConfig defaultConfig(String rawInput) {
141                        return new ImageCollectionConfig(
142                                        String.format("{video: {file: %s}}",rawInput)
143                        );
144                }
145                
146                
147
148                
149        }
150        
151        public static class FromURL extends XuggleVideoImageCollection{
152                @Override
153                protected XuggleVideo loadXuggleVideo(String videoEntry) {
154                        return new XuggleVideo(videoEntry);
155                }
156
157                @Override
158                protected String videoTag() {
159                        return "video.url";
160                }
161                
162                @Override
163                public int useable(String rawInput) {
164                        try {
165                                new URL(rawInput);
166                                return 0;
167                        } catch (MalformedURLException e) {
168                                return -1;
169                        }
170                }
171
172                @Override
173                public ImageCollectionConfig defaultConfig(String rawInput) {
174                        return new ImageCollectionConfig(
175                                String.format("{video: {url: %s}}",rawInput)
176                        );
177                }
178        }
179}