如果或更多,则两个

问题描述 投票:0回答:1
我想在添加更多值的值时读取setting.ini文件。

config.ini [settings] videofile = video.avi codesplit = -vn outputfile = audio.mp3 codec = copy

输出

['ffmpeg.exe', '-i', 'video.avi', '-vn', 'copy', 'audio.mp3']

f您使值“ codessplit”和“ codec”为空,代码将效果很好。示例:将视频AVI文件转换为视频MP4

[settings] videofile = video.avi codesplit = outputfile = videoaudio.mp4 codec =

输出

['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']

如果您想用一个值为空的“代码”或“复制”,则代码不起作用。
[settings] videofile = video.avi codesplit = outputfile = videoaudio.mp4 codec = copy

or
[settings]
videofile = video.avi
codesplit = -vn
outputfile = audio.mp3
codec = 

输出
['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']

这就是我想要的
['ffmpeg.exe', '-i', 'video.avi', 'copy', 'videoaudio.mp4']

['ffmpeg.exe', '-i', 'video.avi', '-vn', 'audio.mp3']

Full代码
import subprocess
import configparser


config = configparser.ConfigParser(allow_no_value=True)
config.read(r"Settings.ini")
videofile = config.get("settings", "videofile")
outputfile = config.get("settings", "outputfile")
codesplit = config.get("settings", "codesplit", fallback=None)
codec = config.get("settings", "codec", fallback=None)

ffmpeg_path = r"ffmpeg.exe"

if codesplit and codec :
    command = [
        f"{ffmpeg_path}",
        "-i",
        (videofile),
        (codesplit),
        (codec),
        (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)

tried添加一个语句“和”

IF(codesplit)和(编解码):

没有发生的事情

比起拥有简单的

if

else
python if-statement
1个回答
0
投票

import subprocess import configparser config = configparser.ConfigParser(allow_no_value=True) config.read(r"Settings.ini") videofile = config.get("settings", "videofile") outputfile = config.get("settings", "outputfile") codesplit = config.get("settings", "codesplit", fallback=None) codec = config.get("settings", "codec", fallback=None) ffmpeg_path = r"ffmpeg.exe" command = [ ffmpeg_path, "-i", videofile ] if codesplit: command.append(codesplit) if codec: command.append(codec) command.append(outputfile) # lines commented out for testing # # process = subprocess.Popen( # command, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # text=True, # bufsize=1, # universal_newlines=True, # ) # process.communicate() print(command)

使用上面的Python脚本我jget从四个示例
config.ini
文件中产生以下结果:

C:\Users\test>python test.py
['ffmpeg.exe', '-i', 'video.avi', '-vn', 'copy', 'audio.mp3']

C:\Users\test>python test.py
['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']

C:\Users\test>python test.py
['ffmpeg.exe', '-i', 'video.avi', 'copy', 'videoaudio.mp4']

C:\Users\test>python test.py
['ffmpeg.exe', '-i', 'video.avi', '-vn', 'audio.mp3']


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.