Triggering multiple videos / stuttering / poor performance

edited January 2017 in Library Questions

Bonjour lovely knowledgeable people,

I have a little problem with playing back videos in processing. The aim is to trigger the videos via an serial port input which works fine. The problem is that the videos are played back very laggy. I assume its because they are somehow all played back in the background. The videos are very short 2-5 seconds, but are in HD (1920x1080) and .H264 coded. They are supposed to be played back as long as the input signal is there, and fade to black when its gone. Can you please help me figure out how to avoid this problem and achieve fluid playback of the videos? That would be superb! I Have been hammering over this code for a while and I cant figure it out. Annnd, do you have any idea how to implement fading in and fading out of the videos? Tried it with tint, but can not manage it to do it fast enough. You would be a life saver! Merci, merci bien — any help is appreciated!

Love ❤️ jules

import processing.video.*;
import processing.serial.*;

Movie[]  movie = new Movie[10]; 
float[] myArray = new float[10];
short videoSpeed= 0 ; 
PImage bg;
PImage logo;
short videoID = 9; 

String[] video ={ "modellblinker1.mp4", 
          "modellblinker2.mp4",
          "modellhood.mp4",
          "modellside2.mp4",
          "modellside1.mp4",
          "modellwindshieldsback.mp4",
          "modellTrunk.mp4",
          "modellwindshieldsback.mp4",
          "modellroof.mp4"};



void setup() {
  fullScreen();
  iniMovies();
  imageMode(CENTER);
  frameRate(25);
  bg = loadImage("1.jpg");
  logo = loadImage("m.png");
  println(Serial.list()[0]);
  myPort = new Serial(this, Serial.list()[0], 19200);
  myPort.bufferUntil('\n');


}
void draw() {
  background(0);
  iniMovies();
  mngMovie();

 if( videoID ==9 ){


    image(logo,-57,-57);//, displayWidth/2, displayHeight/2);

  }else{
    image(movie[videoID], displayWidth / 2, displayHeight / 2);
    if (videoID != 9)
       movie[videoID].loop();
  }
}
void movieEvent(Movie m) {
  m.read();
}
void iniMovies(){
   for (int i = 0 ; i< 9;i++){
       movie[i] =  new Movie(this,video[i]);
   }
}  
void mngMovie(){
 // print(myArray);

 if(  myArray[0] >2000.0 ) {  /// blinker link 
   if ( videoID != 0  && videoID!=9 )      
     movie[videoID].stop();
    movie[0].play();
    videoID = 0; 
 }
 else if(  myArray[1]>-4000.0 ) {  // blinker recht 
   if ( videoID != 1 && videoID!=9 )      
     movie[videoID].stop();
    movie[1].play();
    videoID = 1; 
 }

 else if(  myArray[2] <-25000.0  || myArray[2] >17000.0  ) {  //  motor 
   if ( videoID != 2  && videoID!=9)      
     movie[videoID].stop();
    movie[2].play();
    videoID = 2; 
 }

 else if(  myArray[3] >5000.0 ) {  // Tür recht 
   if ( videoID != 3 && videoID!=9)      
     movie[videoID].stop();
    movie[3].play();
    videoID = 3; 
 }

 else if(  myArray[4] >20000.0 ) {  // Tür links 
   if ( videoID != 4 && videoID!=9 )      
     movie[videoID].stop();
    movie[4].play();
    videoID = 4; 
 }

 else if(  myArray[5]>20000.0 || myArray[5]<-25000.0) {   // Windschutz 
   if ( videoID != 5 && videoID!=9 )      
     movie[videoID].stop();
    movie[5].play();
    videoID = 5; 
 }

 else if(  myArray[6] >3000.0 ) { // coffer
   if ( videoID != 6  && videoID!=9)      
     movie[videoID].stop();
    movie[6].play();
    videoID = 6; 
 }
 else if(  myArray[7] >22000.0|| myArray[7]<-22000.0 ) {  // Fenester hinten
   if ( videoID != 7 && videoID!=9)      
     movie[videoID].stop();
    movie[7].play();
    videoID = 7; 
 }
 else if(  myArray[8]<-22000.0 ) {  // Dach 
   if ( videoID != 8 && videoID!=9  )      
     movie[videoID].stop();
    movie[8].play();
    videoID = 8; 
 }

 else {
   if( videoID != 9) 
       movie[videoID].stop();
  videoID=9;
 }
}
void serialEvent(Serial myPort) {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');
 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // split the string on the commas and convert the
 // resulting substrings into an integer array:
 try{
  myArray =  float(split(inString, ";"));
  mngMovie();
   for (int i = 0 ; i< 9 ; i++){
     print(myArray[i]); 
     print(";");
   }
  println(videoID);
 }
 catch(Exception e){
       // println("Error parsing:");
     //      for (int i = 0 ; i< 9 ; i++){
  //   print(myArray[i]); 
  //  print(";");
  // }
   println();
  //println(videoID);
   //     e.printStackTrace();
    }
 }
}

