首页 > 解决方案 > Java 视频自动重新编码未按预期工作

问题描述

我正在使用 java monte screen recorder jar文件来记录我的屏幕,同时执行 java 代码,

这是我的代码:

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.AWTException;
import java.awt.GraphicsEnvironment;
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.media.FormatKeys.MediaType;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;

public class MonteRecorder extends ScreenRecorder {

    private File movieFolder;
    private String name;

    public MonteRecorder(String name, File movieFolder) throws IOException, AWTException {

        super(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration(),

                 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);
                /* output format for audio - null == no audio */
            this.movieFolder = 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 'at' HH.mm.ss");

        File f = new File(movieFolder, //
                "Scenario - " + name + " - " +dateFormat.format(new Date()) + "."
                        + Registry.getInstance().getExtension(fileFormat));
        return f;
    }



}

文件类型为 .avi 且创建成功。但是当尝试使用 VLC 媒体播放器打开它时,我收到以下错误消息:

此文件不可播放。这可能是因为文件类型不受支持、文件扩展名不正确或文件已损坏。0xc00d36c4

错误信息

注意:此代码在几周前成功运行,但我没有经常测试它。

为了修复它,我尝试将格式参数更改为 MP4 或 quicktime (.mov) 文件,但都无法打开。

如果有人知道另一个可以记录屏幕的 maven 库,那将会很有帮助(带有代码示例)。

标签: javascreen-recording

解决方案


出现问题是因为 o 在屏幕录制结束时确实注意到调用了停止方法。before 使用以下代码开始屏幕录制:

ScreenRecorder screenRecorder = new MonteRecorder(videoName, new File(path));
this.screenRecorder.start();

解决我应该添加的问题

this.screenRecorder.stop();

在场景结束时。

添加后,使用 VLC 媒体播放器按预期开始创建视频。


推荐阅读