Search code examples
javac++ffmpeghttp-live-streaming

Keep ffmpeg converting images to video when new images are incoming


Is there a way to keep ffmpeg convert images incoming to HLS, and stop whenever I want?

I'm using ffmpeg to convert images to m3u8/ts for streaming. A camera device will send images 24/7, and ffmpeg will convert those when users requested until they stop watching.

For now, I get images from a camera device by Java network, then move those images to ffmpeg input directory and convert those with C++. It's a loop that does this job, so new ffmpeg is executed every time images are moved.

The problem is there are too many ts files leftover, and they're taking a lot of spaces on server. So I tried to delete those with delete_segments option, but since ffmpeg is executed every time, it couldn't delete segments made from ffmpeg right before it.

Here are some ways I consider:

  1. To remove files with another function, without ffmpeg.
  2. Maybe transfer images directly to ffmpeg input, and somehow make ffmpeg keep convert those images to ts. (And I couldn't think of somehow)
  3. Or maybe change the whole structure.

It would be much easier with the option 1, but it would be great if I can do this with ffmpeg's own functions.


Solution

  • You can use an input file list, which can be a special file (e.g. a FIFO pipe).

    Then your command may look like:

    mkfifo mylist.pipe
    ffmpeg -f concat -safe 0 -i mylist.pipe output.mkv
    

    And you can add every image to mylist.pipe, as follows:

    echo "file '/path/to/image.png'"  >mylist.pipe
    echo "file '/path/to/image2.png'" >mylist.pipe
    

    If the paths are relative, you don't need the -safe 0 part.

    See https://trac.ffmpeg.org/wiki/Concatenate#Automaticallyappendingtothelistfile