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
| class ScreenRecorderUtil extends ScreenRecorder { private static ScreenRecorder screenRecorder;
private String scenarioName; private static String videoName; private static final String RECORDING_PATH = "./build/recording";
public DPMScreenRecorderUtil(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat, Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String scenarioName) throws IOException, AWTException { super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder); this.scenarioName = scenarioName; }
@Override protected File createMovieFile(Format fileFormat) throws IOException {
if (!movieFolder.exists()) { movieFolder.mkdirs(); } else if (!movieFolder.isDirectory()) { throw new IOException("\"" + movieFolder + "\" is not a directory."); } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss"); videoName = scenarioName + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat); return new File(movieFolder, scenarioName + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat)); }
public static void startRecord(String scenario) throws Exception {
File file = new File(RECORDING_PATH); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = screenSize.width; int height = screenSize.height;
Rectangle captureSize = new Rectangle(0, 0, width, height);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice() .getDefaultConfiguration(); screenRecorder = new DPMScreenRecorderUtil(gc, captureSize, new Format(MediaTypeKey, FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI), new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, (int) 24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (int) (15 * 60)), new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, "black", FrameRateKey, Rational.valueOf(30)), null, file, scenario); screenRecorder.start(); }
public static void stopRecord() throws Exception { screenRecorder.stop(); }
public static byte[] getRecord() throws Exception { return FileUtil.readAsByteArray(convertAviToMP4(new File(RECORDING_PATH + "/" + videoName))); }
private static File convertAviToMP4(File source) throws Exception { File target = new File(RECORDING_PATH + "/" + "target.mp4"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(64000); audio.setChannels(1); audio.setSamplingRate(22050); VideoAttributes video = new VideoAttributes(); video.setCodec("libx264"); video.setBitRate(1920 * 1080); video.setFrameRate(120); video.setPixelFormat("yuv420p"); EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat("mp4"); attrs.setInputFormat("avi"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); MultimediaObject multimediaObject = new MultimediaObject(source); encoder.encode(multimediaObject, target, attrs); return target; } }
|