Search code examples
pythonwindowscommand-linesubprocessvlc

VLC - terminate stream/transcoding interactively via command line on windows/ python / programmatic video capture on windows


I want to use vlc command line tool to capture a video from a usb camera (on Windows!). The Problem is that is not clear, when to stop recording. The aim is to capture the users face, while he is running the program. It is important to have correct timing. I am launching vlc like this:

cmd = ['C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe', 'dshow://', 
'--dshow-size=640x480', '--dshow-adev=None', '--dshow-fps=30', 
'--dshow-vdev=USB_Camera', 
'--sout=#transcode{vcodec=h264,vb=1024,fps=30,width=640,deinterlace}
:standard{access=file,mux=ps,dst=
"path\\to\\dstfile"}', 
'--qt-start-minimized']

p = subprocess.Popen(cmd)

# ... user interaction and stuff ...

# ???? can i do sth. better here ???
p.kill()

The problem with the call to kill is that the transcoding process gets interrupted, which corrupts the video file (it is still playable, but there are dummy frames in the end, and the frame rate / display time doesn't align).

My Question: Is there any clean possibility to terminate the process correctly? Any other signal I tried to use, like

signal.SIGINT
signal.CTRL_C_EVENT
...

failed to terminate vlc, or killed it and thus produced the same corruption (I don't remember every signal I've tried)


Solution

  • Ok, so this answer did the trick to shut down vlc regularly. In my case I also needed to set the muxer in the sout-standard to "ts" in order to get a video with a correct time index, so the command now looks like this:

    cmd = ['C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe', 'dshow://', 
           '--dshow-size=640x480', '--dshow-adev=None', '--dshow-fps=30', 
           '--dshow-vdev=USB_Camera', 
           '--sout=#transcode{vcodec=h264,vb=1024,fps=30,width=640,deinterlace}
           :standard{access=file,mux=ts,dst=
           "path\\to\\dstfile"}', 
           '--qt-start-minimized']
    

    My full solution can be found here:

    https://gist.github.com/knthls/d67f06cbb87f85c4f39ffa2ba2ef66df