ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

how to stop stream (normal is press Q to stop )

Open minhnv16 opened this issue 6 years ago • 4 comments

when ffmpeg stream some input and show option press [q] to stop how to implement in code. thankyou

minhnv16 avatar Jan 16 '19 09:01 minhnv16

#This work for me: and took me a lot of time to find a proper way to stop gracefully the ffmpeg process#

import ffmpeg, time

#Setup for recording windows desktop to mp4 file
process = (
			ffmpeg
			.input(format='gdigrab',framerate=25,filename="desktop")
			.output(crf="0",preset="ultrafast",filename="./output.mp4",c= "libx264" )
			.overwrite_output()
			)
#Launch video recording
process = process.run_async(pipe_stdin=True)


#Stop video recording
process.communicate(str.encode("q")) #Equivalent to send a Q

# To be sure that the process ends I wait 3 seconds and then terminate de process (wich is more like kill -9)
time.sleep(3) 
process.terminate()

Sendery avatar Jan 07 '20 23:01 Sendery

Please consider add a wrapper/function to stop the process, or at least add some notes one this line to the readme or api documentation @kkroening Anyway Thank you very much

Sendery avatar Jan 07 '20 23:01 Sendery

So it comes that using this library is not much different from creating some wrapper function around the Popen and ffmpeg command, and even more it doesn't require you to sort out how to specify your needs via the library. Anyway, thank you for your job, but letting us get rid of direct Popen communication would make it really beautiful and helpful

frankiedrake avatar Apr 22 '22 11:04 frankiedrake

I spent a lot of hours on this, but find simpler solution from this post (new method) https://stackoverflow.com/a/60795888/3673470

So I'm starting process like that (all imports from subprocess):

process = Popen(cmd, stdin=PIPE, creationflags=CREATE_NEW_PROCESS_GROUP)

and when I'm closing it with that signal gracefully:

process.send_signal(signal.CTRL_BREAK_EVENT)

This is literally 2 lines, while other methods require external files / executables / etc.

XCanG avatar Apr 12 '24 22:04 XCanG