Sunday, February 3, 2019

FFMpeg Dynamic Adjustment of Filters


FFMpeg has a full array of video and audio filters, specify the right parameters and it produces pure magic.  The filter scalars can readily be specified as filter static parameters or in some cases based on time.  But, what if you wish to dynamically modify filter parameters dynamically or in real-time?  When compiled with ZeroMQ (0MQ) support, some filters can be adjusted in real-time by sending filter commands vi 0MQ.

The 0MQ support is optional, not configured by the default configuration, so it likely requires building Ffmpeg from source and configuring it for ZeroMQ support.  The build procedure takes the form of a typical autoconf; configure, make, make install.  Refer to my previous post for building on Ubuntu which includes instructions for adding package dependencies and building with what my common feature set; Building FFMpeg.  The --enable-libzmq configure flag enables ZeroMQ based filter commands.  It also requires installation of ZeroMQ development libraries pre-compilation (also found in the instructions).

Not all FFMpeg filters accept ZeroMQ commands, the ones that do are documented in the documentation; FFMpeg Filters, look for 'This filter supports the following commands'.

It's best to start by setting up your command line sequence, then update it to account for ZeroMQ command inputs.  The FFMpeg documentation indicates the hue filter supports ZeroMQ commands; http://ffmpeg.org/ffmpeg-filters.html#Commands-14

10.90.2 Commands

This filter supports the following commands:
b
s
h
H
Modify the hue and/or the saturation and/or brightness of the input video. The command accepts the same syntax of the corresponding option.
If the specified expression is not valid, it is kept at its current value.

The following command applies the hue filter with h=90, s=1 and plays the video after the filter has been applied;

$ ffmpeg -loglevel debug -i /tmp/foo.mp4 -filter_complex "hue=h=90:s=1" -vcodec libx264 -f mpegts - | ffplay -

To apply filter commands via ZeroMQ you need to:
1) know the internal filter name of the pipeline
2) add ZeroMQ input to the filter
3) send the command via zmqsend command

We specifically added debug logging to our FFMpeg command so we could learn the name of the internal filter; Parsed_hue_1
[Parsed_hue_1 @ 0x3a1e600] H:0.5*PI h:90.0 s:1.0 b:0 t:11.9 n:357

Let's add ZeroMQ input to our filter, note the slight modification to our previous command;
$ ffmpeg -loglevel debug -i /tmp/foo.mp4 -filter_complex "zmq,hue=h=90:s=1" -vcodec libx264 -f mpegts - | ffplay -

Lastly, re-run the above command and within a new terminal send a hue filter parameter update;
$ echo Parsed_hue_1 h 50 | zmqsend
$ echo Parsed_hue_1 s 3 | zmqsend

After sending commands to adjust the h and s parameters you should see the video change.

Whelp, that's about all I've got.  While I've on-and-off looked at ZeroMQ integration with FFMpeg on a few occasions over the past years I've never found any solid documentation.  Hopefully this will help set you on your way.  I'll likely post more as I go.

Cheers.


1 comment:

  1. I made an account through this weird platform just to say thank you so much for making this! I've been trying to figure out how to live-edit ffmpeg streams for the past two weeks and finally found this!

    ReplyDelete