java - 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 库,那将会很有帮助(带有代码示例)。
解决方案
出现问题是因为 o 在屏幕录制结束时确实注意到调用了停止方法。before 使用以下代码开始屏幕录制:
ScreenRecorder screenRecorder = new MonteRecorder(videoName, new File(path));
this.screenRecorder.start();
解决我应该添加的问题
this.screenRecorder.stop();
在场景结束时。
添加后,使用 VLC 媒体播放器按预期开始创建视频。
推荐阅读
- javascript - 只运行一次 JavaScript 函数
- android - Android ListView 无法循环滚动条中的不可见项
- asp.net-core - 如何在 ASP.NET CORE MVC 中使用 IExceptionHandler?
- python - keras层中的线性代数
- java - 在编译时或运行时生成 Java 字符串
- sql - 我正在寻找 dups
- spring - Spring Integration DSL 过滤器与带有单个收件人和 DefaultOutputToParentFlow 的 RouteToRecipients
- python - 为什么 Python 列表初始化方法会影响索引?
- html - 我如何只检查 1 个 div 块而不检查其他任何内容
- ios - 如何使用字典中的键值对为表格视图单元格提供标题?