0

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)

knthls
  • 41
  • 7
  • If anybody has a better Idea how to capture video from a webcam programmatically (i.e. launch/terminate from script), you're welcome. – knthls Jan 27 '17 at 14:46
  • Try using the [rc interface](https://wiki.videolan.org/Documentation:Modules/rc) with a socket. I have an example in [this answer](http://stackoverflow.com/a/8111621/205580). – Eryk Sun Jan 27 '17 at 16:33
  • 1
    Windows doesn't have signals, so you can only send `CTRL_C_EVENT` and `CTRL_BREAK_EVENT` to a process group in the console to which you're attached -- or all processes attached to the console (including your own) by targeting group 0. When you create a process you can assign it as the lead of a new group via the creation flag `CREATE_NEW_PROCESS_GROUP`, which is only meaningful if it's a console process -- or later on allocates a console. This creation flag disables Ctrl+C in the process, so unless it manually enables it all you can reliably send is Ctrl+Break. – Eryk Sun Jan 27 '17 at 16:41
  • @eryksun your solution with the rc_interface is really cool. Thx for that. I adapted it for my project like this: https://gist.github.com/knthls/29f46a2699daea02afe7e389b311b452 It solves the Problem that VLC terminates in a regular way. But I get the weirdes error message I've ever seen: [0049445c] core interface error: read error: No error And the videos it produces still don't have a correct time index, so probably my assumption, that this comes from not terminating vlc exactly was wrong. – knthls Jan 31 '17 at 13:55
  • Ok, setting the muxer in sout to ps did the trick – knthls Jan 31 '17 at 14:25

2 Answers2

1

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

Community
  • 1
  • 1
knthls
  • 41
  • 7
0

Best answer with mux=ts, without this magic param no way we could use VLC in command line to save file. I add a case when you want to save RTSP stream from IP camera to file

vlc -vvv -Idummy "rtsp://admin:yourpassword@192.168.1.12:554/onvif1" --sout #transcode{vcodec=h264,scale=Auto,acodec=none,scodec=none}:standard{access=file,mux=ts,dst="D:\\mov.mp4"}" 

and after for example 10 minute, you want to stop, go with

taskkill /f /IM "vlc.exe"

Using above command line will make VLC into a NVR to record IP camera PS: Decode, encode require too many advance knowledge, while we just want to apply to our simple case. So it is best If someone could make a summary case of VLC usage and make a github page

Zacharias
  • 78
  • 1
  • 1
  • 6