1

Hey guys so im trying to use ffmpeg-python to add text to a video but its crashing at

[Parsed_drawtext_0 @ 0000012ea115ee80] Setting 'text' to value 'hi'

and I'm not sure what I can do to fix this problem. I tried pure FFmpeg command-line style and still the same issue where it gets stuck at Setting text to value and it simply outputs a 0byte mp4 after crashing.

My code:

os.environ['FONTCONFIG_FILE'] = r'C:\Users\NOP\NOP\NOP\NOP\binaries\fonts\fonts.conf'
    os.environ['FONTCONFIG_PATH'] = r'C:\Users\NOP\NOP\NOP\NOP\binaries\fonts'
    os.environ['FC_CONFIG_DIR'] = r'C:\Users\NOP\NOP\NOP\NOP\binaries\fonts'
    in_ = ffmpeg.input(output_video_logo)
    in_ = in_.drawtext(text='hi')
    ffmpeg.output(in_, output_video).global_args('-loglevel', 'debug').run(cmd=FFMPEG_PATH)

Font config file:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>

<!--
    DO NOT EDIT THIS FILE.
    IT WILL BE REPLACED WHEN FONTCONFIG IS UPDATED.
    LOCAL CHANGES BELONG IN 'local.conf'.

    The intent of this standard configuration file is to be adequate for
    most environments.  If you have a reasonably normal environment and
    have found problems with this configuration, they are probably
    things that others will also want fixed.  Please submit any
    problems to the fontconfig bugzilla system located at fontconfig.org

    Note that the normal 'make install' procedure for fontconfig is to
    replace any existing fonts.conf file with the new version.  Place
    any local customizations in local.conf which this file references.

    Keith Packard
-->

<!-- Font directory list -->

    <dir>WINDOWSFONTDIR</dir>
    <dir>~/fonts</dir>
    <dir prefix="cwd">.</dir>
    <dir>~/.fonts</dir>

<!--
  Accept deprecated 'mono' alias, replacing it with 'monospace'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>mono</string>
        </test>
        <edit name="family" mode="assign">
            <string>monospace</string>
        </edit>
    </match>

<!--
  Accept alternate 'sans serif' spelling, replacing it with 'sans-serif'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>sans serif</string>
        </test>
        <edit name="family" mode="assign">
            <string>sans-serif</string>
        </edit>
    </match>

<!--
  Accept deprecated 'sans' alias, replacing it with 'sans-serif'
-->
    <match target="pattern">
        <test qual="any" name="family">
            <string>sans</string>
        </test>
        <edit name="family" mode="assign">
            <string>sans-serif</string>
        </edit>
    </match>

<!--
  Load local system customization file
-->
    <include ignore_missing="yes">conf.d</include>

<!-- Font cache directory list -->

    <cachedir>WINDOWSTEMPDIR_FONTCONFIG_CACHE</cachedir>
    <cachedir>~/.fontconfig</cachedir>

    <config>

<!--
  Rescan configuration every 30 seconds when FcFontSetList is called
 -->
        <rescan>
            <int>30</int>
        </rescan>
    </config>

</fontconfig>

Is this a issue with any of my fontconfig or script? I'm really lost on fixing this.

As requested by Rotem tried adding new input to drawtext and this was the output before crashing with no error message: Successfully opened the file.

