使用 vlcj 捕获只会提供损坏的视频文件 [英] Capturing with vlcj only gives a corrupt video file
问题描述
我正在使用 vlcj 在我的 Java 程序中捕获屏幕.因此我使用以下代码:
I am using vlcj to capture the screen in my Java program. Therefore I use the following code:
public static void main(final String[] args) {
NativeLibrary.addSearchPath("vlc", "/Applications/VLC.app/Contents/MacOS/lib/");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CaptureTest().start("screen://");
}
});
}
public CaptureTest() {
factory = new MediaPlayerFactory();
mediaPlayer = (HeadlessMediaPlayer) factory.newMediaPlayer();
}
private void start(String mrl) {
File dir = new File(System.getProperty("user.home"), "Videos");
dir.mkdirs();
DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
String fileName = dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mp4";
String[] options = {
":sout=#transcode{vcodec=h264,acodec=mp4a}:std{mux=mp4,access=file,dst=" + fileName + "}", ":input-slave=screen://"
};
mediaPlayer.playMedia(mrl, options);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mediaPlayer.stop();
mediaPlayer.release();
}
问题是视频输出文件只有4KB,无法播放.谁能帮我?我在 Mac OS 10.6.8 上,我使用 VLC 1.1.12 和 vlcj 1.1.5
The problem is that the video output file is only 4KB and you can't play it. Can anyone help me? I am on Mac OS 10.6.8 and I use VLC 1.1.12 and vlcj 1.1.5
推荐答案
这是您代码中有趣的部分...
Here is the interesting part of your code...
mediaPlayer.playMedia(mrl, options);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mediaPlayer.stop();
mediaPlayer.release();
你为什么播放媒体,睡了5秒钟,然后立即停止?也许,这就是为什么您在屏幕录制过程中得到的文件大小非常小.据我所知,转码速度不够快,所以文件的大小没有立即增加(可能是因为在转码部分发生了缓冲,我猜...)
Why did you play the media, slept for 5 seconds and stopped immediately after that? Maybe, this is why you get a very small file size during the screen recording. From what I notice, the transcode is not so fast enough, so the file is not increasing in size immediately (maybe due to buffering takes place during transcoding part, I guess...)
最好为播放/录制动作和停止动作分别创建一个按钮.
The best is to create a button each for the play/record action and for the stop action.
这篇关于使用 vlcj 捕获只会提供损坏的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!