从命令行切换摄影机的开/关

2024-10-05 10:10:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用PythonOpenCV录制进程的视频,并希望能够通过命令行输入打开/关闭相机。目前,我使用命令行输入打开相机,但必须按某个键停止视频馈送并保存视频,这并不理想

我尝试使用pyautogui模块使用第二个程序触发第一个视频录制脚本结束录制所需的按键,但它不起作用

如果有可能让程序A开始录制并继续录制,直到它被程序B触发,这将实现我想要做的。睡眠方法不起作用,因为进程长度变化很大

理想情况下,要启动的输入如下所示:

camera_recording_program.exe --save C:/path --camera 1

要停止的输入如下所示:

camera_stop_program.exe --end True

我尝试过各种组合,包括使用python输入和在命令行上运行子流程,但没有任何运气。我无法让这两个程序相互通信。此外,该程序是用python编写的,将使用PyInstaller转换为exe文件

谢谢大家!


Tags: 模块方法命令行程序脚本视频进程情况
1条回答
网友
1楼 · 发布于 2024-10-05 10:10:13

试试这个:

from subprocess import check_call

check_call(["pkill", "-f", "filetostop.py"])

或者你也可以试试this

import os,signal
import time
 
def get_now_time():
    # Get current local time
    now_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    return now_time
 
def kill(pid):
    print('pid',pid)
    # pgid=os.getpgid(pid)
    # print(pgid)
    # a = os.killpg(pgid,signal.SIGKILL)
    a = os.kill(pid,signal.SIGKILL)
         Print('The process that killed pid to %s, the return value is: %s' % (pid, a))
 
def kill_target(target):
    cmd_run="ps aux | grep {}".format(target)
    out=os.popen(cmd_run).read()
    for line in out.splitlines():
        print(line)
                 If 'also judges the path where the kill is going' in line:
            pid = int(line.split()[1])
            kill(pid)
 # It is recommended to add the & symbol after the run command, it seems to be running in another thread
os.system('python ./run.py &') 
       
while True:
         # shut down 
    if some_condition:
                 Print('pause')
        kill_target('run.py')
         # turn on
    if some_else_condition:
        os.system('python ./run.py &')

这里还有一些选项:How to stop another already running script in python?https://www.raspberrypi.org/forums/viewtopic.php?t=245623

相关问题 更多 >

    热门问题