Delay when playing multiple videos at the same time!

edited December 2015 in Library Questions

Hello! In my project I have to play a string of videos randomly and at the same time, the problem is that very time a new video is added the others, which are already playing, freeze for a second! I´ve read that preloading them could fix this problem but i don´t now how to go about it and if it realy works?

I will live the code i´m working on here hope you can help me out!

// libs:

import processing.video.\*;

// declarations

int $winW = 1920; // window

int $winH = 1080;

int $margin = 20;

int $celsW = 20; // numero de  celulas horizontal

int $celsH = 15; // numero de  celulas vertical

int $celW, $celH;

int $fps = 25;

int $beat = 10; // tempo entre cada video!

//lista de videos

String[] $videoFiles = {

  "1.mov",

  "2.mov"

};

int $frame = 0;

int $vidMax;

Movie[] $videos;

/\*

 \* Main proggy

 \*/

//

// SETUP:

//

void setup() {

  size(1920, 1080);

  background(0);

  frameRate($fps);

  // defenir o tamanho de cada celula

  $celW =  $winW/$celsW;

  $celH =  $celW;

  /\* Init matrix \*/

  $videos = new Movie[$celsW\*$celsH];

  $vidMax = $videoFiles.length;

}

//

// DRAW:

//

void draw() {

  background(0);

  // show something new?

  if ($frame % $beat == 0) {

    int vid = floor(random(0, $vidMax));

    int pos = floor(random(0, $videos.length));

    while ($videos[pos] != null) {

      pos = floor(random(0, $videos.length));

    }

    $videos[pos] = new Movie(this, $videoFiles[vid]);

    $videos[pos].jump(0);

    $videos[pos].noLoop();

    $videos[pos].play();

    println("loading video " + vid + " at position " + pos);

  }

  // read videos:

  for (int i = 0; i < $videos.length; i++) {

    if ($videos[i] != null) {

      if ($videos[i].time() < $videos[i].duration() - 0.1) {

        $videos[i].read();

        int x = (i % $celsW) \* $celW;

        int y = (i / $celsW) \* ($celH+$celsH/3);

//         int y = (i / ($celsH+$celsH/2)) \* ($celH+$celsH/2);

        image($videos[i], x, y, $celW, $celH);

      } else {

        $videos[i] = null;

      }

    }

  }



  $frame++;
}

