2

I'm trying to pause a video playback started with FFPLAY through a python subprocess. You can do this manually by pressing the "p" key on the keyboard while the video is playing. I'd like to emulate this behavior through a python call.

I'm now sending a "p" string, encoded as bytes, through the stdin of the Popen call. The video starts and I can pause it with the keyboard but the communicate command doesn't seem to do anything.

import subprocess
import time

proc = subprocess.Popen(['ffplay', 'PATH_TO_'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        )
time.sleep(2) # just waiting to make sure playback has started
proc.communicate(input="p".encode())[0]

Thanks in advance!

  • 1
    I found the [following post](https://stackoverflow.com/questions/49744471/sending-keystrokes-to-subprocess-using-pythons-popen) that explains why it's not working. I think it's not possible to pause FFplay by writing to to stdin. There may be OS specific solutions. – Rotem Mar 04 '23 at 22:46
  • thanks! diving further, it indeed seems that FFPLAY doesn’t listen to stdin. A hacky solution is to use the keyboard module or pyautogui. It works for me but seems pretty stupid. The thing I like about FFplay is its speed. I’m trying to simply move frame by frame on a signal to control the speed of a video and sync multiple raspberry pis. OpenCv with python turns out to be too slow, same issue with vlc. Any suggestion? – ChienMouille Mar 05 '23 at 11:01
  • I don't understand the architecture of your system, so I can't suggest anything. – Rotem Mar 05 '23 at 14:37
  • what would you need to know? It's on raspbian which is basically a light version of ubuntu I beleive. Signal between devices goes through hardware GPIOS which I can listen to from python. My main question is: do you see any way to send the same signal that the OS sends to FFPLAY when detecting a keyboard stroke, but from python, and without emulating an actual keyboard stroke (which is leaking over any other open application)? – ChienMouille Mar 05 '23 at 14:43
  • I thought you are asking for advice about solving it without using FFplay. Without emulating an actual keyboard stroke? FFplay is also responding to mouse events - there is an option to make the window in focus, and emulating a mouse click. Is it better than emulating a keyboard stroke? I don't think I can help you with that. – Rotem Mar 05 '23 at 15:51
  • I'm interested either in an a way to communicate properly with FFplay process, or an alternative. Mouse clicks emulation would be pretty similar to keystroke. Thanks for pointing it out though. – ChienMouille Mar 05 '23 at 18:35
  • When `ffplay` starts up get the pid. To pause send the stop signal to the pid. To resume play send the continue signal to the pid. This has worked reliably for years. – WinEunuuchs2Unix Jul 04 '23 at 19:10

0 Answers0