xuggler
Create video from image frames with Xuggler
Let’s now see how to create a video from scratch. As input, we will use sequential snapshots from our desktop. This can be used for a rudimentary screen recording application. This is very important when you want to make your want to create individual frames and make your own custom video. We are going to use Xuggler for that.
In short, to create video from image frames with Xuggler, one should follow these basic steps:
- Make a
IMediaWriter
to write the file usingToolFactory.makeWriter(outputFilename)
. - Get the screen
Dimension
you are working with usingToolkit.getDefaultToolkit().getScreenSize()
. - Tell the
IMediaWriter
that we’re going to add one video stream, with id 0,at position 0, and that it will have a fixed frame rate of FRAME_RATE callingwriter.addVideoStream(0, 0,ICodec.ID.CODEC_ID_MPEG4,screenBounds.width/2,screenBounds.height/2);
- Use
Robot.createScreenCapture
to take Desktop screenshots and convert it the right image type. - Encode the image to stream using
writer.encodeVideo
.
Let’s see the code snippet that follows:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 | package com.javacodegeeks.xuggler; import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.util.concurrent.TimeUnit; import com.xuggle.mediatool.IMediaWriter; import com.xuggle.mediatool.ToolFactory; import com.xuggle.xuggler.ICodec; public class ScreenRecordingExample { private static final double FRAME_RATE = 50 ; private static final int SECONDS_TO_RUN_FOR = 20 ; private static final String outputFilename = "c:/mydesktop.mp4" ; private static Dimension screenBounds; public static void main(String[] args) { // let's make a IMediaWriter to write the file. final IMediaWriter writer = ToolFactory.makeWriter(outputFilename); screenBounds = Toolkit.getDefaultToolkit().getScreenSize(); // We tell it we're going to add one video stream, with id 0, // at position 0, and that it will have a fixed frame rate of FRAME_RATE. writer.addVideoStream( 0 , 0 , ICodec.ID.CODEC_ID_MPEG4, screenBounds.width/ 2 , screenBounds.height/ 2 ); long startTime = System.nanoTime(); for ( int index = 0 ; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) { // take the screen shot BufferedImage screen = getDesktopScreenshot(); // convert to the right image type BufferedImage bgrScreen = convertToType(screen, BufferedImage.TYPE_3BYTE_BGR); // encode the image to stream #0 writer.encodeVideo( 0 , bgrScreen, System.nanoTime() - startTime, TimeUnit.NANOSECONDS); // sleep for frame rate milliseconds try { Thread.sleep(( long ) ( 1000 / FRAME_RATE)); } catch (InterruptedException e) { // ignore } } // tell the writer to close and write the trailer if needed writer.close(); } public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) { BufferedImage image; // if the source image is already the target type, return the source image if (sourceImage.getType() == targetType) { image = sourceImage; } // otherwise create a new image of the target type and draw the new image else { image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType); image.getGraphics().drawImage(sourceImage, 0 , 0 , null ); } return image; } private static BufferedImage getDesktopScreenshot() { try { Robot robot = new Robot(); Rectangle captureSize = new Rectangle(screenBounds); return robot.createScreenCapture(captureSize); } catch (AWTException e) { e.printStackTrace(); return null ; } } } |
This was an example on how to create video from image frames with Xuggler.
Related Article: