Improve fluent style

This commit is contained in:
Karl Kroening 2017-06-03 20:08:43 -07:00
parent a1c4c9b04d
commit eb9e8270cd
2 changed files with 33 additions and 26 deletions

View File

@ -21,11 +21,12 @@ ffmpeg.run(stream)
Or if you prefer a fluent interface:
```
import ffmpeg
ffmpeg \
.input('input.mp4') \
.hflip() \
.output('output.mp4') \
(ffmpeg
.input('input.mp4')
.hflip()
.output('output.mp4')
.run()
)
```
## Complex filter graphs
@ -56,15 +57,16 @@ import ffmpeg
in_file = ffmpeg.input('input.mp4')
overlay_file = ffmpeg.input('overlay.png')
ffmpeg \
(ffmpeg
.concat(
in_file.trim(start_frame=10, end_frame=20),
in_file.trim(start_frame=30, end_frame=40),
) \
.overlay(overlay_file.hflip()) \
.drawbox(50, 50, 120, 120, color='red', thickness=5) \
.output(TEST_OUTPUT_FILE) \
)
.overlay(overlay_file.hflip())
.drawbox(50, 50, 120, 120, color='red', thickness=5)
.output(TEST_OUTPUT_FILE)
.run()
)
```
`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)
Or fluently:
```
ffmpeg \
.input('dummy.mp4') \
.filter_('fps', fps=25, round='up') \
.output('dummy2.mp4') \
(ffmpeg
.input('dummy.mp4')
.filter_('fps', fps=25, round='up')
.output('dummy2.mp4')
.run()
)
```
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).

View File

@ -46,21 +46,23 @@ def test_fluent_concat():
def test_fluent_output():
ffmpeg \
.input('dummy.mp4') \
.trim(start_frame=10, end_frame=20) \
(ffmpeg
.input('dummy.mp4')
.trim(start_frame=10, end_frame=20)
.output('dummy2.mp4')
)
def test_fluent_complex_filter():
in_file = ffmpeg.input('dummy.mp4')
return ffmpeg \
return (ffmpeg
.concat(
in_file.trim(start_frame=10, end_frame=20),
in_file.trim(start_frame=30, end_frame=40),
in_file.trim(start_frame=50, end_frame=60)
) \
)
.output('dummy2.mp4')
)
def test_repr():
@ -86,15 +88,16 @@ def test_get_args_simple():
def _get_complex_filter_example():
in_file = ffmpeg.input(TEST_INPUT_FILE)
overlay_file = ffmpeg.input(TEST_OVERLAY_FILE)
return ffmpeg \
return (ffmpeg
.concat(
in_file.trim(start_frame=10, end_frame=20),
in_file.trim(start_frame=30, end_frame=40),
) \
.overlay(overlay_file.hflip()) \
.drawbox(50, 50, 120, 120, color='red', thickness=5) \
.output(TEST_OUTPUT_FILE) \
)
.overlay(overlay_file.hflip())
.drawbox(50, 50, 120, 120, color='red', thickness=5)
.output(TEST_OUTPUT_FILE)
.overwrite_output()
)
def test_get_args_complex_filter():
@ -153,10 +156,11 @@ def test_custom_filter():
def test_custom_filter_fluent():
node = ffmpeg \
.input('dummy.mp4') \
.filter_('custom_filter', 'a', 'b', kwarg1='c') \
node = (ffmpeg
.input('dummy.mp4')
.filter_('custom_filter', 'a', 'b', kwarg1='c')
.output('dummy2.mp4')
)
assert node.get_args() == [
'-i', 'dummy.mp4',
'-filter_complex', '[0]custom_filter=a:b:kwarg1=c[v0]',