-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy path_format.py
174 lines (134 loc) · 4.63 KB
/
_format.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
ffmpeg_streaming.media
~~~~~~~~~~~~
Video and audio formats
:copyright: (c) 2020 by Amin Yazdanpanah.
:website: https://www.aminyazdanpanah.com
:email: [email protected]
:license: MIT, see LICENSE for more details.
"""
import abc
MULTIPLY_BY_ONE = 1
MULTIPLY_BY_TWO = 2
MULTIPLY_BY_FOUR = 4
MULTIPLY_BY_Eight = 8
MULTIPLY_BY_SIXTEEN = 16
MULTIPLY_BY_THIRTY_TWO = 32
def _verify_codecs(codec, codecs):
if codec is None:
return
elif codec not in codecs:
ValueError("The codec is not available!")
else:
return str(codec)
class Format(abc.ABC):
"""
@TODO: add documentation
"""
def __init__(self, video: str, audio: str, **codec_options):
self.video = video
self.audio = audio
self.codec_options = codec_options
@property
def all(self) -> dict:
args = {
'c:v': self.video,
'c:a': self.audio,
}
args.update(self.get_codec_options())
return args
@abc.abstractmethod
def multiply(self) -> int:
pass
@abc.abstractmethod
def get_codec_options(self) -> dict:
pass
class H264(Format):
def __init__(self, video: str = "libx264", audio: str = 'aac', **codec_options):
"""
@TODO: add documentation
"""
videos = ['libx264', 'h264', 'h264_amf', 'h264_nvenc']
audios = ['copy', 'aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
super(H264, self).__init__(_verify_codecs(video, videos), _verify_codecs(audio, audios), **codec_options)
def multiply(self) -> int:
return MULTIPLY_BY_TWO
def get_codec_options(self) -> dict:
"""
set the default value of h264 codec options and update the value with the specified value by user
see https://ffmpeg.org/ffmpeg-codecs.html#Options-28 for more information about options
:return: dict
"""
h264_codec_options = {
'bf': 1,
'keyint_min': 25,
'g': 250,
'sc_threshold': 40
}
h264_codec_options.update(self.codec_options)
return h264_codec_options
class HEVC(Format):
"""
@TODO: add documentation
"""
def __init__(self, video: str = "libx265", audio: str = 'aac', **codec_options):
videos = ['libx265', 'h265']
audios = ['copy', 'aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
super(HEVC, self).__init__(_verify_codecs(video, videos), _verify_codecs(audio, audios), **codec_options)
def multiply(self) -> int:
return MULTIPLY_BY_TWO
def get_codec_options(self) -> dict:
"""
set the default value of hevc(h265) codec options and update the value with the specified value by user
see https://ffmpeg.org/ffmpeg-codecs.html#Options-29 for more information about options
:return: dict
"""
h265_codec_options = {
'keyint_min': 25,
'g': 250,
'sc_threshold': 40
}
h265_codec_options.update(self.codec_options)
return h265_codec_options
class VP9(Format):
"""
@TODO: add documentation
"""
def __init__(self, video: str = "libvpx-vp9", audio: str = 'aac', **codec_options):
videos = ['libvpx', 'libvpx-vp9']
audios = ['copy', 'aac', 'libvo_aacenc', 'libfaac', 'libmp3lame', 'libfdk_aac']
super(VP9, self).__init__(_verify_codecs(video, videos), _verify_codecs(audio, audios), **codec_options)
def multiply(self) -> int:
return MULTIPLY_BY_TWO
def get_codec_options(self) -> dict:
"""
set the default value of vp9 codec options and update the value with the specified value by user
see https://ffmpeg.org/ffmpeg-codecs.html#Options-26 for more information about options
:return: dict
"""
vp9_codec_options = {}
vp9_codec_options.update(self.codec_options)
return vp9_codec_options
class Formats:
@staticmethod
def h264(video: str = "libx264", audio: str = 'aac', **codec_options) -> Format:
"""
@TODO: add documentation
"""
return H264(video, audio, **codec_options)
@staticmethod
def hevc(video: str = "libx265", audio: str = 'aac', **codec_options) -> Format:
"""
@TODO: add documentation
"""
return HEVC(video, audio, **codec_options)
@staticmethod
def vp9(video: str = "libvpx-vp9", audio: str = 'aac', **codec_options) -> Format:
"""
@TODO: add documentation
"""
return VP9(video, audio, **codec_options)
__all__ = [
'Format',
'Formats'
]