8000 Add `filter` operator · Powercoder64/ffmpeg-python@3cf993e · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3cf993e

Browse files
committed
Add filter operator
1 parent 217bd2b commit 3cf993e

File tree

8 files changed

+31
-22
lines changed

8 files changed

+31
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Alternatively, standard python help is available, such as at the python REPL pro
122122
Don't see the filter you're looking for? `ffmpeg-python` includes shorthand notation for some of the most commonly used filters (such as `concat`), but it's easy to use any arbitrary ffmpeg filter:
123123
```python
124124
stream = ffmpeg.input('dummy.mp4')
125-
stream = ffmpeg.filter_(stream, 'fps', fps=25, round='up')
125+
stream = ffmpeg.filter(stream, 'fps', fps=25, round='up')
126126
stream = ffmpeg.output(stream, 'dummy2.mp4')
127127
ffmpeg.run(stream)
128128
```
@@ -132,7 +132,7 @@ Or fluently:
132132
(
133133
ffmpeg
134134
.input('dummy.mp4')
135-
.filter_('fps', fps=25, round='up')
135+
.filter('fps', fps=25, round='up')
136136
.output('dummy2.mp4')
137137
.run()
138138
)

examples/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ height = int(video_stream['height'])
1717
(
1818
ffmpeg
1919
.input(in_filename, ss=time)
20-
.filter_('scale', width, -1)
20+
.filter('scale', width, -1)
2121
.output(out_filename, vframes=1)
2222
.run()
2323
)
@@ -49,7 +49,7 @@ video = (
4949
out, _ = (
5050
ffmpeg
5151
.input(in_filename)
52-
.filter_('select', 'gte(n,{})'.format(frame_num))
52+
.filter('select', 'gte(n,{})'.format(frame_num))
5353
.output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
5454
.run(capture_output=True)
5555
)
@@ -89,8 +89,8 @@ With additional filtering:
8989
(
9090
ffmpeg
9191
.input('/path/to/jpegs/*.jpg', pattern_type='glob', framerate=25)
92-
.filter_('deflicker', mode='pm', size=10)
93-
.filter_('scale', size='hd1080', force_original_aspect_ratio='increase')
92+
.filter('deflicker', mode='pm', size=10)
93+
.filter('scale', size='hd1080', force_original_aspect_ratio='increase')
9494
.output('movie.mp4', crf=20, preset='slower', movflags='faststart', pix_fmt='yuv420p')
9595
.view(filename='filter_graph')
9696
.run()
@@ -106,11 +106,11 @@ in1 = ffmpeg.input('in1.mp4')
106106
in2 = ffmpeg.input('in2.mp4')
107107
v1 = in1['v'].hflip()
108108
a1 = in1['a']
109-
v2 = in2['v'].filter_('reverse').filter_('hue', s=0)
110-
a2 = in2['a'].filter_('areverse').filter_('aphaser')
109+
v2 = in2['v'].filter('reverse').filter('hue', s=0)
110+
a2 = in2['a'].filter('areverse').filter('aphaser')
111111
joined = ffmpeg.concat(v1, a1, v2, a2, v=1, a=1).node
112112
v3 = joined[0]
113-
a3 = joined[1].filter_('volume', 0.8)
113+
a3 = joined[1].filter('volume', 0.8)
114114
out = ffmpeg.output(v3, a3, 'out.mp4')
115115
out.run()
116116
```

examples/get_video_thumbnail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def generate_thumbnail(in_filename, out_filename, time, width):
2020
(
2121
ffmpeg
2222
.input(in_filename, ss=time)
23-
.filter_('scale', width, -1)
23+
.filter('scale', width, -1)
2424
.output(out_filename, vframes=1)
2525
.overwrite_output()
2626
.run(capture_stdout=True, capture_stderr=True)

examples/read_frame_as_jpeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read_frame_as_jpeg(in_filename, frame_num):
1515
out, err = (
1616
ffmpeg
1717
.input(in_filename)
18-
.filter_('select', 'gte(n,{})'.format(frame_num))
18+
.filter('select', 'gte(n,{})'.format(frame_num))
1919
.output('pipe:', vframes=1, format='image2', vcodec='mjpeg')
2020
.run(capture_stdout=True)
2121
)

examples/split_silence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_chunk_times(in_filename, silence_threshold, silence_duration, start_time
5050
p = _logged_popen(
5151
(ffmpeg
5252
.input(in_filename, **input_kwargs)
53-
.filter_('silencedetect', n='{}dB'.format(silence_threshold), d=silence_duration)
53+
.filter('silencedetect', n='{}dB'.format(silence_threshold), d=silence_duration)
5454
.output('-', format='null')
5555
.compile()
5656
) + ['-nostats'], # FIXME: use .nostats() once it's implemented in ffmpeg-python.

ffmpeg/_filters.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def filter_multi_output(stream_spec, filter_name, *args, **kwargs):
2525

2626

2727
@filter_operator()
28-
def filter_(stream_spec, filter_name, *args, **kwargs):
28+
def filter(stream_spec, filter_name, *args, **kwargs):
2929
"""Apply custom filter.
3030
3131
``filter_`` is normally used by higher-level filter functions such as ``hflip``, but if a filter implementation
@@ -42,11 +42,19 @@ def filter_(stream_spec, filter_name, *args, **kwargs):
4242
4343
Example:
4444
45-
``ffmpeg.input('in.mp4').filter_('hflip').output('out.mp4').run()``
45+
``ffmpeg.input('in.mp4').filter('hflip').output('out.mp4').run()``
4646
"""
4747
return filter_multi_output(stream_spec, filter_name, *args, **kwargs).stream()
4848

4949

50+
@filter_operator()
51+
def filter_(stream_spec, filter_name, *args, **kwargs):
52+
"""Alternate name for ``filter``, so as to not collide with the
53+
built-in python ``filter`` operator.
54+
"""
55+
return filter(stream_spec, filter_name, *args, **kwargs)
56+
57+
5058
@filter_operator()
5159
def split(stream):
5260
return FilterNode(stream, split.__name__)
@@ -343,7 +351,7 @@ def drawtext(stream, text=None, x=0, y=0, escape_text=True, **kwargs):
343351
kwargs['x'] = x
344352
if y != 0:
345353
kwargs['y'] = y
346-
return filter_(stream, drawtext.__name__, **kwargs)
354+
return filter(stream, drawtext.__name__, **kwargs)
347355

348356

349357
@filter_operator()
@@ -432,6 +440,7 @@ def colorchannelmixer(stream, *args, **kwargs):
432440
'crop',
433441
'drawbox',
434442
'drawtext',
443+
'filter',
435444
'filter_',
436445
'filter_multi_output',
437446
'hflip',

ffmpeg/nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __getitem__(self, index):
5353
Process the audio and video portions of a stream independently::
5454
5555
input = ffmpeg.input('in.mp4')
56-
audio = input[:'a'].filter_("aecho", 0.8, 0.9, 1000, 0.3)
56+
audio = input[:'a'].filter("aecho", 0.8, 0.9, 1000, 0.3)
5757
video = input[:'v'].hflip()
5858
out = ffmpeg.output(audio, video, 'out.mp4')
5959
"""
@@ -141,7 +141,7 @@ def __getitem__(self, item):
141141
Process the audio and video portions of a stream independently::
142142
143143
input = ffmpeg.input('in.mp4')
144-
audio = input[:'a'].filter_("aecho", 0.8, 0.9, 1000, 0.3)
144+
audio = input[:'a'].filter("aecho", 0.8, 0.9, 1000, 0.3)
145145
video = input[:'v'].hflip()
146146
out = ffmpeg.output(audio, video, 'out.mp4')
147147
"""

ffmpeg/tests/test_ffmpeg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_combined_output():
174174
def test_filter_with_selector():
175175
i = ffmpeg.input(TEST_INPUT_FILE1)
176176
v1 = i['v'].hflip()
177-
a1 = i['a'].filter_('aecho', 0.8, 0.9, 1000, 0.3)
177+
a1 = i['a'].filter('aecho', 0.8, 0.9, 1000, 0.3)
178178
out = ffmpeg.output(a1, v1, TEST_OUTPUT_FILE1)
179179
assert out.get_args() == [
180180
'-i', TEST_INPUT_FILE1,
@@ -214,8 +214,8 @@ def _get_complex_filter_asplit_example():
214214

215215
return (ffmpeg
216216
.concat(
217-
split0.filter_('atrim', start=10, end=20),
218-
split1.filter_('atrim', start=30, end=40),
217+
split0.filter('atrim', start=10, end=20),
218+
split1.filter('atrim', start=30, end=40),
219219
)
220220
.output(TEST_OUTPUT_FILE1)
221221
.overwrite_output()
@@ -484,7 +484,7 @@ def test__run__dummy_cmd_list():
484484

485485
def test__filter__custom():
486486
stream = ffmpeg.input('dummy.mp4')
487-
stream = ffmpeg.filter_(stream, 'custom_filter', 'a', 'b', kwarg1='c')
487+
stream = ffmpeg.filter(stream, 'custom_filter', 'a', 'b', kwarg1='c')
488488
stream = ffmpeg.output(stream, 'dummy2.mp4')
489489
assert stream.get_args() == [
490490
'-i', 'dummy.mp4',
@@ -497,7 +497,7 @@ def test__filter__custom():
497497
def test__filter__custom_fluent():
498498
stream = (ffmpeg
499499
.input('dummy.mp4')
500-
.filter_('custom_filter', 'a', 'b', kwarg1='c')
500+
.filter('custom_filter', 'a', 'b', kwarg1='c')
501501
.output('dummy2.mp4')
502502
)
503503
assert stream.get_args() == [

0 commit comments

Comments
 (0)
0