kkroening / ffmpeg-python

Python bindings for FFmpeg - with complex filtering support
Apache License 2.0
9.88k stars 884 forks source link

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

Open minhnv16 opened 5 years ago

minhnv16 commented 5 years ago

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

Sendery commented 4 years ago

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 commented 4 years ago

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

frankiedrake commented 2 years ago

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

XCanG commented 5 months ago

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.