user6972
user6972

Reputation: 911

How to apply 2 filters drawtext and drawbox using FFMPEG

I'm having problems combining filters. I'm trying to take video from the camera, apply a timer on it and also overlay a box in the center. I can put a time code (local time and pts) using the -vf drawtext command no problems:

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 \
-vf "drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: \
text='%{localtime} %{pts\:hms}':  fontcolor=white: fontsize=24: box=1: \
[email protected]: boxborderw=5: x=0: y=0" -vcodec libx264 \
-preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

Then I have one that draws a small box using drawbox:

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 \
-filter_complex " drawbox=x=iw/2:y=0:w=10:h=ih:[email protected]": \
-vcodec libx264 -preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

I assumed I could combine these with the filter_complex switch and separate them using the semicolon like this

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 -filter_complex "drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: text='%{localtime} %{pts\:hms}':  fontcolor=white: fontsize=24: box=1: [email protected];drawbox=x=iw/2:y=0:w=10:h=ih:[email protected]": -vcodec libx264 -preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

But it fails to find the input stream on the second filter:

Input #0, video4linux2,v4l2, from '/dev/video0':

Duration: N/A, start: 10651.720690, bitrate: N/A

Stream #0:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, -5 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc

Cannot find a matching stream for unlabeled input pad 0 on filter Parsed_drawbox_1

I tried to direct it to [0] like this:

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 -filter_complex " \
drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: \
text='%{localtime} %{pts\:hms}':  fontcolor=white: fontsize=24: box=1: \
[email protected];[0] drawbox=x=iw/2:y=0:w=10:h=ih:[email protected]": \
-vcodec libx264 -preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

But it fails to put the box on the output.

So I tried to split streams like this

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 -filter_complex " \
split [main][tmp];\
[main] drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: \
text='%{localtime} %{pts\:hms}':  fontcolor=white: fontsize=24: box=1: [email protected] [tmp];\
[main] drawbox=x=iw/2:y=0:w=10:h=ih:[email protected] [tmp2]; [tmp][tmp2] overlay": \
-vcodec libx264 -preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

But my build doesn't have the overlay filter complied with it. At this point I decided to stop and ask if I'm making this harder than it should be. The end result is I just want a timer and a box drawn on the video. Is there a better way or a formatting trick to do this?

Thanks

Upvotes: 3

Views: 6452

Answers (1)

Gyan
Gyan

Reputation: 93058

You can just apply them one after the another, by separating via a comma.

ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 -filter_complex " \
drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: \
text='%{localtime} %{pts\:hms}':  fontcolor=white: fontsize=24: box=1: \
[email protected], drawbox=x=iw/2:y=0:w=10:h=ih:[email protected]": \
-vcodec libx264 -preset ultrafast -f mp4 -pix_fmt yuv420p -y output.mp4

Upvotes: 7

Related Questions