Guest User

Processing sketch to send syphon input to ffmpeg

a guest
Jul 1st, 2015
329
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import codeanticode.syphon.*;
  2.  
  3. import java.io.*;
  4. String ffmpegInstall = "/opt/local/bin/";  // you may need to specify the location that ffmpeg is installed**
  5. String exportFilename = "out.mov";        // MUST INCLUDE EXTENSION (mov, mp4, or see ffmpeg doc for list)
  6. int frameWidth = 1280;                      // size for exported video
  7. int frameHeight = 720;
  8. int fps = 30;                              // frames per second for resulting video
  9. String codec = "mpeg4";
  10. String commandToRun;
  11.  
  12. PGraphics canvas;
  13. SyphonClient client;
  14.  
  15. public void setup() {
  16.   size(1280, 720, P3D);
  17.    
  18.   println("Available Syphon servers:");
  19.   println(SyphonClient.listServers());
  20.    
  21.   // Create syhpon client to receive frames
  22.   // from the first available running server:
  23.   client = new SyphonClient(this);
  24.  
  25.   // A Syphon server can be specified by the name of the application that it contains it,
  26.   // its name, or both:
  27.  
  28.   // Only application name.
  29.   //client = new SyphonClient(this, "SendFrames");
  30.    
  31.   // Both application and server names
  32.   //client = new SyphonClient(this, "SendFrames", "Processing Syphon");
  33.  
  34.   // Only server name
  35.   //client = new SyphonClient(this, "", "Processing Syphon");
  36.    
  37.   // An application can have several servers:
  38.   //client = new SyphonClient(this, "Quartz Composer", "Raw Image");
  39.   //client = new SyphonClient(this, "Quartz Composer", "Scene");
  40.  
  41.   background(0);
  42. }
  43.  
  44. public void draw() {    
  45.   if (client.available()) {
  46.     canvas = client.getGraphics(canvas);
  47.     image(canvas, 0, 0, width, height);
  48.     saveFrame("/Volumes/tempdisk/frame.tif");
  49. //    commandToRun = ffmpegInstall + "ffmpeg -y -r " + fps + " -i /Volumes/tempdisk/frame.tif -vcodec " + codec + " " + exportFilename;   // command that runs ffmpeg
  50. commandToRun = ffmpegInstall + "ffmpeg -y -r " + fps + " -i /Volumes/tempdisk/frame.tif -vcodec " + codec + " " + "-preset ultrafast -vf setsar=1:1 -q 1 -an -f rawvideo udp://239.0.1.22:1234";
  51.     UnixCommand(commandToRun);
  52.   }  
  53. }
  54.  
  55. void keyPressed() {
  56.   if (key == ' ') {
  57.     client.stop();  
  58.   } else if (key == 'd') {
  59.     println(client.getServerName());
  60.   }
  61. }
  62.  
  63. void UnixCommand(String commandToRun) {
  64.   File workingDir = new File(sketchPath(""));
  65.   String returnedValues;
  66.   try {
  67.     Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir);
  68.     int i = p.waitFor();
  69.     if (i == 0) {
  70.       BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  71.       while ( (returnedValues = stdInput.readLine ()) != null) {
  72.         // enable this option if you want to get updates when the process succeeds
  73.         // println("  " + returnedValues);
  74.       }
  75.     }
  76.     else {
  77.       BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  78.       while ( (returnedValues = stdErr.readLine ()) != null) {
  79.         // print information if there is an error or warning (like if a file already exists, etc)
  80.         println("  " + returnedValues);
  81.       }
  82.     }
  83.   }
  84.  
  85.   // if there is an error, let us know
  86.   catch (Exception e) {
  87.     println("Error, sorry!");  
  88.     println("     " + e);
  89.   }
  90. }
RAW Paste Data