How to stream from webcam while simultaneously saving the stream to a local file using ffmpeg? [duplicate]

This command works perfectly for saving a webcam stream to a file:

ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0  out.avi

How would I simultaneously display this captured stream on my computer screen?


Solution 1:

Use the tee muxer:

ffmpeg -f v4l2 -i /dev/video0 -map 0 -c:v libx264 -f tee "output.mp4|[f=nut]pipe:" | ffplay pipe:

Solution 2:

I came here looking for the same command and the one from @llogan works, but with no audio. This is the command I started to use to capture with video in good quality and with audio.

ffmpeg -f v4l2 \
    -framerate 30 \
    -video_size 1024x768 \
    -input_format mjpeg \
    -i /dev/video0 \
    -f alsa \
    -i hw:2,0 \
    -c:a pcm_s16le \
    -c:v mjpeg \
    -b:v 64000k \
    -f tee \
    -map 0:v \
    -map 1:a \
    "output.avi|[f=nut]pipe:" | \
    ffplay -an pipe:

-an: is for not play the audio (but the audio is recorded in file). It would make a feedback with the mic and start a infinite and increasing echo.

Source: Answer from @llogan and ffmpeg Documentation.