-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy path_input.py
155 lines (124 loc) · 4.23 KB
/
_input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
ffmpeg_streaming.input
~~~~~~~~~~~~
Input options
:copyright: (c) 2020 by Amin Yazdanpanah.
:website: https://www.aminyazdanpanah.com
:email: [email protected]
:license: MIT, see LICENSE for more details.
"""
from ffmpeg_streaming._media import Media
from ffmpeg_streaming._utiles import get_os, cnv_options_to_args
from ffmpeg_streaming._clouds import Clouds
cloud = None
class Capture(object):
def __init__(self, video, options):
"""
@TODO: add documentation
"""
self.options = options
self.video = video
def _linux(self):
cap = 'x11grab' if (is_screen := self.options.pop('screen', False)) else 'v4l2'
return {
'f': cap,
'i': self.video
}
def _windows(self):
self.video = f'video={str(self.video)}'
windows_audio = self.options.pop('windows_audio', None)
if windows_audio is not None:
self.video = f'{self.video}:audio={str(windows_audio)}'
return {
'f': 'dshow',
'i': self.video
}
def _os_x(self):
return {
'f': 'avfoundation',
'i': self.video
}
@staticmethod
def _unknown():
raise OSError("Unreported OS!")
def __iter__(self):
yield from getattr(self, f'_{get_os()}')().items()
def get_from_cloud(_cloud: Clouds, options: dict):
"""
@TODO: add documentation
"""
global cloud
if cloud is None:
save_to = options.pop('save_to', None)
cloud = {'i': _cloud.download(save_to, **options), 'is_tmp': save_to is None}
return cloud
class InputOption(object):
def __init__(self, _input, **options):
"""
@TODO: add documentation
"""
self.input_ = _input
self.options = options
def __str__(self):
"""
@TODO: add documentation
"""
return " ".join(cnv_options_to_args(self._create()))
def __iter__(self):
"""
@TODO: add documentation
"""
yield from self._create().items()
def _create(self):
"""
@TODO: add documentation
"""
options = self.options.pop('pre_opts', {'y': None})
is_cap = self.options.pop('capture', False)
if isinstance(self.input_, Clouds):
options.update(get_from_cloud(self.input_, self.options))
elif is_cap:
options.update(Capture(self.input_, self.options))
elif isinstance(self.input_, (str, int)):
i_options = {'i': str(self.input_)}
i_options.update(self.options)
options.update(i_options)
else:
raise ValueError("Unknown input!")
return options
class Input:
def __init__(self, _input: InputOption):
"""
@TODO: add documentation
"""
self.inputs = [_input]
def input(self, _input, **options):
"""
@TODO: add documentation
"""
self.inputs.append(InputOption(_input, **options))
def __getattr__(self, name):
"""
@TODO: add documentation
"""
def method(*args, **kwargs):
media = Media(self)
if hasattr(media, name):
return getattr(media, name)(*args, **kwargs)
else:
raise AttributeError("The object has no attribute {}".format(name))
return method
def input(_input, **options) -> Input:
"""Input options (ffmpeg pre_option ``-i`` input options)
You can also pass a cloud object as an input to the method. the file will be downloaded and will pass it to ffmpeg
if you want to open a resource from a pipe, set input "pipe:"
if you want to open a resource from a capture device, pass a device name as filename and set the capture keyword
to True. To list the supported, connected capture devices, see https://trac.ffmpeg.org/wiki/Capture/Webcam
and https://trac.ffmpeg.org/wiki/Capture/Desktop. See https://ffmpeg.org/ffmpeg.html#Main-options and
https://ffmpeg.org/ffmpeg-protocols.html for more information about input option and supported resources
such as http, ftp, and so on.
"""
return Input(InputOption(_input, **options))
__all__ = [
'input',
]