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).

1  
!7
2  
3  
import org.jcodec.api.awt.AWTSequenceEncoder8Bit;
4  
5  
static int framesToEncode = 100000;
6  
7  
p {
8  
  inputFilePath("Video to modify", voidfunc(final File video) {
9  
    thread {
10  
      invertVideo(video);
11  
    }
12  
  });
13  
}
14  
15  
svoid invertVideo(File video) {
16  
  File outFile = prepareProgramFile(addSuffix("inverted-" + video.getName(), ".mp4"));
17  
  
18  
  final LeftQueue lq = new LeftQueue(video);
19  
  final RightQueue rq = new RightQueue(outFile);
20  
  lq.rightQueue = rq;
21  
  
22  
  stepThreads(1, lq, rq);
23  
}
24  
25  
svoid stepThreads(final int interval, Steppable... engines) {
26  
  final EngineGroup group = new EngineGroup(asList(engines));
27  
  
28  
  new L<Thread> threads;
29  
  for (final Steppable s : engines) {
30  
    Thread thread = new Thread("Stepper Thread") {
31  
      public void run() ctex {
32  
        s.step();
33  
        while licensed {
34  
          while (s.shouldStep) {
35  
            s.shouldStep = false;
36  
            s.step();
37  
          }
38  
          synchronized(s) { s.wait(interval); }
39  
        }
40  
      }
41  
    };
42  
    s.thread = thread;
43  
    threads.add(thread);
44  
  }
45  
  for (Thread t : threads) t.start();
46  
}
47  
48  
sclass EngineGroup {
49  
  L<Steppable> engines;
50  
  
51  
  *(L<Steppable> *engines) {
52  
    for (Steppable engine : engines)
53  
      engine.group = this;
54  
  }
55  
  
56  
  void shouldStep {
57  
    for (Steppable engine : engines)
58  
      engine.shouldStepLocally();
59  
  }
60  
}
61  
62  
abstract sclass Steppable {
63  
  volatile bool shouldStep;
64  
  Thread thread;
65  
  EngineGroup group;
66  
  
67  
  void shouldStep() {
68  
    if (group != null)
69  
      group.shouldStep();
70  
    else
71  
      shouldStepLocally();
72  
  }
73  
  
74  
  void shouldStepLocally {
75  
    shouldStep = true;
76  
    synchronized(this) { notifyAll(); }
77  
  }
78  
  
79  
  abstract void step();
80  
}
81  
82  
// decoding part
83  
Steppable > LeftQueue {
84  
  Iterator<BufferedImage> stream;
85  
  RightQueue rightQueue;
86  
87  
  *(File video) {
88  
    stream = framesFromVideo_reordering(video);
89  
  }
90  
  
91  
  void step {
92  
    if (stream != null && rightQueue.needImage())
93  
      if (stream.hasNext()) {
94  
        rightQueue.receive(stream.next());
95  
        shouldStep();
96  
      } else {
97  
        stream = null;
98  
        rightQueue.close();
99  
      }
100  
  }
101  
}
102  
103  
// encoding part
104  
Steppable > RightQueue {
105  
  File outMP4;
106  
  AWTSequenceEncoder8Bit enc;
107  
  volatile BufferedImage img;
108  
  int frames;
109  
  volatile bool shouldClose;
110  
111  
  *(File *outMP4) ctex {
112  
    enc = AWTSequenceEncoder8Bit.create25Fps(outMP4);
113  
    enc.getEncoder().setKeyInterval(25);
114  
  }
115  
    
116  
  void receive(BufferedImage img) {
117  
    if (this.img != null) fail();
118  
    this.img = img;
119  
    shouldStep();
120  
  }
121  
  
122  
  bool needImage() { ret enc != null && img == null; }
123  
  
124  
  void step ctex {
125  
    if (shouldClose) {
126  
      shouldClose = false;
127  
      close();
128  
    }
129  
    
130  
    if (img != null && enc != null) {
131  
      img = invertedImage(img);
132  
      enc.encodeImage(img);
133  
      img = null;
134  
      ++frames;
135  
      print("Frames: " + frames + "/" + framesToEncode);
136  
      if (frames >= framesToEncode) shouldClose = true;
137  
      else shouldStep();
138  
    }
139  
  }
140  
  
141  
  void close ctex {
142  
    if (enc != null) {
143  
      enc.finish();
144  
      enc = null;
145  
      print("Wrote " + f2s(outMP4) + " (" + frames + " frames)");
146  
    }
147  
  }
148  
}

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: 528 / 618
Version history: 20 change(s)
Referenced in: [show references]