Answers

  • edited December 2015

    Thanks! i was trying to fix that, the text was terrible!!!
    I´ve taken a look to want you sent and it´s fixed now!

  • Hey GoToLoop! I was reading some of your post and i found one where you solve the problem of playing 2 videos at the same time! My teacher told me the solution would be for me to preload the videos, for what i´ve read it would have to be on the void setup. However I having some trouble finding a way to preload a video?

  • edited December 2015

    Hi Tiago, I still couldn't pinpoint exactly what you're asking?

    Are you playing all your loaded videos at once?
    Or just 1, then grab another random vid once finished?
    Or is it a mix of both? If so, exactly how?

    Indeed, we should strive to load all resources within setup() if it's feasible.
    And lastly: Why are you prefixing all your variables w/ $ anyways? ^#(^

  • I will try to explain what my code is doing but you can downlaod this zip file and have a look, that way you will understand better. Zipfile:http://we.tl/gZ71ihLEEF

  • edited December 2015

    What i´m trying to accomplish is to have a grid of squares where randomly a video is playing or not, they could be playing at the same time or just one. It´s like the windows of a building at night. The problem is that when a new video is added the others freeze for a second!!

    hope I explained it better!

  • edited December 2015

    Hey! In this discussion :https://forum.processing.org/two/discussion/comment/57830/#Comment_57830

    I manage to finaly find something that might be what i´m looking for

    var fingers;
     
    function preload() {
      fingers = createVideo(['assets/fingers.mov', 'assets/fingers.webm']);
    }
     
    function setup() {
      createCanvas(320, 240);
      ellipseMode(CENTER).fill(0).noStroke();
     
      fingers.loop();
      //fingers.hide();
    }
    

    GotoLoop do you think i can adapt this to preload around 70 videos? Or is there a better way!?

  • edited December 2015

    I've just looked at your "sketch_4.pde" sample and things don't look too easy! :-S
    Even though you're using only 2 small files: "1.mov" & "2.mov", you're still creating lotsa unique Movie instances outta them! @-)
    This type of approach makes unfeasible to instantiate everything within setup(). :-<

    It all depends whether it is allowed for you to use the same Movie instance across "cells".
    That is, whether you can play() the same Movie showing the same frame position for all "cells".

    Only then you can load everything within setup().
    Otherwise, you're gonna need to come up w/ some very complicated approach to create Movie instances on-the-fly after setup()! 8-|

  • edited December 2015

    Sorry GoToLoop, but as I´m a begginer at this, I don´t know what you mean by "movie instances"!? :( And what would it be the problem of using the same movie instance?

  • edited December 2015

    Instance of some class = object. :P

    If you use the same Movie instance, every cell displaying it gonna show the same video position.
    In other words, you won't be able to have 1 cell showing the beginning and another 1 showing some other part of the same Movie. All of them is gonna show the same thing at the same time! ~:>

  • edited December 2015

    -.- if I did what you are subjecting I wouldn't be able to have 1.mov and 2.mov playing at the same time in different cells! That wont work :( I wouldn't mind if 1.mov would had is own movie instance and if a cell was playing that video no other could do it!

    I´ve tried to export the video in several ways to see if the size or codec of the video was the problem but i didn't had any luck X(

    In you opinion what could be causing this lag on starting the videos? Since no video is added I can have 10 videos playing without any problem!

  • edited December 2015

    I wouldn't be able to have "1.mov" and "2.mov" playing at the same time in different cells!

    You misunderstood me. You can have as many of them playing in various cells at the same time.
    The only impediment is that they would show the same scene frame for all cells.

    In you opinion what could be causing this lag on starting the videos?

    Any loading and save operations cause lag!
    That's why they should happen within setup() before draw() starts.
    Loading & saving stuff once draw() had started w/o causing lag to it demands advanced techniques.

  • edited December 2015

    Ups! my bad #-o So if a video was already playing and another was added it would play the frame equivalent to the other which was already playing.

     Any loading and save operations cause lag! 

    Thanks for that!

  • edited December 2015

    I was wondering!! :-? As you said i have to load the videos on setup to avoid lag but having so many movie instances it´s a problem! (i believe you since i could not grasp why that happens :P ) so i thought, could we create like 5 movie instances width 14 videos for each!? i hope i´m going some where width this :P

  • edited December 2015 Answer ✓

    You'll load and create as many Movie objects as your total number of video files within setup().
    It's got nothing to do w/ your actual number of "cells".
    Then each cell's gonna choose which Movie to play() among those loaded within setup().
    That is, they won't create any more Movie objects after setup(). Just gonna re-use them.

  • edited December 2015

    ok! i think that is it! For me it would be perfect not to have multiple "cells" playing the same video! Sorry for taking so long to get it and THANK YOU! ^:)^ Now i will just have to try to do it :-S

  • edited December 2015

    Hello GoToLoop! I`ve been working on the code and the lag is resolved! But i still have the same video playing simultaneously, which causes some wierd freezes!

    What i was trying to do is to go through a array i created and check if theres is any video already playing !! if so the vid varibel would run again and so on...

    I found the code below on the net and i was trying to adapt it so that i could use it

    int[] test = { 1,2,3,5,7,4,23,64};
    for (int i=0; i<test.length; i++){
     if (test[i]==5){
       println("found 5 at index position: "+i);
     }
    }
    

    this is the code from my project

    /**
     * Boilerplate
     */
    
    // libs:
    import processing.video.*;
    
    // declarations
    int $winW = 1280;  // window
    int $winH = 720;
    int $celsW = 3;   // cells number
    int $celsH = 3;
    int $margin = 10;  // bottom margin
    
    int $celW, $celH;
    int $fps = 15;     // frames 
    int $beat = 10;    // time between images
    
    String[] $videoFiles = {      // list of videos 
      "1.mov",
      "2.mov",
      "3.mov",
      "4.mov"
    };
    
    int $frame = 0;
    int[] $matrix;
    int[] $val;
    int $vidMax;
    Movie[] $videos; 
    PImage img;
    
    
    /**
     * Main proggy
     */
    
    //
    // SETUP:
    //
    void setup() {
      size(1280, 720);
      background(0);
      frameRate($fps);
    
      $celW = $winW/$celsW;
      $celH = $celW;
      /* Init matrix */
    
      $matrix = new int[$celsW*$celsH]; // total numbre of cells
      for (int i = 0; i < $matrix.length; i++) {   // gives each cell of the array the value -1
        $matrix[i] = -1;
        int $val = $matrix[i];
      }
    
      /* Init videos */
      $videos = new Movie[$videoFiles.length]; 
      for (int i = 0; i < $videoFiles.length; i++) {
        $videos[i] = new Movie(this, $videoFiles[i]);
        $videos[i].jump(0); // always go to the start of the movie
        $videos[i].loop();  // movie when finish will stop
        $videos[i].play();   // action play
    
      }
    
      $vidMax = $videos.length ; // total number of videos 
    }
    
    
    //
    // DRAW:
    //
    void draw() {
    
      // show something new?
      if ($frame % $beat == 0) {
        if (isComplete() == false) {
          int vid = floor(random(0, $vidMax)); // choses a video 
          int pos = floor(random(0, $matrix.length)); // choses a position
    
            while ($matrix[pos] > -1) {  // asks if the value -1
              pos = floor(random(0, $matrix.length)); // while the result of the operation is not -1 it runs $pos again
            }
    
           $matrix[pos] = vid; // add video if pos == -1 
        }
      }
    
    
    
      // show the videos:
      for (int i = 0; i < $matrix.length; i++) {
          int vid = $matrix[i]; 
          if (vid != -1) { // if the value is different from -1
              if ($videos[vid].time() < $videos[vid].duration() - 0.1){
                  $videos[vid].read();
                  int x = (i % $celsW) * $celW;
                  int y = (i / $celsW) * ($celH + $margin);
                  image($videos[vid], x, y, $celW, $celH);
                  } else {   
                    $matrix[i]= -1;
                    int x = (i % $celsW) * $celW;
                    int y = (i / $celsW) * ($celH + $margin);
                    fill(0, 0, 0);
                    rect(x, y, $celW, $celH);
              }
          }
       }
    
      $frame++;
    }
    
    
    
    
    //
    // MATRIZ COMPLETA?
    //
    boolean isComplete() {
      boolean c = true;
      for (int i = 0; i < $matrix.length; i++) {
        if ($matrix[i] == -1) {
          c = false;
          break;
        }
      }
      return c;
    }
    

    is this the best way?!

  • Problem solved!! I will leave here the code for anyone o might have the same problem!

    /* * Boilerplate */

    // libs:
    import processing.video.*;
    
    // declarations
    int $winW = 1280;  // window
    int $winH = 720;
    int $celsW = 5;   // cells number
    int $celsH = 3;
    int $margin = 10;  // bottom margin
    
    int $celW, $celH;
    int $fps = 15;     // frames 
    int $beat = 60;    // time between images
    
    String[] $videoFiles = {      // list of videos 
      "1.mov",
      "2.mov",
      "3.mov",
      "4.mov",
      "5.mov",
      "6.mov",
      "7.mov",
      "8.mov",
      "9.mov",
      "10.mov",
      "11.mov",
      "13.mov",
      "14.mov",
      "15.mov"
    };
    
    int $frame = 0;
    int[] $matrix;
    int $vidMax;
    Movie[] $videos; 
    PImage img;
    
    
    /*
     * Main proggy
     */
    
    //
    // SETUP:
    //
    void setup() {
      size(1280, 720);
      background(0);
      frameRate($fps);
    
      $celW = $winW/$celsW;
      $celH = $celW;
      /* Init matrix */
    
      $matrix = new int[$celsW*$celsH]; // total numbre of cells
      for (int i = 0; i < $matrix.length; i++) {   // gives each cell of the array the value -1
        $matrix[i] = -1;
    
      }
    
      /* Init videos */
      $videos = new Movie[$videoFiles.length]; 
      for (int i = 0; i < $videoFiles.length; i++) {
        $videos[i] = new Movie(this, $videoFiles[i]);
        $videos[i].jump(0); // always go to the start of the movie
        $videos[i].loop();  // movie when finish will stop
        $videos[i].play();   // action play
    
      }
    
      $vidMax = $videos.length ; // total number of videos 
    }
    
    boolean allVideosInUse()
    {
        int noVideosMatrix = 0;
         for (int i = 0; i < $matrix.length; i++) {
            if($matrix[i] != -1)
            noVideosMatrix++;
            }
    
        if(noVideosMatrix >= $vidMax)
        return true;
        else return false;
    }
    
    boolean existsInMatrix(int valor)
    {
         for (int i = 0; i < $matrix.length; i++) {
            if($matrix[i] == valor)
            return true;
            }
    return false;
    }
    
    void draw() {
    
       // show something new?
      if ($frame % $beat == 0) {
        if (isComplete() == false && !allVideosInUse()) {
          int vid = floor(random(0, $vidMax)); // choses a video
            while(existsInMatrix(vid)) // If video exists in matrix, chooses another
            {
                vid = floor(random(0, $vidMax)); //chooses another video
            }
          int pos = floor(random(0, $matrix.length)); // choses a position
    
            while ($matrix[pos] > -1) {  // asks if the value -1
              pos = floor(random(0, $matrix.length)); // while the result of the operation is not -1 it runs $pos again
            }
    
           $matrix[pos] = vid; // add video if pos == -1
        }
      }
    
      // show the videos:
      for (int i = 0; i < $matrix.length; i++) {
          int vid = $matrix[i]; 
          if (vid != -1) { // if the value is different from -1
              if ($videos[vid].time() < $videos[vid].duration() - 0.1){
                  $videos[vid].read();
                  int x = (i % $celsW) * $celW;
                  int y = (i / $celsW) * ($celH + $margin);
                  image($videos[vid], x, y, $celW, $celH);
                  } else {   
                    $matrix[i]= -1;
                    int x = (i % $celsW) * $celW;
                    int y = (i / $celsW) * ($celH + $margin);
                    fill(0, 0, 0);
                    rect(x, y, $celW, $celH);
              }
          }
       }
    
      $frame++;
    }
    
    
    
    
    //
    // MATRIZ COMPLETA?
    //
    boolean isComplete() {
      boolean c = true;
      for (int i = 0; i < $matrix.length; i++) {
        if ($matrix[i] == -1) {
          c = false;
          break;
        }
      }
      return c;
    }
    
Sign In or Register to comment.