How to record video automatically with multicopter

I want to record a video while hovering and stop recording while moving automatically.
I hope that both modes such as mission mode and manual mode like position flight mode will be applied.
I recognized that the trigger can be activated at every way-point in the mission planning menu. But what I want is to activate the trigger to record the image when hovering is detected, and to turn off the recording when the drone is detected to move.

How can I apply this feature?

Not sure how to do it if you want to run it on the Pixhawk. But if you have a companion computer it is very simple, make a program using MAVSDK to read if the drone is loitering or moving and turn on/off the camera from there.

Thanks for your answer!

Then, is it eough to use raspberry pi as companion computer?

Yes a Raspberry pi works perfectly as a companion computer, just install Ubuntu server on it with Ros and Mavlink and you are good to go.