您可以尝试使用if-else语句来检查是否存在
codesplit
是否从配置文件传递了选项。
import subprocess
import configparser
config = configparser.ConfigParser(allow_no_value=True)
config.read(r"config.ini")
videofile = config.get("settings", "videofile")
outputfile = config.get("settings", "outputfile")
codesplit = config.get("settings", "codesplit", fallback=None)
ffmpeg_path = r"ffmpeg.exe"
if codesplit:
command = [
f"{ffmpeg_path}",
"-i",
(videofile),
(codesplit),
(outputfile),
]
else:
command = [
f"{ffmpeg_path}",
"-i",
(videofile),
(outputfile),
]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
universal_newlines=True,
)
process.communicate()
print(command)
此代码检查是否
代码分割
配置中有。如果是这样,它会将其添加到
command
列表。如果没有,就没有。
输出看起来像
['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']
其中不存在空字符串。