0

In my Python script I intercept KeyboardInterrupts to ensure that the script can finish what its doing before shutting down. However I've found that FFMPEG, which I'm using by triggering commandline commands, immediately ends its current prosessing, before then leading into my script's shutdown processes.

The code below shows my KeyboardInterrupt handling system.

prepareToStop = 0
while prepareToStop == 0:
    t = threading.Thread(target=main_loop, args=[instanceID])
    try:
        t.start()
        t.join() # wait for the threading task to end
    except KeyboardInterrupt:
        prepareToStop = 1

And the code below creates and runs the FFMPEG commands:

command = "\"" + ffmpeg + "\" -y -i " + "uploaded/" + videofile_name + " -c:v " + codec + " " + commandDimensions + " " + commandCompression + " -c:a copy" + commandAspect + " "+str(outputFilePath) #bash command to proccess file
print("Running command: " + command) 
res = os.system(command) #run command 

What I'm looking for is a way to ensure that FFMPEG ignores KeyboardInterrupts, so that the system can handle shutdown process after it has completed.

Edit:

I've identified that this is an issue with FFMPEG. I need it to ignore external commands while processing. I've seen examples suggesting that using '-nostdin' can do this, however it appeared to have no effect when I added it to the command. I intended on posting about this as a seperate question, but my account is apparently restricted.

Straxan
  • 13
  • 4
  • Does this answer your question? [Handling keyboard interrupt when using subproccess](https://stackoverflow.com/questions/23826695/handling-keyboard-interrupt-when-using-subproccess) – ekon Mar 03 '23 at 18:13
  • @ekon It looks like it may be relevant, though I'm not sure how sure how it would be adapted to my system. As you can see, my code is creating a new thread to run a function from the same file, whereas it looks like the code you linked is running a script from a seperate file. Can the 'start_new_session' code from the linked solution be used with the threading in my code? – Straxan Mar 03 '23 at 19:30
  • @ekon I feel that this is likely an FFMPEG-specific issue, as I've tried a few different solutions, but FFMPEG still stops and shows the 'exiting normally received signal 2' message. – Straxan Mar 03 '23 at 20:09
  • What OS do you use? – ekon Mar 03 '23 at 20:24
  • @ekon I'm on windows 10. I already tried to implement the code that uses 'signal masks' from [this thread](https://stackoverflow.com/questions/52763508/python-prevent-child-threads-from-being-affected-from-sigint-signal), before realising that they are not supported on windows – Straxan Mar 03 '23 at 20:32
  • https://stackoverflow.com/questions/43092371/ignore-sigint-in-python-multiprocessing-subprocess might be useful – ekon Mar 03 '23 at 20:43

0 Answers0