ffmpeg single quote in drawtext
Asked Answered
M

10

18

I haven't been able to get ffmpeg's drawtext video filter to draw apostrophes/single quotes when they are in drawtext's "text=" parameter, even when I escape them. Double quotes work fine, and apostrophes in text loaded from a file (e.g. textfile="example.txt") work fine. Is this a bug?

e.g.

ffmpeg -i test.mpg -vf drawtext="apostrophes don't print" ...
ffmpeg -i test.mpg -vf drawtext="even when they\'re escaped" ...
Mixie answered 23/5, 2012 at 17:48 Comment(0)
C
39

Special character escapes are like violence: if they're not solving your problem, you're not using enough.

ffmpeg -i test.mpg -vf drawtext=text="It\\\\\'s so easy"

Produces a textual overlay that includes an apostrophe. The text is being parsed a couple times, so you not only have to escape the quote, you also have to escape the slash escaping the quote. Twice.

Your alternative of using a textfile might be a better method in this situation.

Chromium answered 24/5, 2012 at 0:8 Comment(3)
While composing this command line from Java I actually had to code: this.watermarkText = watermarkText.replaceAll(":", "").replaceAll("\'", "\\\\\\\\\\\\'"); which, if you ask me, it totally whack.Respirator
lol, I had the issue that I used single quotes also where this example uses double quotes around the text value. That doesn't work apparently, it needs to be double quotes. Or in my case I use a more complex -vf where I put the whole argument of -vf in double quotes and then don't use any quotes for the drawtext text value. Hope this may help someone. ThanksLarose
I had to use more violence for it to work ffmpeg -i test.mpg -vf drawtext=text="It\\\\\\\'s so easy"Rumen
B
14

I was able to insert Unicode \u2019 into argument string and it worked for single right quote.

Bewray answered 16/11, 2014 at 22:51 Comment(4)
how did you insert that? If i have a variable $string containing the text with the quote, what should I do to get the quote printed?Sather
Filix this is the only way I could make it work using the node tool fluent-ffmpeg. Thanks! :) string.replace(/'/g, "\u2019"); // then feed this to the text property of drawtextAsthenopia
This also worked for me in JavaScript: text = text.replace(/'/g, `\\\\\'`); text = text.replace(/\'/g, "\\" + `\\\\\'`);.Anthonyanthophore
THIS IS SO HELPFULRawley
O
6

// This works for me

public function replaceSpecialFfmpegChars($text)
    {
        return str_replace(
            ":",
            "\\\\\\\\\\\:",
            str_replace(
                "%",
                "\\\\\\\\\\\%",
                str_replace(
                    "'",
                    "'\\\\\\\\\\\\''",
                    str_replace(
                        "\"",
                        "\\\\\\\\\\\"",
                        str_replace(
                            "\\",
                            "\\\\\\\\\\\\\\\\",
                            $text
                        )
                    )
                )
            )
        );
    }
Open answered 18/5, 2020 at 5:39 Comment(2)
Ok, cool and working, thanks!!... but seriously... 😅Progestin
Genius, this is the only thing I could get working.Krimmer
H
5

In case someone needs this for Python, this escape function is working for me (based on the FFmpeg Quoting and Escaping documentation + multi-escaping advice above):

return "'" + text.replace(":", "\\\\:").replace("'", "'\\\\\\\\\\\\''") + "'"
Heave answered 10/11, 2015 at 3:55 Comment(5)
Not sure how this would be working for you it produces it's -> 'it'\\\''s'Thesda
return text.replace(":", "\\:").replace("'", "\\\\\\\\\\\\'")Thesda
It could depend on shell, I am using zsh :)Heave
what would that command be for bash, if I have my string with quote in $string? I'm completely confused as to how many \ I should add, is it 5, is it 12?Sather
You sir, are a lifesaver!Loretaloretta
T
3

Just put your text into a file e.g. myText.txt and use textfile option:

->myText.txt This is my text with special characters :,(,),'

Then instead of using :

ffmpeg -i test.mpg -vf drawtext="This is my text with special characters :,(,),'"

use the following command:

ffmpeg -i test.mpg -vf textfile=textFile.txt

Tildie answered 27/9, 2020 at 16:11 Comment(1)
I am using a bat file with draw text inside a filter_complex, and only this method worked for me, all other method using escape string didn't work for meSubvert
L
2

For Windows Users:

The logic: for easier understanding I will call the single quote to be shown as a result: apostrophe and all other single quotes simply single quotes...

You have to put the first part of the text before the apostrophe between single quotes + 3 backslashes before the apostrophe, then the apostrophe itself + the text after the apostrophe between single quotes again"

So to produce: "apostrophes don't print" it would be:

drawtext=text='apostrophes don'\\\''t print'

Using an example:

ffplay -f lavfi  "color=blue:size=700x200,drawtext=text='apostrophes don'\\\''t print':fontsize=40:fontcolor=white:x=(W-tw)/2:y=(H-th)/2"

enter image description here

The other solution seems to be, to use a text file as already mentioned here by others:

drawtext=textfile=somefile.txt

Lainelainey answered 10/11, 2022 at 14:51 Comment(0)
C
0

This is probably something to do with magic quotes. Through a bunch of testing I just did using Windows Command Line and MinGW on Windows, I ran into the same problem every time. Since ffmpeg drawtext uses Freetype (I would guess this is where magic quotes are enabled) I doubt there's much to be done to disable magic quotes. I'm not sure there's a way to remove the added slashes in the command line either, as everything I've seen involves PHP scripts. I could be wrong, since I'm no PHP guru, but I'm not aware of how to integrate them into an ffmpeg command.

Companion answered 23/5, 2012 at 20:45 Comment(2)
Interesting, doesn't look like there's a solution then! Weird that it is possible to escape double quotes, semicolons etc. but not single quotes though...Mixie
Yeah apostrophes are always the big problem. Whoever came up with magic quotes apparently decided that whenever you used it, it was always going to be an apostrophe in a contraction or to show ownership, where it's curved toward the preceding letter. I never did like magic quotes.Companion
C
0

If you're passing the string via Python, the following works for me:

txt = 'it\'s good!'
txt = txt.replace('\'', '\'\\\\\\\\\\\'\'')
Chuffy answered 17/2, 2021 at 12:46 Comment(0)
M
0

in node.js, i am able to put a colon in the text with this line.

let surahTxt = "QURAN " + chapterNo + "\\:"+ from + "-" + to ;

later this variable is used with the drawtext

Mordancy answered 1/11, 2021 at 7:54 Comment(0)
B
0

ricardo-bohner's methods works really well, but just make sure that, you're getting exactly the as shown below:

I used this;

line = it's
str(line.replace("'", "'\\\\\\''"))

but due to the way I code some functions in the executed command, I got '\\\'' like below:

it'\\\''s
Bourke answered 28/9, 2023 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.