-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFile.cpp
More file actions
424 lines (376 loc) · 9.59 KB
/
InputFile.cpp
File metadata and controls
424 lines (376 loc) · 9.59 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "pch.h"
#include "InputFile.h"
#include<afxstr.h>
#include<utility>
#include<vector>
/*
构造函数.
参数表: 路径, 格式, 编码, 视频码率 (CRF), 音频码率, 是否复制视频流, 是否复制音频流.
注意: 未指定的编码, CRF 将设置为 25, 码率设置为 5000.
*/
InputFile::InputFile(CString path, int format, int code, int vb, int ab, bool vcopy, bool acopy)
{
this->path = path;
int splitPos = path.ReverseFind(L'\\');
if (splitPos != -1) this->dir = path.Left(splitPos);
this->format = format;
this->code = code;
if (code == CRF)
{
this->crf = vb;
this->vb = 5000;
}
else
{
this->vb = vb;
this->crf = 25;
}
this->ab = ab;
this->cut = false;
this->acopy = acopy;
this->vcopy = vcopy;
this->range = std::make_pair(0, 0);
}
/*
构造函数.
参数表: 路径, 设置 (InputFile)
使用现有的 InputFile 的设置来初始化.
注意: 未指定的编码, CRF 将设置为 25, 码率设置为 5000.
*/
InputFile::InputFile(CString path, InputFile settings)
{
this->path = path;
int splitPos = path.ReverseFind(L'\\');
if (splitPos != -1) this->dir = path.Left(splitPos);
this->setData(settings.getData());
if (code == CRF)
{
vb = 5000;
}
else
{
crf = 25;
}
}
/*
translate -> vector<CString>
outputPath: 输出目录
根据当前文件的转码设置, 生成一组转码命令行 (一个或者两个). 如果 outputPath 为空, 那么设定为当前文件夹.
*/
std::vector<CString> InputFile::translate(CString outputPath)
{
auto combineArgs = [](std::vector<CString> args)->CString
{
CString ans;
for (auto i : args)
{
ans += i + L" ";
}
return ans;
};
const CString common = L"-keyint_min 30 -preset slow -f mp4 -hide_banner -y";
CString tmp, after=L"_bdcoder.mp4";
std::vector<CString> ans;
std::vector<CString> commands;
ans.push_back(L"ffmpeg");
if (cut)
{
tmp.Format(L"-ss %d -to %d -accurate_seek", range.first, range.second);
ans.push_back(tmp);
after = L"_bdcoder_cut.mp4";
}
ans.push_back(L"-i \"" + path + L"\"");
if (cut) ans.push_back(L"-avoid_negative_ts 1");
ans.push_back(common);
ans.push_back(L"-vcodec");
if (vcopy)
{
ans.push_back(L"copy");
}
else
{
if (format == H264)
{
if (code == VBR) ans.push_back(L"h264_nvenc");
else ans.push_back(L"libx264");
ans.push_back(L"-profile:v high10");
}
else
{
if (code == VBR) ans.push_back(L"hevc_nvenc");
else ans.push_back(L"libx265");
ans.push_back(L"-profile:v main10");
}
if (code == CRF)
{
ans.push_back(L"-crf");
tmp.Format(L"%d", crf);
ans.push_back(tmp);
}
if (code == PASS2)
{
ans.push_back(L"-b:v");
tmp.Format(L"%dk", vb);
ans.push_back(tmp);
auto p2 = ans;
p2.push_back(L"-pass 1");
ans.push_back(L"-pass 2");
p2.push_back(L"-an NUL");
commands.push_back(combineArgs(p2));
}
}
// after
if (!acopy) tmp.Format(L"-acodec aac -b:a %dki", ab);
else tmp = L"-acodec copy";
ans.push_back(tmp);
if (outputPath == L"")
{
outputPath = dir;
}
if (outputPath[outputPath.GetLength() - 1] != L'\\')
{
outputPath += L'\\';
}
int splitPos = path.ReverseFind(L'\\');
if (splitPos != -1) tmp = path.Right(path.GetLength() - splitPos-1);
else tmp = path;
splitPos = tmp.ReverseFind(L'.');
if (splitPos!=-1) tmp= tmp.Left(splitPos);
ans.push_back(L"\"" + outputPath+ tmp + L"_vbdcoder.mp4\"");
// combine
CString command;
for (auto i : ans)
{
command += i + L" ";
}
commands.push_back(command);
return commands;
}
int InputFile::setVideoBitrate(int vb)
{
this->ready = true;
int tmp;
if (code == CRF)
{
tmp = this->crf;
this->crf = vb;
}
else
{
tmp = this->vb;
this->vb = vb;
}
return tmp;
}
int InputFile::getVideoBitrate()
{
if (code == CRF) return crf;
else return vb;
}
int InputFile::setAudioBitrate(int ab)
{
this->ready = true;
int tmp = this->ab;
this->ab = ab;
return tmp;
}
int InputFile::getStatus()
{
return ready;
}
int InputFile::setStatus(int status)
{
this->ready = true;
auto tmp = ready;
ready = status;
return tmp;
}
bool InputFile::setCut(bool cut)
{
this->ready = true;
bool tmp = this->cut;
this->cut = cut;
return tmp;
}
int InputFile::setCode(int code)
{
this->ready = true;
int tmp = this->code;
this->code = code;
return tmp;
}
int InputFile::setFormat(int format)
{
this->ready = true;
int tmp = this->format;
this->format = format;
return tmp;
}
std::pair<int, int> InputFile::setCutRange(int start, int end)
{
this->ready = true;
auto tmp = this->range;
this->range = std::make_pair(start, end);
return tmp;
}
std::pair<int, int> InputFile::setCutRange(std::pair<int, int> range)
{
this->ready = true;
auto tmp = this->range;
this->range = range;
return tmp;
}
bool InputFile::setCopyVideoStream(bool vcopy)
{
this->ready = true;
auto tmp = this->vcopy;
this->vcopy = vcopy;
return tmp;
}
bool InputFile::setCopyAudioStream(bool acopy)
{
this->ready = true;
auto tmp = this->acopy;
this->acopy = acopy;
return tmp;
}
CString InputFile::getPath()
{
return path;
}
/*
getItemInfo -> vector<CString>
不接受参数
将转码设置转换为人类可读的字符数组. 用于显示视图中的列表.
*/
std::vector<CString> InputFile::getItemInfo()
{
CString tmp;
std::vector<CString> ans;
int splitPos = path.ReverseFind(L'\\');
if (splitPos != -1) tmp = path.Right(path.GetLength() - splitPos - 1);
else tmp = path;
ans.push_back(tmp);
switch (ready)
{
case READY: ans.push_back(L"就绪"); break;
case CODING: ans.push_back(L"转码中"); break;
case CODED: ans.push_back(L"完成"); break;
}
switch (format)
{
case H264: ans.push_back(L"H264"); break;
case H265: ans.push_back(L"H265"); break;
}
switch (code)
{
case PASS2: ans.push_back(L"2P"); break;
case CRF: ans.push_back(L"CRF"); break;
case VBR: ans.push_back(L"VBR"); break;
}
if (!vcopy)
{
if (code==CRF) tmp.Format(L"%d", crf);
else tmp.Format(L"%d", vb);
}
else tmp = L"copy";
ans.push_back(tmp);
if (!acopy) tmp.Format(L"%d", ab);
else tmp = L"copy";
ans.push_back(tmp);
if (cut)
{
tmp.Format(L"%02d:%02d:%02d", range.first / 3600, range.first % 3600 / 60, range.first % 60);
ans.push_back(tmp);
tmp.Format(L"%02d:%02d:%02d", range.second / 3600, range.second % 3600 / 60, range.second % 60);
ans.push_back(tmp);
}
else
{
ans.push_back(L"-");
ans.push_back(L"-");
}
ans.push_back(dir);
return ans;
}
/*
getData -> vector<int>
不接受参数
将转码设置转换为一组数值.
*/
std::vector<int> InputFile::getData()
{
if (code != CRF)
{
std::vector<int> ans{ format,code,vb,ab,vcopy,acopy,cut,range.first,range.second };
return ans;
}
else
{
std::vector<int> ans{ format,code,crf,ab,vcopy,acopy,cut,range.first,range.second };
return ans;
}
}
/*
setData -> bool
vector<int>: 用一组数值表示的转码设置, writeNegativeOne: 标记是否写 -1 值, 默认为真
设置转码设置为输入的转码设置
*/
bool InputFile::setData(std::vector<int> data, bool writeNegativeOne)
{
this->ready = true;
if (writeNegativeOne || data[0] != -1) this->format = data[0];
if (writeNegativeOne || data[1] != -1) this->code = data[1];
if (writeNegativeOne || data[2] != -1)
{
if (data[1] != CRF)
{
this->vb = data[2];
//this->crf = 25;
}
else
{
this->crf = data[2];
//this->vb = 5000;
}
}
if (writeNegativeOne || data[3] != -1)this->ab = data[3];
if (writeNegativeOne || data[4] != -1)this->vcopy = data[4];
if (writeNegativeOne || data[5] != -1)this->acopy = data[5];
if (writeNegativeOne || data[6] != -1)this->cut = data[6];
if (writeNegativeOne || data[7] != -1)this->range.first = data[7];
if (writeNegativeOne || data[8] != -1)this->range.second = data[8];
return false;
}
/*
setData -> bool
vector<int>: 用一组数值表示的转码设置, writeNegativeOne: 标记是否写 -1 值, 默认为真,
overrideCRFOrVB: 标记是否写未指定的编码的设置, 默认为否
重载函数. 设置转码设置为输入的转码设置
*/
bool InputFile::setData(InputFile data, bool writeNegativeOne, bool overrideCRFOrVB)
{
this->ready = true;
if (writeNegativeOne || data.format != -1) this->format = data.format;
if (writeNegativeOne || data.code != -1) this->code = data.code;
if (writeNegativeOne || data.vb != -1)
{
if (data.code != CRF)
{
this->vb = data.vb;
if (overrideCRFOrVB) this->crf = data.crf;
}
else
{
this->crf = data.crf;
if (overrideCRFOrVB) this->vb = data.vb;
}
}
if (writeNegativeOne || data.ab != -1)this->ab = data.ab;
if (writeNegativeOne || data.vcopy != -1)this->vcopy = data.vcopy;
if (writeNegativeOne || data.acopy != -1)this->acopy = data.acopy;
if (writeNegativeOne || data.cut != -1)this->cut = data.cut;
if (writeNegativeOne || data.range.first != -1)this->range.first = data.range.first;
if (writeNegativeOne || data.range.second != -1)this->range.second = data.range.second;
return false;
}