[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'box' to value '1'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'boxcolor' to value 'yellow'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'fontcolor' to value 'blue'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'fontsize' to value '72'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'text' to value 'hi'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'x' to value '10'
[Parsed_drawtext_0 @ 00000171a2f0f900] Setting 'y' to value '10'

Full log: https://pastebin.com/E6sHvwUz

DeadSec
  • 808
  • 1
  • 12
  • 38
  • No error message? Try larger font and different color: `in_ = in_.drawtext(text='hi', box=1, fontcolor='blue', boxcolor='yellow', fontsize=72, x=10, y=10)` – Rotem Feb 10 '22 at 16:54
  • @Rotem added those inputs still crashed, added some info in the post – DeadSec Feb 10 '22 at 17:47
  • I think you are posting the wrong information. Why are you setting `os.environ`? What is the value of `output_video_logo`? What is the value of `output_video`? What is the value of `FFMPEG_PATH`? Are you deleting the output existing file, before execution or getting a question like `File 'output.mp4' already exists. Overwrite? [y/N]`? Why are you posting font config file, but not posting the log of FFmpeg (I suppose the reason for the crush may be in FFmpeg printing)? – Rotem Feb 10 '22 at 19:40
  • In case you can't see the report, you may also create a log file by adding `-report`: `ffmpeg.output(in_, output_video).global_args('-loglevel', 'debug', '-report').overwrite_output().run(cmd=FFMPEG_PATH)`. You may post the content of the log file (at least the relevant parts). – Rotem Feb 10 '22 at 19:42
  • @Rotem the setting of env variables is because FFmpeg requires a font config file and fonts file to work, the output_video_logo contains the path to the video that is working as input, FFMPEG_PATH is the path for the ffmpeg executable. – DeadSec Feb 10 '22 at 21:43
  • @Rotem log added to post – DeadSec Feb 10 '22 at 21:47
  • 1
    FFmpeg version `N-104465-g08a501946f-20211104` is a nightly built version, and not a stable release. Try replacing `ffmpeg.exe` with a stable release. – Rotem Feb 10 '22 at 22:01

1 Answers1

0

tldr; the config file worked for a few hours then broke, my advice is to skip it and just write out the font path, taking into consideration that you may have to mess around to find the correct level of escaping.

For me ffmpeg wouldn't alter output files or would create files with 0kb.

-report flag generated reports that just ended abruptly with no error msg.

Embarrasingly, it took me a while to realize it was crashing, which lead me here, the info you provided was actually the key figuring this out! So thank you OP!

I noticed you have the same config setup as I did (more or less), with EnvVar

DO NOT EDIT THIS FILE.

...yada yada yada...

Keith Packard

I deleted the EnvVar but that threw a no config file error again but after messing around with the escaping, this worked:

>C:\Users\*user*\Desktop>

ffmpeg -f lavfi -i 
"color=color=black:s=1920x1080:duration=10" -vf 
"drawtext=fontfile=C\\:/ffmpeg/fonts/arial.ttf:text='Stack Overflow'
:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2" -preset 
veryfast -an -tune stillimage "C:\\Users\\*user*\\Desktop\\output.mkv" -y

inline:

ffmpeg -f lavfi -i "color=color=black:s=1920x1080:duration=10" -vf "drawtext=fontfile=C\\:/ffmpeg/fonts/arial.ttf:text='Stack Overflow':fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2" -preset veryfast -an -tune stillimage "C:\\Users\\*user*\\Desktop\\output.mkv" -y

As you can see I'm operating out of desktop, you can quickly acheive this by opening desktop in exporer and changing the address to cmd. Secondly, for clarity, *user* should be replaced with your username.

lastly, as stated in the docs

This string contains the ' special escaping character, and the : special character, so it needs to be escaped in this way:

text=this is a \'string\'\: may contain one, or more, special characters

A second level of escaping is required when embedding the filter description in a filtergraph description, in order to escape all the filtergraph special characters. Thus the example above becomes:

drawtext=text=this is a \\\'string\\\'\\: may contain one\, or more\, special characters

(note that in addition to the ' escaping special characters, also , needs to be escaped).

Finally an additional level of escaping is needed when writing the filtergraph description in a shell command, which depends on the escaping rules of the adopted shell. For example, assuming that \ is special and needs to be escaped with another , the previous string will finally result in:

-vf "drawtext=text=this is a \\\\\\'string\\\\\\'\\\\: may contain one\\, or more\\, special characters"

Note that they probably must also be escaped as the value for the text option in the filter argument string and as the filter argument in the filtergraph description, and possibly also for the shell, that makes up to four levels of escaping; using a text file with the textfile option avoids these problems.

and from this stack post

If you are trying to get the font using script/program, you should additionally escape backslashes. This path worked for me:

const FONT_FILE = 'C\\\\:/Windows/Fonts/arial.ttf';

edit: As for the fontfile=C\\:/ffmpeg/fonts/arial.ttf I simply copied the .ttf file from C:\windows\fonts with ctrl+c and pasted into my ffmpeg folder but I suspect you can simply reference the C:\windows\fonts instead.

In summary, the config file worked for a few hours then broke, my advice is to skip it and just write out the font path, taking into consideration that you may have to mess around to find the correct level of escaping.