Passed
Push — master ( 6ab7ae...6f6dda )
by Amin
03:33
created

ffmpeg_streaming._input.get_from_cloud()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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