Search code examples
javaffmpegvideo-capture

ffmpeg only records 6 seconds of video


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

My code:

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);
    }
}

Solution

  • 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:

    1. Span a new thread which reads the messages to empty the stream buffers
    2. use -loglevel quiet to suppress output messages. This might be unwanted if you need to look for specific messages in the streams