Answers

  • Have you tried with the P2D OpenGL renderer?

  • Answer ✓
    void iniMovies(){
       for (int i = 0 ; i< 9;i++){
           movie[i] =  new Movie(this,video[i]);
       }
    }  
    

    you are calling this within draw! draw runs 60 times a second (or tries to). so you are loading 10 HD movies every single frame. do it in setup().

  • hey — thanks so much koogs. True, now it runs really smoothly. Also changing the renderer helps a little bit. Here is my solution.

    import processing.video.*;
    import processing.serial.*;
    Serial myPort;
    Movie[] movie = new Movie[9];
    float[] myArray = new float[9];
    int videoSpeed = 0;
    PImage bg;
    PImage logo;
    int videoID = -1;
    String buffer = "";
    
    float fadeDirection = 0;
    float fadeLevel = 255.0;
    
    String[] video = {
      "v1.mp4",
      "v2.mp4",
      "v3.mp4",
      "v4.mp4",
      "v5.mp4",
      "v6.mp4",
      "v7.mp4",
      "v8.mp4",
      "v9.mp4"
    };
    
    
    
    void setup() {
      size(1920, 1080, P3D);
      iniMovies();
      frameRate(25);
      //bg = loadImage("1.jpg");
      //logo = loadImage("m.png");
       println(Serial.list()[0]);
       myPort = new Serial(this, Serial.list()[0], 19200);
       myPort.bufferUntil('\n');
    
    }
    void draw() {
      background(0);
    
      if (videoID < 0) {
        //image(logo, -57, -57); //, displayWidth/2, displayHeight/2);
      } else {
        image(movie[videoID], 0, 0);
      }
    
      if (fadeDirection != 0.0) {
        fadeLevel += fadeDirection;
        if (fadeLevel <= 0 || fadeLevel >= 255.0) {
           fadeDirection = 0.0;
           if (fadeLevel >= 255.0) {
              videoID = -1;
              fadeLevel = 255;
           } else {
             fadeLevel = 0;
           }
        }
    
    
      }
      fill(0, 0 ,0,fadeLevel);
      rect(0, 0, 1920, 1080);
    }
    
    void fadeIn() {
      fadeDirection = -30;
    }
    
    void fadeOut() {
      fadeDirection = 10;
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    void iniMovies() {
      for (int i = 0; i < 9; i++) {
        movie[i] = new Movie(this, video[i]);
      }
    }
    
    //int keyPressed() {
    //  int result;
    //  if (key == '1') { /// blinker link 
    //    result = 0;
    //  } else if (key == '2') { // blinker recht 
    //    result = 1;
    //  } else if (key == '3') { //  motor 
    //    result = 2;
    //  } else if (key == '4') { // Tür recht 
    //    result = 3;
    //  } else if (key == '5') { // Tür links 
    //    result = 4;
    //  } else if (key == '6') { // Windschutz 
    //    result = 5;
    //  } else if (key == '7') { // coffer
    //    result = 6;
    //  } else if (key == '8') { // Fenster hinten
    //    result = 7;
    //  } else if (key == '9') { // Dach 
    //    result = 8;
    //  } else {
    //    result = 9;
    //  }
    //  return result;
    //}
    
    
    int determineMovieId() {
      int result;
      if (myArray[0] > 2000.0) { /// Sensor blinker link 
        println("Sensor Blinker Links" + myArray[0]);
        result = 0;
      } else if (myArray[1] > -4000.0) { // Sensor blinker recht 
        println("Sensor Blinker Rechts" + myArray[1]);
        result = 1;
      } else if (myArray[2] < -25000.0 || myArray[2] > 17000.0) { //  Sensor motor 
      println("Sensor Motorhaube" + myArray[2]);
        result = 2;
      } else if (myArray[3] > 5000.0) { // Sensor Tür recht 
        result = 3;
      } else if (myArray[4] > 20000.0) { // Sensor Tür links 
        result = 4;
      } else if (myArray[5] > 20000.0 || myArray[5] < -25000.0) { // Sensor Windschutz 
        result = 5;
      } else if (myArray[6] > 3000.0) { // Sensor coffer
        result = 6;
      } else if (myArray[7] > 22000.0 || myArray[7] < -22000.0) { // Sensor Fenster hinten
        result = 7;
      } else if (myArray[8] < -22000.0) { // Sensor Dach 
        result = 8;
      } else {
        fadeOut();
        return videoID;
      }
      return result;
    }
    
    void mngMovie(int newMovie) {
      for (int x = 0; x < movie.length; x++) {
        if (x != newMovie) {
          //println("Stoping " + x);
          movie[x].stop();
        }
      }
      if (newMovie != videoID && videoID < 0) {
        fadeIn();
      }
      videoID = newMovie;
      if (videoID >= 0) {
        movie[videoID].loop();
        //println(frameRate);
      }
    }
    
    
    void keyPressed() {
    
      int newVideo = (videoID + 1) % myArray.length;
      print("Press - ");
      if (videoID == 1) {
        print("Fadeout - ");
        fadeOut();
        return;
      }
      mngMovie(newVideo);
    }
    
    
    void serialEvent(Serial myPort) {
      // get the ASCII string:
      String inString = myPort.readStringUntil('\n');
      if (inString != null) {
        // trim off any whitespace:
        inString = trim(inString);
        // split the string on the semicolons and convert the
        // resulting substrings into an float array:
        try {
          String[] numbers = split(inString, ";");
          //println("Raw line", inString);      
          for (int x=0;x<numbers.length && x < myArray.length;x++) {
            myArray[x] = float(numbers[x]);
            /*print(myArray[x]);
            print("- " + numbers[x]);
            print(";");*/
    
          }
          //println();
          mngMovie(determineMovieId());
          //println("Video ID" + videoID);
        } catch (Exception e) {
          // println("Error parsing:");
          //      for (int i = 0 ; i< 9 ; i++){
          //   print(myArray[i]); 
          //  print(";");
          // }
          //println(e);
          //println(videoID);
          //     e.printStackTrace();
        }
      }
     }
    
Sign In or Register to comment.