-
Notifications
You must be signed in to change notification settings - Fork 922
Escape terminator characters in filter arguments #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from __future__ import unicode_literals | ||
|
||
from builtins import object | ||
from past.builtins import basestring | ||
import hashlib | ||
import json | ||
|
||
|
@@ -51,8 +52,21 @@ class FilterNode(Node): | |
"""FilterNode""" | ||
def _get_filter(self): | ||
params_text = self._name | ||
arg_params = ['{}'.format(arg) for arg in self._args] | ||
kwarg_params = ['{}={}'.format(k, self._kwargs[k]) for k in sorted(self._kwargs)] | ||
|
||
# Helper function to escape uncomfortable characters | ||
def escape_chars(seq, keys): | ||
for k in keys: | ||
if not isinstance(seq[k], basestring): | ||
continue | ||
for ch in "[]=;:,": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also escape |
||
seq[k] = seq[k].replace(ch, "\\\\"+ch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
return seq | ||
|
||
_args = escape_chars(self._args[:], range(len(self._args))) | ||
_kwargs = escape_chars(self._kwargs.copy(), self._kwargs.keys()) | ||
|
||
arg_params = ['{}'.format(arg) for arg in _args] | ||
kwarg_params = ['{}={}'.format(k, _kwargs[k]) for k in sorted(_kwargs)] | ||
params = arg_params + kwarg_params | ||
if params: | ||
params_text += '={}'.format(':'.join(params)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we pull this out to a global function we can put a test around it:
Then
FilterNode._get_filter
becomes..Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I ran a few scenarios against
ffmpeg
usingdrawtext
, andffmpeg
's string unescaping is super bizarre. Some chars only need a single backslash before them, some require two backslashes, and some require eight backslashes.Here's an example that draws the string
a\b
:Yup, eight backslashes.
I found this stackoverflow article talking about it:
Ahaha.
Anyways I'm gonna try to see if there's any method to the madness and whether there's a way to completely correctly escape. It's nice to have this done in a tool like
ffmpeg-python
so that hopefully nobody else has to deal with ffmpeg's insanity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OMG this is crazy. In fact I did notice that I only needed one backslash when running it on the shell, and two with python, which resulted in 4 backslashes.
I'm currently running my code in my testing branch and I'm keeping it for the moment as it works for timestamps (which are basically the only thing that needs escaping in our project, though we could avoid it using
SS.fraction
only).Let me know if I should commit the change you proposed or if you're going to do it some other way.
If what the StackOverflow article says is right, we should do a few real-world tests with ffmpeg, find out how many backslashes are required for a particular filter or character, then make an intelligent escaper out of these numbers. The escaper would escape characters in
trim
arguments withr'\\'
,text
argument ofdrawtext
withr'\\\
, etc., or fallback to some default.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my science results:
I was thinking something like this:
And then another
escape_chars
for rendering thetext
param ofdrawtext
. I think this produces similar results to what's above. But need more scienceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there's any way to encode unicode chars in ffmpeg args. You'd think there would be, since people might want to render Chinese text over a video