Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2012-06-18 07:25:56, viewed 244 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316  
File src = new File("D:/downloads/12345678.mp4");
   File dst = new File("R:/vlc-frames/");

   PlaybackVLC playback = new PlaybackVLC(src, dst, "tga");
   playback.configureStartStop(5*60, 8*60);
   playback.configureSceneSize(1024, -1);
   playback.spawnVLC("C:/mounts/workdisk/apps/videolan-201/vlc.exe");
   playback.startDisplayLoop(srcFile.getName());

// ---

   public class PlaybackVLC {

      final AtomicBoolean processTerminated = new AtomicBoolean(false);
      final File srcFile, dstDir;
      final String ext;

      public PlaybackVLC(File srcFile, File dstDir, String ext) {
         if (!srcFile.exists()) {
            throw new IllegalStateException("file not found: " + srcFile);
         }

         this.srcFile = srcFile;
         this.dstDir = dstDir;
         this.ext = ext;

         this.frameDataQueue = new SimpleBlockingQueue<>(2);
         this.frameImageQueue = new SimpleBlockingQueue<>(2);
      }

      private int startTime = -1, stopTime = -1;

      public void configureStartStop(int startTime, int stopTime) {
         if (startTime < -1 || stopTime < -1)
            throw new IllegalArgumentException();
         if ((startTime != -1 && stopTime != -1) && startTime > stopTime)
            throw new IllegalArgumentException();
         this.startTime = startTime;
         this.stopTime = stopTime;
      }

      private int sceneWidth, sceneHeight;

      public void configureSceneSize(int sceneWidth, int sceneHeight) {
         if (sceneWidth < -1 || sceneHeight < -1)
            throw new IllegalArgumentException();
         this.sceneWidth = sceneWidth;
         this.sceneHeight = sceneHeight;
      }

      final SimpleBlockingQueue<byte[]> frameDataQueue;
      final SimpleBlockingQueue<BufferedImage> frameImageQueue;

      Process vlcProcess;

      public void spawnVLC(String path) {

         dstDir.mkdirs();
         FileUtil.deleteDirectory(dstDir, false);

         List<String> vlcArgs = new ArrayList<String>();
         {
            vlcArgs.add(path);
            vlcArgs.add(srcFile.getAbsolutePath());
            vlcArgs.add("--vout=dummy");
            vlcArgs.add("-I");
            vlcArgs.add("dummy");
            vlcArgs.add("--dummy-quiet");
            vlcArgs.add("-V");
            vlcArgs.add("dummy");

            if (startTime != -1) {
               vlcArgs.add("--start-time=" + startTime);
            }
            if (stopTime != -1) {
               vlcArgs.add("--stop-time=" + stopTime);
            }

            vlcArgs.add("--video-filter=scene");
            vlcArgs.add("--scene-ratio=1"); // show every frame
            vlcArgs.add("--scene-path=" + dstDir.getAbsolutePath());
            vlcArgs.add("--scene-format=" + ext);
            vlcArgs.add("--scene-prefix=frame_");
            // vlcArgs.add("--scene-replace"); // overwrite the same file

            if (sceneWidth != -1) {
               vlcArgs.add("--scene-width=" + sceneWidth);
            }
            if (sceneHeight != -1) {
               vlcArgs.add("--scene-height=" + sceneHeight);
            }

            vlcArgs.add("vlc://quit");
         }

         try {
            vlcProcess = Runtime.getRuntime().exec(vlcArgs.toArray(new String[0]));
         } catch (Exception exc) {
            throw new IllegalStateException("failed to start VLC", exc);
         }

         Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
               PlaybackVLC.this.stop();
            }
         });

         this.startProcessMonitor();
         this.startReadLoop();
         this.startDecodeLoop();
      }

      void startProcessMonitor() {
         Streams.asynchronousTransfer(vlcProcess.getInputStream(), System.out, true, false);
         Streams.asynchronousTransfer(vlcProcess.getErrorStream(), System.err, true, false);

         new Thread(new Runnable() {
            @Override
            public void run() {
               int exitValue;
               while (true) {
                  try {
                     exitValue = vlcProcess.waitFor();
                     break;
                  } catch (Exception exc) {
                     // ignore
                  }
               }

               processTerminated.set(true);

               System.out.println("VLC exit value: " + exitValue);
            }
         }).start();
      }

      void startReadLoop() {
         new Thread(new Runnable() {
            @Override
            public void run() {
               runReadLoop();
            }
         }).start();
      }

      void startDecodeLoop() {
         new Thread(new Runnable() {
            @Override
            public void run() {
               runDecodeLoop();
            }
         }).start();
      }

      void startDisplayLoop(final String windowTitle) {
         new Thread(new Runnable() {
            @Override
            public void run() {
               runDisplayLoop(windowTitle);
            }
         }).start();
      }

      void runReadLoop() {

         outer: for (int frameIndex = 1; true;) {

            String fIndex = TextValues.formatNumber(frameIndex, 5);
            File frameFile = new File(dstDir + "/frame_" + fIndex + "." + ext);

            while (!frameFile.exists()) {
               if (processTerminated.get()) {
                  break outer;
               }
               HighLevel.sleep(10);
               continue;
            }

            byte[] currFrame;

            try {
               currFrame = FileUtil.readFile(frameFile);
            } catch (Exception exc) {
               continue;
            }

            if (currFrame == null) {
               continue;
            }

            frameDataQueue.put(currFrame);

            if (!frameFile.delete()) {
               System.out.println("failed to delete: " + frameFile.getName());
               frameFile.deleteOnExit();
            }

            frameIndex++;
         }

         frameDataQueue.put(null);

         System.out.println("VLC Process: EOF");
      }

      void runDecodeLoop() {
         while (true) {
            byte[] data = frameDataQueue.take();
            if (data == null) {
               break;
            }

            BufferedImage image;
            try {
               if (ext.equals("tga"))
                  image = ImageUtil.readTGA(new ByteArrayInputStream(data), data.length);
               else
                  image = ImageIO.read(new ByteArrayInputStream(data));
            } catch (Exception exc) {
               continue;
            }
            if (image == null) {
               continue;
            }

            frameImageQueue.put(image);
         }

         frameImageQueue.put(null);

         System.out.println("Data Queue: EOF");
      }

      public void runDisplayLoop(String windowTitle) {
         BufferedImage image = frameImageQueue.take();
         if (image == null) {
            System.out.println("no initial frame");
            return;
         }
         Dimension dim = new Dimension(image.getWidth(), image.getHeight());
         System.out.println(dim);

         Canvas canvas = new Canvas();
         canvas.setIgnoreRepaint(true);
         canvas.setSize(dim);

         final Frame window = new Frame();
         window.setIgnoreRepaint(true);
         window.setTitle(windowTitle);
         window.setFocusable(true);
         window.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               window.dispose();
            }
         });
         window.add(canvas);
         window.setResizable(false);
         window.pack();
         window.setLocationRelativeTo(null);
         window.setVisible(true);

         canvas.createBufferStrategy(2);
         final BufferStrategy buffer = canvas.getBufferStrategy();

         long secondStarted = System.nanoTime();
         int framecounter = 0;

         while (window.isVisible()) {
            image = frameImageQueue.take();
            if (image == null) {
               System.out.println("Image Queue: EOF");
               break;
            }

            Graphics g;
            try {
               g = buffer.getDrawGraphics();
            } catch (IllegalStateException exc) {
               break;
            }
            g.drawImage(image, 0, 0, null);
            g.dispose();
            image.flush();
            buffer.show();

            framecounter++;
            if (System.nanoTime() > secondStarted + 1000_000_000L) {
               window.setTitle(windowTitle + " - " + framecounter + "fps");
               framecounter = 0;
               secondStarted += 1000_000_000L;
            }
         }

         window.dispose();

         this.stop();
      }

      public void stop() {
         if (this.isProcessAlive()) {
            System.out.println("Terminating VLC process");
            vlcProcess.destroy();
         }
      }

      private boolean isProcessAlive() {
         try {
            vlcProcess.exitValue();
            return false;
         } catch (IllegalThreadStateException exc) {
            return true;
         }
      }
   }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (49 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!