简体   繁体   English

在 Python 应用程序中使用 Raspberry Pi 4B 终端命令

[英]Use Raspberry Pi 4B Terminal Command in Python Application

I am currently working on a small Python Application which turns a Raspberry Pi 4B into a 48 Channel Audio-Recorder.我目前正在开发一个小型 Python 应用程序,它将 Raspberry Pi 4B 变成 48 通道录音机。 Basics work, but during Recording, I need a log file which tells me when recording started, which ALSA warnings occurred and when recording stopped.基础工作,但在录制过程中,我需要一个日志文件来告诉我何时开始录制、发生了哪些 ALSA 警告以及何时停止录制。

The recorder can be started with this terminal command:可以使用以下终端命令启动记录器:

pi@raspberrypi:~ $ rec -q -t caf --endian little --buffer 96000 -c 48 -b 24 /home/pi/myssd-one/Aufnahmen/test.caf 2>&1 | tee /home/pi/myssd-one/Aufnahmen/logging.log

this records audio in the test.caf file and writes ALSA warnings to logging.log So far so good.这会在 test.caf 文件中记录音频并将 ALSA 警告写入 logging.log 到目前为止一切顺利。

The Python Program (which should run on a touchscreen with GUI so recording can easily started and stopped) takes care of variable audio-filenames (date-time-stamp) and controls an LED to show that recording is running. Python 程序(应该在带有 GUI 的触摸屏上运行,以便轻松开始和停止录制)负责可变音频文件名(日期时间戳)并控制 LED 以显示录制正在运行。

This part of the code takes care of switching on and off:这部分代码负责打开和关闭:

#!/usr/bin/env python

from tkinter import *
import shlex
import os
import subprocess
import tkinter.font
import datetime
from gpiozero import LED
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.OUT)

def ledToggle():
    if led.is_lit:
        led.off()
        my_env = os.environ.copy()
        my_env['AUDIODRIVER'] = 'alsa'
        my_env['AUDIODEV'] = 'hw:KTUSB,0'
        ledButton["text"] = "Turn Recorder on"
        print ("recorder stops")
        subprocess.Popen(['sudo', 'pkill', '-SIGINT', 'rec'],  env = my_env, shell = FALSE, stdout=subprocess.PIPE)
    else:
        led.on()
        my_env = os.environ.copy()
        my_env['AUDIODRIVER'] = 'alsa'
        my_env['AUDIODEV'] = 'hw:KTUSB,0'
        ledButton["text"] = "Turn Recorder off"
        print ("recorder starts")
        ##reference statement command line: "rec -q -t caf --endian little --buffer 96000 -c 48 -b 24 /home/pi/myssd-one/Aufnahmen/test.caf 2>&1 | tee /home/pi/myssd-one/Aufnahmen/logging.log"
        command_line = shlex.split("rec '-q' '-t' 'caf' '--buffer' '96000' '-c 48' '-b 24' '/home/pi/myssd-one/Aufnahmen/test.caf' '"2>&1 | tee"' '/home/pi/myssd-one/Aufnahmen/logging.log'")
        p1 = subprocess.Popen(command_line, env = my_env, shell = False, stdout=subprocess.PIPE)

I am trying to move the original command line statement into the subprocess.Popen command, to no success yet.我正在尝试将原始命令行语句移动到 subprocess.Popen 命令中,但尚未成功。 The part where routing to the log file is done, fails.完成路由到日志文件的部分失败。 It looks as the initiating sox-application 'rec' tries to interpret it as part of its own parameter list, instead of interpreting it as a redirection of stdout and stderr to the log file.看起来启动的 sox 应用程序“rec”试图将其解释为它自己的参数列表的一部分,而不是将其解释为 stdout 和 stderr 到日志文件的重定向。 I appreciate some guidance in this issue.我感谢在这个问题上的一些指导。 Variable Filenames for audio files is already done, but for simplicity taken out of this code snippet.音频文件的变量文件名已经完成,但为了简单起见,从这个代码片段中取出。

Thanks Mark, I dived into this command line along your hint that it only can run with shell=True and this implied that it had to be written as a full statement without separating commas and escape quotes.谢谢马克,我根据你的提示深入研究了这个命令行,它只能在 shell=True 下运行,这意味着它必须写成一个完整的语句,而不用分隔逗号和转义引号。 Now it works.现在它起作用了。 Actually, the shlex.split() becomes obsolete.实际上, shlex.split() 已经过时了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM