I want to read the Settings.ini file when adding more values with some empty values
config.ini
[settings]
videofile = video.avi
codesplit = -vn
outputfile = audio.mp3
codec = copy
Output
['ffmpeg.exe', '-i', 'video.avi', '-vn', 'copy', 'audio.mp3']
f you make the value "codesplit" and "codec" empty, the code will It works well. example : To convert a video avi file to video mp4
[settings]
videofile = video.avi
codesplit =
outputfile = videoaudio.mp4
codec =
Output
['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']
If you want to make with one value empty "codesplit" or "copy" , the code does not work.
[settings]
videofile = video.avi
codesplit =
outputfile = videoaudio.mp4
codec = copy
or
[settings]
videofile = video.avi
codesplit = -vn
outputfile = audio.mp3
codec =
Output
['ffmpeg.exe', '-i', 'video.avi', 'videoaudio.mp4']
This is what I want
['ffmpeg.exe', '-i', 'video.avi', 'copy', 'videoaudio.mp4']
['ffmpeg.exe', '-i', 'video.avi', '-vn', 'audio.mp3']
full code
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 adding a statement "and"
if (codesplit) and (codec) :
Nothing happens
Rather than having a simple if
else
you should build the command piece by piece based on the optional values. Something like:
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)
Using the above Python script I jget the following results from the four sample config.ini
files:
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']