ffmpeg how to record and preview at the same time
Asked Answered
S

2

6

I want to capture video+audio from directshow device like webcam and stream it to RTMP server. This part no problem. But the problem is that I want to be able to see the preview of it. After a lot of search someone said pipe the input using tee muxer to ffplay. but I couldn't make it work. Here is my code for streaming to rtmp server. how should I change it?

ffmpeg -rtbufsize 8196k -framerate 25 -f dshow -i video="Microsoft® LifeCam Studio(TM)":audio="Desktop Microphone (Microsoft® LifeCam Studio(TM))" -vcodec libx264 -acodec aac -strict -2 -b:v 1024k -b:a 128k -ar 48000 -s 720x576 -f flv "rtmp://ip-address-of-my-server/live/out"
Succinylsulfathiazole answered 4/9, 2017 at 6:27 Comment(2)
ffmpeg.org/ffmpeg-formats.html#teeHanschen
Thanks, finally I made it workSuccinylsulfathiazole
S
4

Here is the final code I used and it works.

ffmpeg -rtbufsize 8196k -framerate 25 -f dshow -i video="Microsoft® LifeCam Studio(TM)":audio="Desktop Microphone (Microsoft® LifeCam Studio(TM))" -vcodec libx264 -acodec aac -strict -2 -f tee -map 0:v -map 0:a "[f=flv]rtmp://ip-address-and-path|[f=nut]pipe:" | ffplay pipe:
Succinylsulfathiazole answered 5/9, 2017 at 6:44 Comment(2)
Yes I'm using an old build. Is there a better way instead of nut?Succinylsulfathiazole
What is the actual core command that you needed? E.g. the canonical answer for those interested.Rubi
L
1

The core command for those running ffmpeg on a Unix-compatible system (e.g. MacOS, BSD and GNU-Linux) is really quite simple. It's to redirect or to "pipe" one of the outputs of ffmpeg to ffplay. The main problem here is that ffmpeg cannot autodetect the media format (or container) if the output doesn't have a recognizable file extension such as .avi or .mkv.

Therefore you should specify the format with the option -f. You can list the available choices for option -f with the ffmpeg -formats command.

In the following GNU/Linux command example, we record from an input source named /dev/video0 (possibly a webcam). The input source can also be a regular file.

ffmpeg -i /dev/video0 -f matroska - filename.mkv | ffplay -i -

A less ambiguous way of writing this for non-Unix users would be to use the special output specifier pipe.

ffmpeg -i /dev/video0 -f matroska pipe:1 filename.mkv | ffplay -i pipe:0

The above commands should be enough to produce a preview. But to make sure that you get the video and audio quality you want, you also need to specify, among other things, the audio and video codecs.

ffmpeg -i /dev/video -c:v copy -c:a copy -f matroska - filename.mkv | ffplay -i -

If you choose a slow codec like Google's AV1, you'd still get a preview, but one that stutters.

Landmark answered 28/10, 2022 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.