Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

148
LINES

< > BotCompany Repo | #1006827 // Invert video (output to .mp4), use 2 threads [seems to work]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 2002K of libraries. Compilation Failed (7899L/56K).

!7

import org.jcodec.api.awt.AWTSequenceEncoder8Bit;

static int framesToEncode = 100000;

p {
  inputFilePath("Video to modify", voidfunc(final File video) {
    thread {
      invertVideo(video);
    }
  });
}

svoid invertVideo(File video) {
  File outFile = prepareProgramFile(addSuffix("inverted-" + video.getName(), ".mp4"));
  
  final LeftQueue lq = new LeftQueue(video);
  final RightQueue rq = new RightQueue(outFile);
  lq.rightQueue = rq;
  
  stepThreads(1, lq, rq);
}

svoid stepThreads(final int interval, Steppable... engines) {
  final EngineGroup group = new EngineGroup(asList(engines));
  
  new L<Thread> threads;
  for (final Steppable s : engines) {
    Thread thread = new Thread("Stepper Thread") {
      public void run() ctex {
        s.step();
        while licensed {
          while (s.shouldStep) {
            s.shouldStep = false;
            s.step();
          }
          synchronized(s) { s.wait(interval); }
        }
      }
    };
    s.thread = thread;
    threads.add(thread);
  }
  for (Thread t : threads) t.start();
}

sclass EngineGroup {
  L<Steppable> engines;
  
  *(L<Steppable> *engines) {
    for (Steppable engine : engines)
      engine.group = this;
  }
  
  void shouldStep {
    for (Steppable engine : engines)
      engine.shouldStepLocally();
  }
}

abstract sclass Steppable {
  volatile bool shouldStep;
  Thread thread;
  EngineGroup group;
  
  void shouldStep() {
    if (group != null)
      group.shouldStep();
    else
      shouldStepLocally();
  }
  
  void shouldStepLocally {
    shouldStep = true;
    synchronized(this) { notifyAll(); }
  }
  
  abstract void step();
}

// decoding part
Steppable > LeftQueue {
  Iterator<BufferedImage> stream;
  RightQueue rightQueue;

  *(File video) {
    stream = framesFromVideo_reordering(video);
  }
  
  void step {
    if (stream != null && rightQueue.needImage())
      if (stream.hasNext()) {
        rightQueue.receive(stream.next());
        shouldStep();
      } else {
        stream = null;
        rightQueue.close();
      }
  }
}

// encoding part
Steppable > RightQueue {
  File outMP4;
  AWTSequenceEncoder8Bit enc;
  volatile BufferedImage img;
  int frames;
  volatile bool shouldClose;

  *(File *outMP4) ctex {
    enc = AWTSequenceEncoder8Bit.create25Fps(outMP4);
    enc.getEncoder().setKeyInterval(25);
  }
    
  void receive(BufferedImage img) {
    if (this.img != null) fail();
    this.img = img;
    shouldStep();
  }
  
  bool needImage() { ret enc != null && img == null; }
  
  void step ctex {
    if (shouldClose) {
      shouldClose = false;
      close();
    }
    
    if (img != null && enc != null) {
      img = invertedImage(img);
      enc.encodeImage(img);
      img = null;
      ++frames;
      print("Frames: " + frames + "/" + framesToEncode);
      if (frames >= framesToEncode) shouldClose = true;
      else shouldStep();
    }
  }
  
  void close ctex {
    if (enc != null) {
      enc.finish();
      enc = null;
      print("Wrote " + f2s(outMP4) + " (" + frames + " frames)");
    }
  }
}

Author comment

Began life as a copy of #1006825

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1006827
Snippet name: Invert video (output to .mp4), use 2 threads [seems to work]
Eternal ID of this version: #1006827/21
Text MD5: 0507aa2ada4df98137f8d06598464089
Transpilation MD5: 6983705742f29aec88d6a65105762a1d
Author: stefan
Category: javax video
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-05 06:42:59
Source code size: 3209 bytes / 148 lines
Pitched / IR pitched: No / No
Views / Downloads: 529 / 618
Version history: 20 change(s)
Referenced in: