首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将屏幕和摄像头记录到视频文件

将屏幕和摄像头记录到视频文件
EN

Stack Overflow用户
提问于 2013-07-03 02:47:30
回答 1查看 3.7K关注 0票数 2

我试图创建一个应用程序,以记录用户的屏幕或从摄像头,并保存到视频文件。

我尝试过来使它工作,但是在Windows 64位中它失败了。我发现很老了,现在不支持它:

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

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

谢谢你的进阶!

更新:--我正在HTML5中找到另一个解决方案

更新: 2013/07/05**我发现WebRTC 1.0可以达到解决方案,但Google还没有支持媒体流记录。

EN

回答 1

Stack Overflow用户

发布于 2013-07-26 07:11:05

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

代码语言:javascript
复制
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;
        }

    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17438874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档