-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.py
executable file
·221 lines (199 loc) · 7.81 KB
/
youtube.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
YouTube recommended upload encoding settings for ffmpeg
https://support.google.com/youtube/answer/1722171
"""
import sys
import os.path
import json
import argparse
#from pprint import pprint, pformat
from mymediainfo import MyMediaInfo
from lib import run_cmd, ENCODERS
import enc_hevc_vaapi
class YouTube:
DEFAULT_YT_CATEGORY = 'HDR'
DEFAULT_CRF = '18'
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='in_file', required=True, type=str,
help='Input file')
parser.add_argument('-o', dest='out_file', required=True, type=str,
help='Output file')
parser.add_argument('--yt-cat', dest='yt_cat', type=str,
default=self.DEFAULT_YT_CATEGORY,
help='YouTube category (default: %(default)s)')
parser.add_argument('--crf', default=self.DEFAULT_CRF, help='CRF')
parser.add_argument('--enc', default='sw',
help='Encoder (%(default)s)')
parser.add_argument('--opencl', action='store_true')
parser.add_argument('--deint', action='store_true',
help='deinterlace')
parser.add_argument('--preset', default='slow',
help='preset (%(default)s)')
parser.add_argument('-t', dest='duration')
parser.add_argument('--dry', action='store_true',
help='Dry run')
self.args = parser.parse_args()
if not self.args.enc in ENCODERS:
sys.exit(f"Bad encoder '{self.args.enc}', use one of {ENCODERS}")
script_dir = os.path.dirname(os.path.abspath(__file__))
conf_path = os.path.join(script_dir, 'youtube.json')
with open(conf_path, 'r', encoding="utf-8") as file:
self.conf = json.load(file)
self.mi = MyMediaInfo(self.args.in_file)
self.mi.print()
self.get_params()
# frame rate, list of frame rates, list of bit rates
def estimate_bitrate(self, frame_rate, fr_list, br_list):
# print(f'estimate_bitrate frame_rate:{frame_rate} fr_list:{fr_list} br_list:{br_list}')
frame_rate1 = fr_list[0]
bitrate1 = br_list[0]
frame_rate2 = fr_list[-1]
bitrate2 = br_list[-1]
estimated_bitrate = (
bitrate1 +
((bitrate2 - bitrate1) / (frame_rate2 - frame_rate1)) *
(frame_rate - frame_rate1)
)
return int(estimated_bitrate)
def get_params(self):
height_p = f'{self.mi.height}p'
frame_rate = int(float(self.mi.frame_rate))
for mode in self.conf[self.args.yt_cat]:
if frame_rate in mode['FrameRates'] and height_p in mode['Bitrates']:
self.vbitrate = mode['Bitrates'][height_p]
if isinstance(self.vbitrate, list):
self.vbitrate = self.estimate_bitrate(frame_rate,
mode['FrameRates'],
mode['Bitrates'][height_p])
print(f"YT category: {mode['Category']}")
if not hasattr(self, 'vbitrate'):
print('no profile')
sys.exit(1)
print(f"YT bitrate: {self.vbitrate} Mbps")
self.gop = frame_rate // 2
print(f"YT GOP: {self.gop}")
def run(self):
cmd = ['ffmpeg', '-hide_banner', '-nostdin', '-ignore_editlist', '1']
params_in = {}
filter_v = {}
match self.args.enc:
case 'amf':
params = {
'c:v': 'h264_amf',
'usage': 'lowlatency_high_quality',
'profile': 'high',
'quality': 'quality',
}
if self.args.crf:
params['rc'] = 'cqp'
params['qp_i'] = self.args.crf
params['qp_p'] = self.args.crf
params['qp_b'] = self.args.crf
case 'vaapi':
# ffmpeg -hide_banner -h encoder=h264_vaapi|less
params_in = enc_hevc_vaapi.PARAMS_IN
params = {
'c:v': 'h264_vaapi',
'compression_level': '29',
# 'quality': '0' # def -1
}
if self.mi.bit_depth == 8:
params['profile:v'] = 'high'
else:
params['profile:v'] = 'high10'
if self.args.crf:
params['rc_mode'] = 'CQP'
params['qp'] = self.args.crf
case 'nv':
params_in = {
'hwaccel': 'cuda',
'hwaccel_output_format': 'cuda',
}
params = {
'c:v': 'h264_nvenc',
'preset:v': 'p7',
'tune:v': 'hq',
'rc:v': 'vbr',
'cq:v': self.args.crf,
'b:v': '0',
#'tier': 'high',
#'profile:v' 'high',
}
if self.mi.bit_depth == 10:
params['profile:v'] = 'main10'
case 'sw':
params = {
'c:v': 'libx264',
'preset': self.args.preset,
'bf': '2', # maximum number of B-frames between non-B-frames
}
if self.args.opencl:
params_in['hwaccel'] = 'auto'
params['x264opts'] = 'opencl'
if self.mi.bit_depth == 8:
filter_v['format'] = 'yuv420p'
params['profile:v'] = 'high'
else:
filter_v['format'] = 'yuv420p10le'
params['profile:v'] = 'high422'
if self.args.crf:
# for 10 bit params['qp'] = 0
params['crf'] = self.args.crf
if self.args.deint:
filter_v['bwdif'] = 'mode=send_field:parity=auto:deint=all'
if self.mi.has_audio:
params['c:a'] = 'aac'
channels = self.mi.audio_channels
params['ac'] = str(channels)
if channels == 1:
# force stereo
params['ac'] = '2'
abr = 256
elif channels == 2:
abr = 384
else:
abr = 512
params['b:a'] = f'{abr}k'
if self.mi.audio_sampling_rate <= 48000:
ar = 48000
else:
ar = 96000
params['ar'] = str(ar)
else:
cmd.append('-an')
if not self.args.crf:
params['b:v'] = f'{self.vbitrate}M'
mr = f'{int(self.vbitrate*1.2)}M'
params['maxrate:v'] = mr
params['bufsize'] = mr
#params['r'] = f'{self.mi.frame_rate}'
#params['g'] = f'{self.gop}'
params['force_key_frames'] = 'expr:gte(t,n_forced/2)'
params['flags'] = 'cgop'
params['movflags'] = '+faststart'
params['use_editlist'] = '0'
params['coder'] = 'cabac'
if self.args.duration:
params['t'] = self.args.duration
# input
for key, value in params_in.items():
cmd.extend([f'-{key}', value])
cmd.extend(['-i', self.args.in_file])
# output
for key, value in params.items():
cmd.extend([f'-{key}', value])
# filters
if filter_v:
vf = [f"{key}={value}" for key, value in filter_v.items()]
cmd.extend(['-filter:v', ','.join(vf)])
cmd.append('-y')
cmd.append(self.args.out_file)
run_cmd(cmd, self.args.dry)
def __main__():
yt = YouTube()
yt.run()
if __name__ == '__main__':
__main__()