问题描述
我正在尝试从采集卡录制视频.当我希望程序控制ffmpeg时,我在Java中启动了一个进程,让ffmpeg运行30秒钟,然后通过向该进程发送"q"来关闭它.但是视频只有6秒长.我在代码中找不到任何错误,希望对您有所帮助
I'm trying to record a video from a capture card. As I want my program to control ffmpeg, I started a process within Java, let ffmpeg run for 30 seconds and then shut it down by sending "q" to the process. The video however is only 6 seconds long. I couldn't find anything wrong in my code and would appreciate some help
我的代码:
import java.io.*;
public class FfmpegTest {
public static void main(String[] args) throws Exception {
Process process = Runtime.getRuntime().exec("E:\\ffmpeg\\bin\\ffmpeg.exe -y -f dshow -i video=\"The video card\" bla.mp4");
PrintWriter pw = new PrintWriter(process.getOutputStream(),true);
Thread.sleep(30000);
System.out.println("recording done");
pw.println("q");
Thread.sleep(2000);
}
}
推荐答案
好,我知道了.视频只有6秒长的原因是因为ffmpeg有很多输出,在6秒后就已满.由于输出无处可去,因此它们堵塞了缓冲区,ffmpeg停止记录.
Ok I figured it out. The reason videos are only 6 seconds long was because ffmpeg has a lot of output which was full after 6 seconds. As the output didn't go anywhere, they clogged up the buffers and ffmpeg stopped to record.
要解决此问题,您可以执行以下两项操作之一:
To solve this, you can do one of two things:
- 跨越一个新线程,该线程读取消息以清空流缓冲区
- 使用
-loglevel quiet
禁止显示输出消息.如果您需要在信息流中查找特定的消息,这可能是不需要的
- Span a new thread which reads the messages to empty the stream buffers
- use
-loglevel quiet
to suppress output messages. This might be unwanted if you need to look for specific messages in the streams
这篇关于ffmpeg只录制6秒的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!