Advertisement
Guest User

capEncode.pde

a guest
Feb 17th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. import java.nio.channels.FileChannel;
  2. import java.nio.MappedByteBuffer;
  3. import java.nio.channels.Channels;
  4. import java.nio.channels.WritableByteChannel;
  5.  
  6. int ww = 1280;
  7. int hh = 720;
  8. String tmpLoc = "C:\\p5tmp";
  9. String saveLoc = "test\\";
  10. int ind = -1;
  11.  
  12. boolean doAudio;
  13. boolean started = true;
  14. boolean over;
  15. MappedByteBuffer mbb;
  16. String[] throb =
  17.   new String[]{
  18.     ".", " .", "  .", "   .", "    .", "     .","      ."};
  19.  
  20. RunCmd ffVid, ffAud;
  21.  
  22. void setup() {
  23.   size(200, 100);
  24.   frameRate(30);
  25.   saveLoc = sketchPath(saveLoc);
  26.  
  27.   try {
  28.     FileChannel fc =
  29.       new RandomAccessFile(tmpLoc, "rw").getChannel();
  30.     mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ww*hh*4);
  31.    
  32.     ffVid = new RunCmd(true, false, ww*hh*16) {
  33.       public String getCmd() {
  34.         return
  35.           "ffmpeg " +
  36.           "-y " +
  37.           "-f rawvideo -vcodec rawvideo -pix_fmt bgra " +
  38.           "-s " + ww + "x" + hh + " -i - " +
  39.           "-r 30 -vcodec h264 -crf 18 " +
  40.           "-x264opts open_gop=0:cabac=1 " +
  41.           "-preset veryfast -rtbufsize 3686400 " +
  42.           "-vf vflip " +
  43.           "testv.mp4";
  44.       }
  45. //      public void doResponse(String re) {
  46. //        println(re);
  47. //      }
  48.     };
  49.    
  50.     if(doAudio) {    
  51.       ffAud = new RunCmd(true, false, 5120) {
  52.         public String getCmd() {
  53.           return
  54.             "ffmpeg " +
  55.             "-y " +
  56.             "-f dshow -i audio=\"virtual-audio-capturer\" " +
  57.             "-ac 2 " +
  58.             "testa.mp4";
  59.         }
  60. //        public void doResponse(String re) {
  61. //          println(re);
  62. //        }
  63.       };
  64.     }
  65.    
  66.   } catch(Throwable ex) {ex.printStackTrace();}
  67.  
  68. }
  69.  
  70. void mouseClicked() {
  71.   if(!started) {
  72.     println("starting output");
  73.     started = true;
  74.     return;
  75.   } else {
  76.     println("ending output");
  77.     started = false;
  78.     try {
  79.       ffVid.proc.getOutputStream().close();
  80.       if(doAudio) {
  81.         OutputStream os = ffAud.proc.getOutputStream();
  82.         os.write("q\n".getBytes());
  83.         os.flush();
  84.      
  85.         Thread.sleep(500);
  86.        
  87.         new RunCmd(true, true, 4096) {
  88.           public String getCmd() {
  89.             return
  90.               "ffmpeg -y " +
  91.               "-i testv.mp4 -i testa.mp4 " +
  92.               "-c copy -shortest " +
  93.               "testm.mp4";
  94.           }
  95.           public void doResponse(String re) {
  96.             println(re);
  97.           }
  98.           public void ended() {
  99.             new File(sketchPath("testa.mp4")).delete();
  100.             new File(sketchPath("testv.mp4")).delete();
  101.           }
  102.         };
  103.  
  104.       }
  105.      
  106.     } catch(Throwable ex) {ex.printStackTrace();}    
  107.     exit();
  108.   }
  109. }
  110.  
  111. void draw() {
  112.   frame.setTitle(
  113.     nf((ind>-1?ind:0), 6) + " " + throb[frameCount%7]);
  114.  
  115.   try {
  116.     if(started) {
  117.       ind++;
  118.       mbb.rewind();
  119.       if(!ffVid.dead) ffVid.wbc.write(mbb);
  120.     }
  121.   } catch(Throwable ex) {}
  122. }
  123.  
  124. void exit() {
  125.   try {
  126.     Thread.currentThread().sleep(500);
  127.   } catch(Throwable ex) {ex.printStackTrace();}
  128.   super.exit();
  129. }
  130.  
  131. class RunCmd {
  132.   boolean dead;
  133.   Process proc;
  134.   WritableByteChannel wbc;
  135.  
  136.   RunCmd(boolean doCom, boolean doEnd, int bf) throws Throwable {
  137.     ProcessBuilder builder = new ProcessBuilder(
  138.       Arrays.asList(splitTokens(getCmd())));
  139.     builder.directory(new File(getDir()));
  140.     builder.redirectErrorStream(true);
  141.     proc = builder.start();
  142.     BufferedOutputStream bos = new BufferedOutputStream(
  143.       proc.getOutputStream(), bf);
  144.     final BufferedReader br = new BufferedReader(
  145.       new InputStreamReader(proc.getInputStream()));
  146.    
  147.     if(doCom) {
  148.       wbc = Channels.newChannel(bos);
  149.       new Thread(new Runnable() {
  150.         public void run() {
  151.           String ln;
  152.           try {
  153.             while(!dead && (ln = br.readLine()) != null) {
  154.               doResponse(ln);
  155.             }
  156.           } catch(Throwable ex) {ex.printStackTrace();}
  157.         }
  158.       }).start();
  159.     }
  160.    
  161.     if(doEnd) {
  162.       new Thread(new Runnable() {
  163.         public void run() {
  164.           try {
  165.             proc.waitFor();
  166.             die();
  167.             proc.destroy();
  168.           } catch(Throwable ex) {ex.printStackTrace();}
  169.         }
  170.       }).start();
  171.     }
  172.   }
  173.  
  174.   void die() throws Throwable {
  175.     dead = true;
  176.     ended();
  177.   }
  178.  
  179.   String getDir() {return sketchPath("");}
  180.   String getCmd() {return "";}
  181.   void doResponse(String re) {}
  182.   void ended() {}
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement