Saturday, March 9, 2019

Record video and save video by method name or any name by passing parameter

Create a class named:  SpecializedScreenRecorder.java

Paste the below code into the class

 package learning;  
 import java.awt.AWTException;  
 import java.awt.GraphicsConfiguration;  
 import java.awt.Rectangle;  
 import java.io.File;  
 import java.io.IOException;  
 import java.text.SimpleDateFormat;  
 import java.util.Date;  
 import org.monte.media.Format;  
 import org.monte.media.Registry;  
 import org.monte.screenrecorder.ScreenRecorder;  
 public class SpecializedScreenRecorder extends ScreenRecorder {  
      private String name;  
      public SpecializedScreenRecorder(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat,  
                Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder,String name)  
                throws IOException, AWTException {  
           super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder);  
           this.name=name;  
      }  
      @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");  
           return new File(movieFolder,  
                     name + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat));  
      }  
 }  

Create another class named: Video.java

copy below code into that

 package learning;  
 import static org.monte.media.FormatKeys.EncodingKey;  
 import static org.monte.media.FormatKeys.FrameRateKey;  
 import static org.monte.media.FormatKeys.KeyFrameIntervalKey;  
 import static org.monte.media.FormatKeys.MIME_AVI;  
 import static org.monte.media.FormatKeys.MediaTypeKey;  
 import static org.monte.media.FormatKeys.MimeTypeKey;  
 import static org.monte.media.VideoFormatKeys.CompressorNameKey;  
 import static org.monte.media.VideoFormatKeys.DepthKey;  
 import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;  
 import static org.monte.media.VideoFormatKeys.QualityKey;  
 import java.awt.Dimension;  
 import java.awt.GraphicsConfiguration;  
 import java.awt.GraphicsEnvironment;  
 import java.awt.Rectangle;  
 import java.awt.Toolkit;  
 import java.io.File;  
 import org.monte.media.Format;  
 import org.monte.media.FormatKeys.MediaType;  
 import org.monte.media.math.Rational;  
 import org.monte.screenrecorder.ScreenRecorder;  
 public class Video{  
   public static final String USER_DIR = "user.dir";  
   public static final String DOWNLOADED_FILES_FOLDER = "downloadFiles";  
   private static ScreenRecorder screenRecorder;  
   public static void startRecording(String methodName) throws Exception {  
     File file = new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER);  
     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 SpecializedScreenRecorder(gc, captureSize, new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),  
         new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, 24, FrameRateKey,  
             Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, 15 * 60),  
         new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black", FrameRateKey, Rational.valueOf(30)), null, file, methodName);  
     screenRecorder.start();  
   }  
   public static void stopRecording() throws Exception {  
     screenRecorder.stop();  
   }  
 }  



No comments:

Post a Comment