Passed
Push — master ( 7bc1c2...5b857f )
by Amin
03:31
created

ffmpeg_streaming._input.Capture._os_x()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 4
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
        self.options = options
21
        self.video = video
22
23
    def _linux(self):
24
        is_screen = self.options.pop('screen', False)
25
        if is_screen:
26
            cap = 'x11grab'
27
        else:
28
            cap = 'v4l2'
29
30
        return {
31
            'f': cap,
32
            'i': self.video
33
        }
34
35
    def _windows(self):
36
        self.video = 'video=' + str(self.video)
37
        windows_audio = self.options.pop('windows_audio', None)
38
        if windows_audio is not None:
39
            self.video = self.video + ':audio=' + str(windows_audio)
40
41
        return {
42
            'f': 'dshow',
43
            'i': self.video
44
        }
45
46
    def _os_x(self):
47
        return {
48
            'f': 'avfoundation',
49
            'i': self.video
50
        }
51
52
    @staticmethod
53
    def _unknown():
54
        raise OSError("Unreported OS!")
55
56
    def __iter__(self):
57
        yield from getattr(self, '_' + get_os())().items()
58
59
60
def get_from_cloud(cloud: Clouds, options: dict):
61
    save_to = options.pop('save_to', None)
62
    return {
63
        'i': cloud.download(save_to, **options),
64
        'is_tmp': True if save_to is None else False
65
    }
66
67
68
class InputOption(object):
69
    def __init__(self, _input, **options):
70
        self.input_ = _input
71
        self.options = options
72
73
    def __str__(self):
74
        return " ".join(cnv_options_to_args(self._create()))
75
76
    def __iter__(self):
77
        yield from self._create().items()
78
79
    def _create(self):
80
        options = self.options.pop('pre_opts', {'y': None})
81
        is_cap = self.options.pop('capture', False)
82
83
        if isinstance(self.input_, Clouds):
84
            options.update(get_from_cloud(self.input_, self.options))
85
        elif is_cap:
86
            options.update(Capture(self.input_, self.options))
87
        elif isinstance(self.input_, (str, int)):
88
            i_options = {'i': str(self.input_)}
89
            i_options.update(self.options)
90
            options.update(i_options)
91
        else:
92
            raise ValueError("Unknown input!")
93
94
        return options
95
96
97
def input(_input, **options) -> Media:
98
    """Input options (ffmpeg pre_option ``-i`` input options)
99
        You can also pass a cloud object as an input to the method. the file will be downloaded and will pass it to ffmpeg
100
        if you want to open a resource from a pipe, set input "pipe:"
101
        if you want to open a resource from a capture device, pass a device name as filename and set the capture keyword
102
        to True. To list the supported, connected capture devices, see https://trac.ffmpeg.org/wiki/Capture/Webcam
103
         and https://trac.ffmpeg.org/wiki/Capture/Desktop. See https://ffmpeg.org/ffmpeg.html#Main-options and
104
         https://ffmpeg.org/ffmpeg-protocols.html for more information about input option and supported resources
105
         such as http, ftp, and so on.
106
        """
107
    return Media(InputOption(_input, **options))
108
109
110
__all__ = [
111
    'input',
112
]
113