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
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)
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']