This is not ffmpeg related. We query the jpg url after sending 24 times M400 to be sure printer has stopped moving. Then start making snapshot in separate thread and send firmware to wait 700ms G4 P700. Can you verify if both commands do what is expected on ultimaker? You said there is a pause, so G4 seems to work.
To test M400 send some long slow moves and then
M400
M114
If you see coordinates in log before move is finished M400 was ignored.
We send 24 M400 to flood input buffer so if we receive a ok we know moves are through. This means ultimaker buffer must be smaller then 24 * Nxxx M40023 which will be at least 13 byte so 2413 = 312 byte. Not sure how much Ultimaker buffers, but marlin does normally only 127 byte in input queue. Also it is more important that host would not send them according to his queue size, so guess size is not the problem. Only if M400 does wait for end of moves or not. More exactly server checks marlin.xml and sends this command
<command type="waitEndOfMoves">M400</command>
so you can even modify it if does not work.