how to make a movie array?

edited April 2015 in How To...

like this you can play back movie, how to make array with video files and use those with movie library? so i can use

image(myMovie[1], 0, 0);

image(myMovie[2], 0, 0);

etc.

import processing.video.*;
Movie myMovie;

void setup() {
  size(200, 200);
  background(0);
  myMovie = new Movie(this, "totoro.mov");
  myMovie.loop();
}

void draw() {
  image(myMovie, 0, 0);
}

// Called every time a new frame is available to read
void movieEvent(Movie m) {
  m.read();
}

Answers

  • edited June 2014 Answer ✓
    Movie [] movee = new Movie[2];
    
    void setup(){
    movee[0]= new Movie(this,"totoro.mov")
    movee[1]= new Movie(this,"totoro.mov")
    movee[0].loop();
    movee[1].loop();
    }
    
    void draw() {
      image(movee[0], 0, 0);
    
    image(movee[1],255,0);
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void movieEvent(Movie [] ma) {
    for (int i=0;i<ma.length;i++){
      ma[i].read();
    }
    }
    
  • edited June 2014

    thanks, but also how do i load movie file paths into array so i don't need to specify them for each movee instance?

    this is how i would do this for image files:

    PImage[] p = new PImage[2];
    for (int i = 0; i < p.length; i++) {
        p[i] = loadImage("v"+nf(i, 2) + ".jpg");  
      }
    

    but what about movie files? String[] PATH= new String[2]; there is no loadPath to use with for loop etc.

    Also why you use this? code seems to work also when this is commented out

        void movieEvent(Movie [] ma) {
        for (int i=0;i<ma.length;i++){
          ma[i].read();
        }
        }
    
  • edited July 2014

    Let's build on owaun's code:

    String[] movieNames = { "One.mov", "Two.mp4" };
    Movie[] movee = new Movie[movieNames.length];
    
    void setup() 
    {
      for (int i = 0; i < movieNames.length; i++)
      {
        movee[i] = new Movie(this, movieNames[i]);
        movee[i].loop();
      }
    }
    
    void draw() 
    {
      for (int i = 0; i < movieNames.length; i++)
      {
        image(movee[i], 255 * i, 0);
      }
    }
    
    void movieEvent(Movie m) 
    {
      m.read();
    }
    

    I think the second movieEvent function was a mistake...
    My code is no more tested than owaun's one. :-)

  • thank you for answer, but how to instead of explicitly writing every file String[] movieNames = { "One.mov", "Two.mp4" }; show path to folder with video files and load all those with the for loop?

  • edited July 2014

    Take a look at my example in your other thread below:
    http://forum.processing.org/two/discussion/6115/how-to-load-images-ignoring-file-names-and-extensions

    Just replace: ".png", ".jpg", ".jpeg", ".gif" w/ analogous video extensions! *-:)

    ... show path to folder with video files...

    If you meant a pop-up folder selector, you gotta use selectFolder():
    http://processing.org/reference/selectFolder_.html

    My complete app "Display by Last Modified" uses that too! Check it out: :-c
    http://forum.processing.org/one/topic/listing-last-10-modified-files-in-directory

    /** 
     * Display by Last Modified (v4.53)
     * by Kelvin Mead (2013/Aug)
     * mod GoToLoop
     * 
     * forum.processing.org/one/topic/listing-last-10-modified-files-in-directory
     * forum.processing.org/two/discussion/2578/how-to-filter-and-order-a-set-of-files
     * forum.processing.org/two/discussion/6004/how-to-make-a-movie-array
     */
    
    ////////////////////////////////////////////////////////////////////////////////////
    import java.io.FilenameFilter;
    
    static final FilenameFilter pictFilter = new FilenameFilter() {
      final String[] EXTS = {
        ".png", ".jpg", ".jpeg", ".gif"
      };
    
      @ Override boolean accept(final File dir, String name) {
        name = name.toLowerCase();
        for (final String ext: EXTS)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    import java.util.Arrays;
    import java.util.Comparator;
    
    static final Comparator modifiedComparator = new Comparator<File>() {
      @ Override int compare(final File f1, final File f2) {
        //return Long.signum(modifiedCache.get(f2) - modifiedCache.get(f1)); //safe!
        //return 1 | (int) (modifiedCache.get(f2) - modifiedCache.get(f1) >> 63); //risky!
        return int(modifiedCache.get(f2) - modifiedCache.get(f1)); //balanced!
      }
    };
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    import java.util.Map;
    import java.util.WeakHashMap;
    
    static final Map<File, Long> modifiedCache = new WeakHashMap(0200);
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    import java.util.concurrent.atomic.AtomicBoolean;
    
    static final AtomicBoolean isBusy = new AtomicBoolean();
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    static final int NUM = 10, SLEEP = 1 * 60*1000;
    static final PImage[] picts = new PImage[NUM];
    static final String[] paths = new String[NUM];
    
    static final PImage BOGUS_IMAGE = new PImage();
    
    static final float FPS = .5;
    static final color BG  = 0;
    
    static File currDir;
    static int  currLen, cw, ch;
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    void setup() {
      size(1200, 750);
      frameRate(FPS);
      noSmooth();
      imageMode(CENTER);
    
      cw = width>>1;
      ch = height>>1;
    
      picts[0] = BOGUS_IMAGE;
      paths[0] = "NONE SELECTED";
    
      thread("currentFolderSentinel");
    }
    
    void draw() {
      PImage pic = picts[frameCount %= NUM];
      if (pic == null)  pic = picts[frameCount = 0];
    
      background(BG);
      image(pic, cw, ch);
    
      frame.setTitle("#" + (frameCount + 1) + ":  " + paths[frameCount]
        + "\t\tBuffer: " + modifiedCache.size());
    }
    
    void keyPressed() {
      if (key == ENTER | key == RETURN) {
        currLen = 0;
    
        recheckCurrentFolder();
        //thread("recheckCurrentFolder");
    
        frameCount = NUM - 1;
      }
    
      else if (key != CODED && !(looping ^= true))
        frame.setTitle(frame.getTitle() + "\t PAUSED!");
    }
    
    void mouseClicked() {
      selectFolder("Select a folder to process:", "folderSelected");
    }
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    void currentFolderSentinel() {
      for (;;delay(SLEEP))  recheckCurrentFolder();
    }
    
    void recheckCurrentFolder() {
      if (currDir == null)  return;
    
      println("\nRe-checking \"" + currDir + "\" for new files.");
    
      final File[] filteredFiles = getFolderContent(currDir);
      final int len = filteredFiles.length;
    
      if (len != currLen)  println("# of files changed from "
        + currLen + " to " + len + "." + "\nUpdating files now...\t "
        + (sortByModified(filteredFiles)? "Done!" : "Failed!"));
    
      else println("Nothing has changed.");
    }
    
    protected static final File[] getFolderContent(final File dir) {
      return dir.listFiles(pictFilter);
    }
    
    void folderSelected(final File folder) {
      if (folder == null)
        println("\nWindow was closed or the user hit cancel.");
    
      else if (!folder.isDirectory())
        println("\n\"" + folder + "\" is an invalid folder.");
    
      else {
        final File[] filteredFiles = getFolderContent(folder);
    
        println("\n\"" + folder + "\" selected & "
          + filteredFiles.length + " image(s) found.");
    
        for (int i = 0; i != 5; ++i, delay(500))
          if (sortByModified(filteredFiles))  return;
    
        println("Attempts to call sortByModified() have all timed-out!!!");
      }
    }
    ////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////////////////////////////////////////
    protected boolean sortByModified(final File[] files) {
      if (files == null || files.length == 0) {
        println("No valid picture formats found here.");
        return true;
      }
    
      if (!isBusy.compareAndSet(false, true)) {
        println("Function sortByModified() is busy!");
        return false;
      }
    
      currDir = files[0].getParentFile();
      currLen = files.length;
    
      fillModifiedCache(files);
      Arrays.sort(files, modifiedComparator);
      loadImageFilePaths(files);
    
      isBusy.lazySet(false);
      return true;
    }
    
    protected static final void fillModifiedCache(final File[] archives) {
      //modifiedCache.clear(); //guaranteed!
      //System.gc(); //optimized!
      //; none!
    
      for (final File f: archives)  modifiedCache.put(f, f.lastModified());
    }
    
    protected void loadImageFilePaths(final File[] archives) {
      final int num = min(NUM, archives.length);
    
      //Arrays.fill(picts, null);
      if (num < NUM)  picts[num] = null;
    
      PImage pic;
      for (int i = num; i-- != 0; picts[i] =
        ( pic = loadImage(paths[i] = archives[i].getPath()) ) != null?
        pic : BOGUS_IMAGE);
    
      frameCount = NUM;
    }
    ////////////////////////////////////////////////////////////////////////////////////
    
Sign In or Register to comment.