Advertisement
Guest User

xuggle-xuggler 5.4 NullPointerException when encoding audio

a guest
Jun 18th, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. package exe.media;
  2.  
  3. import java.awt.Rectangle;
  4. import java.awt.Robot;
  5. import java.awt.Toolkit;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.text.SimpleDateFormat;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. import javax.sound.sampled.AudioFormat;
  12. import javax.sound.sampled.AudioSystem;
  13. import javax.sound.sampled.DataLine;
  14. import javax.sound.sampled.TargetDataLine;
  15.  
  16. import com.xuggle.mediatool.IMediaWriter;
  17. import com.xuggle.mediatool.ToolFactory;
  18. import com.xuggle.xuggler.IRational;
  19.  
  20. import exe.logger.TimeLookup;
  21.  
  22. public class CaptureScreen {
  23.  
  24.     private static IRational FRAME_RATE = IRational.make(15, 1);
  25.     private static volatile boolean recording = false;
  26.  
  27.     public synchronized static File captureScreen(String name,
  28.             final int duration) {
  29.         SimpleDateFormat dateFormat = new SimpleDateFormat(
  30.                 "yyyy-MM-dd HH.mm.ss");
  31.         final File file = new File(name + "-"
  32.                 + dateFormat.format(TimeLookup.getTime()) + ".mp4");
  33.         new Thread(new Runnable() {
  34.  
  35.             @Override
  36.             public void run() {
  37.                 try {
  38.                     // This is the robot for taking a snapshot of the
  39.                     // screen. It's part of Java AWT
  40.                     final Robot robot = new Robot();
  41.                     final Toolkit toolkit = Toolkit.getDefaultToolkit();
  42.                     final Rectangle screenBounds = new Rectangle(toolkit
  43.                             .getScreenSize());
  44.  
  45.                     // First, let's make a IMediaWriter to write the file.
  46.                     final IMediaWriter writer = ToolFactory.makeWriter(file
  47.                             .getAbsolutePath());
  48.  
  49.                     // We tell it we're going to add one video stream, with id
  50.                     // 0,
  51.                     // at position 0, and that it will have a fixed frame rate
  52.                     // of
  53.                     // FRAME_RATE.
  54.                     writer.addVideoStream(0, 0, FRAME_RATE, screenBounds.width,
  55.                             screenBounds.height);
  56.                     AudioFormat format = new AudioFormat(8000.0F, 16, 1, true,
  57.                             false);
  58.                     writer.addAudioStream(1, 0, 1, (int) format.getSampleRate());
  59.                     TargetDataLine line = getTargetDataLineForRecord(format);
  60.                     final int frameSizeInBytes = format.getFrameSize();
  61.                     final int bufferLengthInFrames = line.getBufferSize() / 8;
  62.                     final int bufferLengthInBytes = bufferLengthInFrames
  63.                             * frameSizeInBytes;
  64.                     final byte[] buf = new byte[bufferLengthInBytes];
  65.                     final long startTime = System.nanoTime();
  66.                     new Thread(new Runnable() {
  67.  
  68.                         @Override
  69.                         public void run() {
  70.                             long lastTime = startTime;
  71.                             double ns = 1000000000 / FRAME_RATE.getDouble();
  72.                             double delta = 0.0D;
  73.                             double FRAMES_TAKEN = 0.0D;
  74.                             while (FRAMES_TAKEN < (duration * FRAME_RATE
  75.                                     .getDouble())) {
  76.                                 long now = System.nanoTime();
  77.                                 delta += (now - lastTime) / ns;
  78.                                 lastTime = now;
  79.                                 if (delta >= 1.0D) {
  80.                                     BufferedImage screen = robot
  81.                                             .createScreenCapture(screenBounds);
  82.                                     BufferedImage bgrScreen = convertToType(
  83.                                             screen,
  84.                                             BufferedImage.TYPE_3BYTE_BGR);
  85.                                     writer.encodeVideo(0, bgrScreen,
  86.                                             System.nanoTime() - startTime,
  87.                                             TimeUnit.NANOSECONDS);
  88.                                     FRAMES_TAKEN++;
  89.                                     delta -= 1.0D;
  90.                                 }
  91.                             }
  92.                             recording = false;
  93.                         }
  94.                     }).start();
  95.                     recording = true;
  96.                     while (recording) {
  97.                         int numBytesRead = 0;
  98.                         numBytesRead = line.read(buf, 0, bufferLengthInBytes);
  99.                         int numSamplesRead = numBytesRead / 2;
  100.                         short[] audioSamples = new short[numSamplesRead];
  101.                         if (format.isBigEndian()) {
  102.                             for (int i = 0; i < numSamplesRead; i++) {
  103.                                 audioSamples[i] = (short) ((buf[2 * i] << 8) | buf[2 * i + 1]);
  104.                             }
  105.                         } else {
  106.                             for (int i = 0; i < numSamplesRead; i++) {
  107.                                 audioSamples[i] = (short) ((buf[2 * i + 1] << 8) | buf[2 * i]);
  108.                             }
  109.                         }
  110.                         writer.encodeAudio(1, audioSamples, System.nanoTime()
  111.                                 - startTime, TimeUnit.NANOSECONDS); // CaptureScreen.java:118
  112.                     }
  113.                     writer.close();
  114.                 } catch (Exception e) {
  115.                     e.printStackTrace();
  116.                 }
  117.             }
  118.  
  119.         }).start();
  120.         return file;
  121.     }
  122.  
  123.     /**
  124.      * Convert a {@link BufferedImage} of any type, to {@link BufferedImage} of
  125.      * a specified type. If the source image is the same type as the target
  126.      * type, then original image is returned, otherwise new image of the correct
  127.      * type is created and the content of the source image is copied into the
  128.      * new image.
  129.      *
  130.      * @param sourceImage
  131.      *            the image to be converted
  132.      * @param targetType
  133.      *            the desired BufferedImage type
  134.      *
  135.      * @return a BufferedImage of the specifed target type.
  136.      *
  137.      * @see BufferedImage
  138.      */
  139.  
  140.     private static BufferedImage convertToType(BufferedImage sourceImage,
  141.             int targetType) {
  142.         BufferedImage image;
  143.  
  144.         // if the source image is already the target type, return the source
  145.         // image
  146.  
  147.         if (sourceImage.getType() == targetType)
  148.             image = sourceImage;
  149.  
  150.         // otherwise create a new image of the target type and draw the new
  151.         // image
  152.  
  153.         else {
  154.             image = new BufferedImage(sourceImage.getWidth(),
  155.                     sourceImage.getHeight(), targetType);
  156.             image.getGraphics().drawImage(sourceImage, 0, 0, null);
  157.         }
  158.  
  159.         return image;
  160.     }
  161.  
  162.     private static TargetDataLine getTargetDataLineForRecord(AudioFormat format) {
  163.         TargetDataLine line;
  164.         final DataLine.Info info = new DataLine.Info(TargetDataLine.class,
  165.                 format);
  166.         if (!AudioSystem.isLineSupported(info)) {
  167.             return null;
  168.         }
  169.         // get and open the target data line for capture.
  170.         try {
  171.             line = (TargetDataLine) AudioSystem.getLine(info);
  172.             line.open(format, line.getBufferSize());
  173.         } catch (final Exception ex) {
  174.             return null;
  175.         }
  176.         return line;
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement