【问题标题】:recording screen and webcam to video file [closed]录制屏幕和网络摄像头到视频文件[关闭]
【发布时间】:2013-07-03 02:47:30
【问题描述】:

我正在尝试创建一个应用程序来记录用户屏幕或网络摄像头并保存到视频文件中。

我尝试使用 Java Media Framework 使其工作,但它在 Windows 64 位中失败。 我发现 Java Media Framework 已经很老了,现在不支持了 :(

我的问题是: 有没有其他图书馆可以帮助我们从网络摄像头录制屏幕和视频? 如果不支持 Java,什么语言可以提供帮助?

我发现一些应用程序具有相同的功能: + http://www.screencast-o-matic.com/ + http://www.apowersoft.com/free-online-screen-recorder 用户无需安装即可从服务器下载的java小程序。

谢谢你!

更新: 我在 HTML5 中寻找另一种解决方案

更新:2013/07/05** 我发现 WebRTC 1.0 可以解决这个问题,但 Google Chrome 尚不支持 Media Stream Recorder。

【问题讨论】:

    标签: java webcam jmf video-recording screen-recording


    【解决方案1】:

    我有一个使用 com.xuggle 包记录屏幕的示例代码

    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;
            }
    
        }
    
    }
    

    【讨论】:

    • 我试过这个解决方案,它有点慢而且丑陋。该解决方案已在整个网络上发布,但在我的情况下,视频在 vlc 和 mplayer 上播放效果不佳。我在 Fedora 上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2011-03-24
    相关资源
    最近更新 更多