As the title says: weird problem. I've been struggling to make this work for a whole day but to no avail. Any help would be deeply appreciated.
# I've tried every one of the below path but still errors: Fontconfig error: Cannot load default config file: No such file: (null)
FONT_LOC = r'C\\:/Users/acer/AppData/Local/Microsoft/Windows/Fonts/jf-openhuninn-1.1.ttf'
FONT_LOC = 'C:/Users/acer/AppData/Local/Microsoft/Windows/Fonts/jf-openhuninn-1.1.ttf'
FONT_LOC = 'C://Users/acer/AppData/Local/Microsoft/Windows/Fonts/jf-openhuninn-1.1.ttf'
duration = '30
subtitle='abc'
comd = [
"ffmpeg",
"-f", "lavfi",
"-i", "color=c=black:s=1280x720:d={duration}".format(duration=duration),
"-vf", f'''drawtext=fontfile="{FONT_LOC}":text='{subtitle}':fontcolor=white:fontsize=24:x=(w-text_w)/2:y=(h-text_h)/2''',
"-c:v", "libx264",
"-t", duration,
"-pix_fmt", "yuv420p",
output_file+'.mp4'
]
print(' '.join(comd))
subprocess.run(comd, check=True, capture_output=True, text=True) # Failed
# The below command runs successfully in the terminal.
# ffmpeg -f lavfi -i color=c=black:s=1280x720:d=2.352000 -vf drawtext=fontfile="C\\://Users/acer/AppData/Local/Microsoft/Windows/Fonts/jf-openhuninn-1.1.ttf":text='abc':fontcolor=white:fontsize=24:x=(w-text_w)/2:y=(h-text_h)/2 -c:v libx264 -t 2.352000 -pix_fmt yuv420p output_Test-0004.mp3.mp4
I expect the subprocess.run() would produce the same result. I've tried all versions of paths (different kind of escaping) but none of the aforementioned one worked.
So I figured it out and would like to post it here in case someone who joined the python ffmpeg journey later than I do. Got stuck and struggle to resolve the issue. I summarize the important point as bullet points and I think it'll solve most annoying escaping problems like this one I encountered.
1.In windows, ffmpeg path should be something like this 'C\\:/Users/acer/AppData/Local/Microsoft/Windows/Fonts/jf-openhuninn-1.1.ttf'
. Double back slash before semi colon and forward slashes afterwards. There are other escaping method using many back slashes 4-6. Didn't work for me as '\U' always being interpreted for special characters...
For my question, simply drop the double quote of the path and it'll work. Namely, the following command with everything remained untouched as aforementioned.
comd = [ "ffmpeg", "-f", "lavfi", "-i", "color=c=black:s=1280x720:d={duration}".format(duration=duration), "-vf", f'''drawtext=fontfile={FONT_LOC}:text='{subtitle}':fontcolor=white:fontsize=24:x=(w-text_w)/2:y=(h-text_h)/2''', "-c:v", "libx264", "-t", duration, "-pix_fmt", "yuv420p", output_file+'.mp4' ]
Or one could use subprocess.run(whole string)
instead of passing a list. This one works too.
Note: If your path contains whitespaces, then the first method will fail. You have to escape the whitespaces or use 2..
conclusion: Quotes in list command have problems and won't work no matter what.