python-ffmpeg
python-ffmpeg copied to clipboard
Keyboard interrupt not working on Windows
Hello,
I have found that keyboard interrupt does not work on Windows (I haven't tested it on other platforms).
What I found is that concurrent.futures.wait() does not return on the keyboard interrupt, making it necessary to introduce a timeout so the system can process the exception.
In ffmpeg.py execute() I tried changing:
done, pending = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION)
self._executed = False
to:
pending = futures
try:
while pending:
done, pending = concurrent.futures.wait(
futures,
timeout=1,
return_when=concurrent.futures.FIRST_EXCEPTION,
)
except KeyboardInterrupt as e:
self.terminate()
executor.shutdown(wait=True, cancel_futures=True)
raise e
self._executed = False
With that change, keyboard interrupt works as expected.
https://github.com/jonghwanhyeon/python-ffmpeg/assets/1588996/22111705-23f7-4b58-931d-c189b7afc7e0
It seems working on my environment:
- Windows 11
- Python 3.12
(The response time seems to be a bit slow. A Keyboard Interrupt occurs 1-2 seconds after pressing Control C.)
Interesting, I wonder if there is something else going on in my environment -- I do have multiple thread pools running (multiple ffmpeg in parallel with progress). I have not seen keyboard interrupt work on Windows 10 or 11 with python 3.11.
Wondering if this is related to this issue as my ffmpeg is infinitely hanging unless I press ENTER on my keyboard. Then it continues.
Progress(frame=0, fps=0.0, size=2400256, time=datetime.timedelta(seconds=11), bitrate=1745.1, speed=1860.0)
If instead of ENTER I hit CTRL+C the code is hanging at the same location.
done, pending = concurrent.futures.wait(futures, return_when=concurrent.futures.FIRST_EXCEPTION)
And oddly enough if I run my same code through PyTest it never hangs.
Edit: Seems to be related to issuing the exact same command with the same files. My Pytest suite was handling multiple different files but running the code from my main function was trying to handle the same file. No ffmpeg process was still running still. Just issuing a different command to call ffmpeg again seems to fix the issue with whatever process (that I can't see listed anywhere) is holding the files and hanging ffmpeg. No idea why hitting ENTER fixed it either though. I'd guess its something with windows and how this library is handling the pass off to ffmpeg.
Edit2: Having pyautogui.press('enter') even stops the hang if I connect to ffmpeg.on("progress"). Very odd behavior.
It's just a guess but it sounds like ffmpeg is waiting for input from you. I'd suggest trying ffmpeg options of "-y" (answer yes to any questions) and possibly "-nostdin".