10000 Improve fluent style · dreamCodeMan/ffmpeg-python@eb9e827 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb9e827

Browse files
committed
Improve fluent style
1 parent a1c4c9b commit eb9e827

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ ffmpeg.run(stream)
2121
Or if you prefer a fluent interface:
2222
```
2323
import ffmpeg
24-
ffmpeg \
25-
.input('input.mp4') \
26-
.hflip() \
27-
.output('output.mp4') \
24+
(ffmpeg
25+
.input('input.mp4')
26+
.hflip()
27+
.output('output.mp4')
2828
.run()
29+
)
2930
```
3031

3132
## Complex filter graphs
@@ -56,15 +57,16 @@ import ffmpeg
5657
5758
in_file = ffmpeg.input('input.mp4')
5859
overlay_file = ffmpeg.input('overlay.png')
59-
ffmpeg \
60+
(ffmpeg
6061
.concat(
6162
in_file.trim(start_frame=10, end_frame=20),
6263
in_file.trim(start_frame=30, end_frame=40),
63-
) \
64-
.overlay(overlay_file.hflip()) \
65-
.drawbox(50, 50, 120, 120, color='red', thickness=5) \
66-
.output(TEST_OUTPUT_FILE) \
64+
)
65+
.overlay(overlay_file.hflip())
66+
.drawbox(50, 50, 120, 120, color='red', thickness=5)
67+
.output(TEST_OUTPUT_FILE)
6768
.run()
69+
)
6870
```
6971

7072
`ffmpeg-python` takes care of running `ffmpeg` with the command-line arguments that correspond to the above filter diagram, and it's easy to see what's going on and make changes as needed.
@@ -112,11 +114,12 @@ ffmpeg.run(stream)
112114

113115
Or fluently:
114116
```
115-
ffmpeg \
116-
.input('dummy.mp4') \
117-
.filter_('fps', fps=25, round='up') \
118-
.output('dummy2.mp4') \
117+
(ffmpeg
118+
.input('dummy.mp4')
119+
.filter_('fps', fps=25, round='up')
120+
.output('dummy2.mp4')
119121
.run()
122+
)
120123
```
121124

122125
When in doubt, refer to the [existing filters](https://github.com/kkroening/ffmpeg-python/blob/master/ffmpeg/_filters.py) and/or the [official ffmpeg documentation](https://ffmpeg.org/ffmpeg-filters.html).

ffmpeg/tests/test_ffmpeg.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,23 @@ def test_fluent_concat():
4646

4747

4848
def test_fluent_output():
49-
ffmpeg \
50-
.input('dummy.mp4') \
51-
.trim(start_frame=10, end_frame=20) \
49+
(ffmpeg
50+
.input('dummy.mp4')
51+
.trim(start_frame=10, end_frame=20)
5252
.output('dummy2.mp4')
53+
)
5354

5455

5556
def test_fluent_complex_filter():
5657
in_file = ffmpeg.input('dummy.mp4')
57-
return ffmpeg \
58+
return (ffmpeg
5859
.concat(
5960
in_file.trim(start_frame=10, end_frame=20),
6061
in_file.trim(start_frame=30, end_frame=40),
6162
in_file.trim(start_frame=50, end_frame=60)
62-
) \
63+
)
6364
.output('dummy2.mp4')
65+
)
6466

6567

6668
def test_repr():
@@ -86,15 +88,16 @@ def test_get_args_simple():
8688
def _get_complex_filter_example():
8789
in_file = ffmpeg.input(TEST_INPUT_FILE)
8890
overlay_file = ffmpeg.input(TEST_OVERLAY_FILE)
89-
return ffmpeg \
91+
return (ffmpeg
9092
.concat(
9193
in_file.trim(start_frame=10, end_frame=20),
9294
in_file.trim(start_frame=30, end_frame=40),
93-
) \
94-
.overlay(overlay_file.hflip()) \
95-
.drawbox(50, 50, 120, 120, color='red', thickness=5) \
96-
.output(TEST_OUTPUT_FILE) \
95+
)
96+
.overlay(overlay_file.hflip())
97+
.drawbox(50, 50, 120, 120, color='red', thickness=5)
98+
.output(TEST_OUTPUT_FILE)
9799
.overwrite_output()
100+
)
98101

99102

100103
def test_get_args_complex_filter():
@@ -153,10 +156,11 @@ def test_custom_filter():
153156

154157

155158
def test_custom_filter_fluent():
156-
node = ffmpeg \
157-
.input('dummy.mp4') \
158-
.filter_('custom_filter', 'a', 'b', kwarg1='c') \
159+
node = (ffmpeg
160+
.input('dummy.mp4')
161+
.filter_('custom_filter', 'a', 'b', kwarg1='c')
159162
.output('dummy2.mp4')
163+
)
160164
assert node.get_args() == [
161165
'-i', 'dummy.mp4',
162166
'-filter_complex', '[0]custom_filter=a:b:kwarg1=c[v0]',

0 commit comments

Comments
 (0)
0