10000 Support input/output parameters · dreamCodeMan/ffmpeg-python@03f99db · GitHub
[go: up one dir, main page]

Skip to content

Commit 03f99db

Browse files
committed
Support input/output parameters
1 parent 26e7cff commit 03f99db

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

ffmpeg/_ffmpeg.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
)
99

1010

11-
def input(filename):
11+
def input(filename, **kwargs):
1212
"""Input file URL (ffmpeg ``-i`` option)
1313
1414
Official documentation: `Main options <https://ffmpeg.org/ffmpeg.html#Main-options>`__
1515
"""
16-
return InputNode(input.__name__, filename=filename)
16+
kwargs['filename'] = filename
17+
fmt = kwargs.pop('f', None)
18+
if fmt:
19+
assert 'format' not in kwargs, "Can't specify both `format` and `f` kwargs"
20+
kwargs['format'] = fmt
21+
return InputNode(input.__name__, **kwargs)
1722

1823

1924
@operator(node_classes={OutputNode, GlobalNode})
@@ -31,12 +36,17 @@ def merge_outputs(*parent_nodes):
3136

3237

3338
@operator(node_classes={InputNode, FilterNode})
34-
def output(parent_node, filename):
39+
def output(parent_node, filename, **kwargs):
3540
"""Output file URL
3641
3742
Official documentation: `Synopsis <https://ffmpeg.org/ffmpeg.html#Synopsis>`__
3843
"""
39-
return OutputNode([parent_node], output.__name__, filename=filename)
44+
kwargs['filename'] = filename
45+
fmt = kwargs.pop('f', None)
46+
if fmt:
47+
assert 'format' not in kwargs, "Can't specify both `format` and `f` kwargs"
48+
kwargs['format'] = fmt
49+
return OutputNode([parent_node], output.__name__, **kwargs)
4050

4151

4252

ffmpeg/_run.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import unicode_literals
22

3+
from functools import reduce
34
from past.builtins import basestring
5+
import copy
46
import operator as _operator
57
import subprocess as _subprocess
68

@@ -16,15 +18,27 @@
1618
operator,
1719
OutputNode,
1820
)
19-
from functools import reduce
2021

2122
def _get_stream_name(name):
2223
return '[{}]'.format(name)
2324

2425

2526
def _get_input_args(input_node):
2627
if input_node._name == input.__name__:
27-
args = ['-i', input_node._kwargs['filename']]
28+
kwargs = copy.copy(input_node._kwargs)
29+
filename = kwargs.pop('filename')
30+
fmt = kwargs.pop('format', None)
31+
video_size = kwargs.pop('video_size', None)
32+
args = []
33+
if fmt:
34+
args += ['-f', fmt]
35+
if video_size:
36+
args += ['-video_size', '{}x{}'.format(video_size[0], video_size[1])]
37+
for k, v in kwargs.items():
38+
args.append('-{}'.format(k))
39+
if v:
40+
args.append('{}'.format(v))
41+
args += ['-i', filename]
2842
else:
2943
assert False, 'Unsupported input node: {}'.format(input_node)
3044
return args
@@ -78,7 +92,17 @@ def _get_output_args(node, stream_name_map):
7892
if stream_name != '[0]':
7993
args += ['-map', stream_name]
8094
if node._name == output.__name__:
81-
args += [node._kwargs['filename']]
95+
kwargs = copy.copy(node._kwargs)
96+
filename = kwargs.pop('filename')
97+
fmt = kwargs.pop('format', None)
98+
99+
if fmt:
100+
args += ['-f', fmt]
101+
for k, v in kwargs.items():
102+
args.append('-{}'.format(k))
103+
if v:
104+
args.append('{}'.format(v))
105+
args += [filename]
82106
else:
83107
assert False, 'Unsupported output node: {}'.format(node)
84108
return args

ffmpeg/tests/test_ffmpeg.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import pytest
55
import subprocess
6+
import random
67

78

89
TEST_DIR = os.path.dirname(__file__)
@@ -167,3 +168,41 @@ def test_custom_filter_fluent():
167168
'-map', '[v0]',
168169
'dummy2.mp4'
169170
]
171+
172+
173+
def test_pipe():
174+
width = 32
175+
height = 32
176+
frame_size = width * height * 3 # 3 bytes for rgb24
177+
frame_count = 10
178+
start_frame = 2
179+
180+
out = (ffmpeg
181+
.input('pipe:0', format='rawvideo', pixel_format='rgb24', video_size=(width, height), framerate=10)
182+
.trim(start_frame=start_frame)
183+
.output('pipe:1', format='rawvideo')
184+
)
185+
args = out.get_args()
186+
assert args == [
187+
'-f', 'rawvideo',
188+
'-video_size', '{}x{}'.format(width, height),
189+
'-pixel_format', 'rgb24',
190+
'-framerate', '10',
191+
'-i', 'pipe:0',
192+
'-filter_complex',
193+
'[0]trim=start_frame=2[v0]',
194+
'-map', '[v0]',
195+
'-f', 'rawvideo',
196+
'pipe:1'
197+
]
198+
199+
cmd = ['ffmpeg'] + args
200+
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
201+
202+
in_data = bytes(bytearray([random.randint(0,255) for _ in range(frame_size * frame_count)]))
203+
p.stdin.write(in_data)
204+
p.stdin.close()
205+
206+
out_data = p.stdout.read()
207+
assert len(out_data) == frame_size * (frame_count - start_frame)
208+
assert out_data == in_data[start_frame*frame_size:]

0 commit comments

Comments
 (0)
0