I'm using a program which continuously writes MPEG-TS video data to a file while it's running. I'm expecting it to run continuously for many days.

I want to use ffmpeg to transcode this video data live. So that the .mts file doesn't grow continuously until I run out of hard drive space, I'm trying to get the first program to write to a named pipe and for ffmpeg to read from that pipe.

I tried doing ffmpeg -i /tmp/test.mts -c:v libx264 test.mp4 but it seems that ffmpeg quits once it reaches the end of the pipe, instead of waiting for new data. For example if I start the program, wait 30 seconds and then run ffmpeg, I'll only get ~50 seconds of video out. (30 seconds + the time it takes ffmpeg to catch up)

I have managed to get it working by doing ffmpeg -i pipe:0 -c:v libx264 test.mp4 < /tmp/test.mts but this feels kind of hacky to me, using stdin to do this. Is there a way I can directly provide the named pipe as an input to ffmpeg and have it wait for new data once it reaches the end of the current data?


  • Why does it feel hacky to put a standerd input into a standerd input field? – Michael Prokopec Nov 22 '18 at 7:20
  • 1
    I don't understand how ffmpeg -i pipe:0 < fifo.mts is different in this regard from ffmpeg -i fifo.mts; both will receive an EOF and exit here when the (last handle to the) writing end of fifo.mts is closed. – mosvy Nov 22 '18 at 8:47

Simply open that fifo for writing (and keep it open) from another place, too. Example:

In a window:

mkfifo /tmp/test.mts
exec 7<>/tmp/test.mts
ffmpeg -i /tmp/test.mts out.mp4

In another window:

cat ... >/tmp/test.mts
cat ... >/tmp/test.mts

The idea is that a reader won't receive an EOF from a pipe until all processes which had it open for writing have closed it:

$ mkfifo /tmp/fifo
$ cat /tmp/fifo &
[1] 26437
$ exec 7>/tmp/fifo
$ echo yes >/tmp/fifo
$ echo yes >/tmp/fifo
$ echo yes >/tmp/fifo
$ exec 7>&-
[1]+  Done                    cat /tmp/fifo

Without the exec 7>/tmp/fifo which keeps an open handle to the writing end of /tmp/fifo, the cat would've terminated after the first echo.

| improve this answer | |
  • The weird thing is, the process that's writing to the fifo doesn't end if I do the ffmpeg -i pipe:0 -c:v libx264 test.mp4 < /tmp/test.mts thing. From tests I've done, the process writing to the fifo exits as soon as ffmpeg exits. – Joshua Walsh Nov 26 '18 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.