-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathVideoSegment.cs
More file actions
540 lines (479 loc) · 20.1 KB
/
VideoSegment.cs
File metadata and controls
540 lines (479 loc) · 20.1 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
using JyDraft.meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static JyDraft.meta.VideoSceneEffectType;
using static JyDraft.TimeUtil;
namespace JyDraft
{
public class Mask
{
public MaskMeta MaskMeta { get; set; }
public string GlobalId { get; set; }
public float CenterX { get; set; }
public float CenterY { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double AspectRatio { get; set; }
public float Rotation { get; set; }
public bool Invert { get; set; }
public float Feather { get; set; }
public float RoundCorner { get; set; }
public Mask(MaskMeta maskMeta,
float cx, float cy, double w, double h,
double ratio, float rot, bool inv, float feather, float roundCorner)
{
this.MaskMeta = maskMeta;
this.GlobalId = Guid.NewGuid().ToString();
this.CenterX = cx;
this.CenterY = cy;
this.Width = w;
this.Height = h;
this.AspectRatio = ratio;
this.Rotation = rot;
this.Invert = inv;
this.Feather = feather;
this.RoundCorner = roundCorner;
}
public Dictionary<string, object> ExportJson()
{
return new Dictionary<string, object>
{
["config"] = new Dictionary<string, object>
{
["aspectRatio"] = AspectRatio,
["centerX"] = CenterX,
["centerY"] = CenterY,
["feather"] = Feather,
["height"] = Height,
["invert"] = Invert,
["rotation"] = Rotation,
["roundCorner"] = RoundCorner,
["width"] = Width
},
["id"] = GlobalId,
["name"] = MaskMeta.Name,
["platform"] = "all",
["position_info"] = "",
["resource_type"] = MaskMeta.ResourceType,
["resource_id"] = MaskMeta.ResourceId,
["type"] = "mask"
};
}
}
public class VideoEffect
{
public string Name { get; set; }
public string GlobalId { get; set; }
public string EffectId { get; set; }
public string ResourceId { get; set; }
public string EffectType { get; set; } // "video_effect" or "face_effect"
public int ApplyTargetType { get; set; } // 0 or 2
public List<EffectParamInstance> AdjustParams { get; set; }
// 你需要根据实际情况实现 effectMeta 的类型,可以是父类或接口
public VideoEffect(
EffectMeta effectMeta,
List<float?> parameters = null,
int applyTargetType = 0
)
{
GlobalId = Guid.NewGuid().ToString();
AdjustParams = new List<EffectParamInstance>();
var category = VideoEffectCategoryResolver.GetCategory(effectMeta);
if (category== VideoEffectCategory.Scene)
{
EffectType = "video_effect";
Name = effectMeta.Name;
EffectId = effectMeta.EffectId;
ResourceId = effectMeta.ResourceId;
AdjustParams = effectMeta.ParseParams(parameters);
}
else if (category==VideoEffectCategory.Character)
{
EffectType = "face_effect";
Name = effectMeta.Name;
EffectId = effectMeta.EffectId;
ResourceId = effectMeta.ResourceId;
AdjustParams = effectMeta.ParseParams(parameters);
}
else
{
throw new ArgumentException("Invalid effect meta type");
}
ApplyTargetType = applyTargetType;
}
public Dictionary<string, object> ExportJson()
{
var adjustParamsJson = new List<object>();
foreach (var param in AdjustParams)
{
adjustParamsJson.Add(param.ExportJson());
}
return new Dictionary<string, object>
{
["adjust_params"] = adjustParamsJson,
["apply_target_type"] = ApplyTargetType,
["apply_time_range"] = null,
["category_id"] = "",
["category_name"] = "",
["common_keyframes"] = new List<object>(),
["disable_effect_faces"] = new List<object>(),
["effect_id"] = EffectId,
["formula_id"] = "",
["id"] = GlobalId,
["name"] = Name,
["platform"] = "all",
["render_index"] = 11000,
["resource_id"] = ResourceId,
["source_platform"] = 0,
["time_range"] = null,
["track_render_index"] = 0,
["type"] = EffectType,
["value"] = 1.0,
["version"] = ""
};
}
}
public class Filter
{
/// <summary>滤镜全局 ID</summary>
public string GlobalId { get; private set; }
/// <summary>滤镜的元数据</summary>
public EffectMeta EffectMeta { get; set; }
/// <summary>滤镜强度</summary>
public float Intensity { get; set; }
/// <summary>应用目标类型:0 - 片段,2 - 全局</summary>
public int ApplyTargetType { get; set; }
public Filter(EffectMeta meta, float intensity, int applyTargetType = 0)
{
GlobalId = Guid.NewGuid().ToString();
EffectMeta = meta;
Intensity = intensity;
ApplyTargetType = applyTargetType;
}
public Dictionary<string, object> ExportJson()
{
return new Dictionary<string, object>
{
["adjust_params"] = new List<object>(),
["algorithm_artifact_path"] = "",
["apply_target_type"] = ApplyTargetType,
["bloom_params"] = null,
["category_id"] = "",
["category_name"] = "",
["color_match_info"] = new Dictionary<string, object>
{
["source_feature_path"] = "",
["target_feature_path"] = "",
["target_image_path"] = ""
},
["effect_id"] = EffectMeta.EffectId,
["enable_skin_tone_correction"] = false,
["exclusion_group"] = new List<object>(),
["face_adjust_params"] = new List<object>(),
["formula_id"] = "",
["id"] = GlobalId,
["intensity_key"] = "",
["multi_language_current"] = "",
["name"] = EffectMeta.Name,
["panel_id"] = "",
["platform"] = "all",
["resource_id"] = EffectMeta.ResourceId,
["source_platform"] = 1,
["sub_type"] = "none",
["time_range"] = null,
["type"] = "filter",
["value"] = Intensity,
["version"] = ""
};
}
}
public class Transition
{
/// <summary>转场名称</summary>
public string Name { get; set; }
/// <summary>转场全局id,由程序自动生成</summary>
public string GlobalId { get; set; }
/// <summary>转场效果id,由剪映本身提供</summary>
public string EffectId { get; set; }
/// <summary>资源id,由剪映本身提供</summary>
public string ResourceId { get; set; }
/// <summary>转场持续时间,单位为微秒</summary>
public int Duration { get; set; }
/// <summary>是否与上一个片段重叠</summary>
public bool IsOverlap { get; set; }
public Transition(TransitionMeta transitionMeta, int? duration = null)
{
Name = transitionMeta.Name;
GlobalId = Guid.NewGuid().ToString(); // 相当于 Python 的 uuid.uuid4().hex
EffectId = transitionMeta.EffectId;
ResourceId = transitionMeta.ResourceId;
Duration = duration ?? transitionMeta.DefaultDuration;
IsOverlap = transitionMeta.IsOverlap;
}
public Dictionary<string, object> ExportJson()
{
return new Dictionary<string, object>
{
{ "category_id", "" },
{ "category_name", "" },
{ "duration", Duration },
{ "effect_id", EffectId },
{ "id", GlobalId },
{ "is_overlap", IsOverlap },
{ "name", Name },
{ "platform", "all" },
{ "resource_id", ResourceId },
{ "type", "transition" }
// 不导出 path 和 request_id
};
}
}
public class BackgroundFilling
{
/// <summary>
/// 背景填充全局ID,由程序自动生成
/// </summary>
public string GlobalId { get; private set; }
/// <summary>
/// 背景填充类型:"canvas_blur" 或 "canvas_color"
/// </summary>
public string FillType { get; private set; }
/// <summary>
/// 模糊程度,范围 0-1
/// </summary>
public double Blur { get; private set; }
/// <summary>
/// 背景颜色,格式为 "#RRGGBBAA"
/// </summary>
public string Color { get; private set; }
public BackgroundFilling(string fillType, double blur, string color)
{
if (fillType != "canvas_blur" && fillType != "canvas_color")
throw new ArgumentException("fillType must be either 'canvas_blur' or 'canvas_color'");
if (blur < 0 || blur > 1)
throw new ArgumentOutOfRangeException(nameof(blur), "blur must be between 0 and 1");
if (string.IsNullOrEmpty(color) || !color.StartsWith("#") || color.Length != 9)
throw new ArgumentException("color must be in the format '#RRGGBBAA'");
GlobalId = Guid.NewGuid().ToString();
FillType = fillType;
Blur = blur;
Color = color;
}
/// <summary>
/// 导出为 JSON 对象(使用 Dictionary 表示)
/// </summary>
public Dictionary<string, object> ExportJson()
{
return new Dictionary<string, object>
{
{ "id", GlobalId },
{ "type", FillType },
{ "blur", Blur },
{ "color", Color },
{ "source_platform", 0 }
};
}
}
public class VideoSegment : VisualSegment
{
public VideoMaterial MaterialInstance { get; set; }
public (int Width, int Height) MaterialSize { get; set; }
public List<VideoEffect> Effects { get; set; } = new List<VideoEffect>();
public List<Filter> Filters { get; set; } = new List<Filter>();
public Mask Mask { get; set; }
public Transition Transition { get; set; }
public BackgroundFilling BackgroundFilling { get; set; }
public VideoSegment(
VideoMaterial material,
Timerange targetTimerange,
Timerange sourceTimerange = null,
double? speed = null,
double volume = 1.0,
ClipSettings clipSettings = null)
: base(material.MaterialId, sourceTimerange, targetTimerange, speed ?? 1.0, volume, clipSettings)
{
if (sourceTimerange != null && speed.HasValue)
{
targetTimerange = new Timerange(targetTimerange.Start, (int)(sourceTimerange.Duration / speed.Value));
}
else if (sourceTimerange != null && !speed.HasValue)
{
speed = sourceTimerange.Duration / targetTimerange.Duration;
}
else
{
speed ??= 1.0;
sourceTimerange = new Timerange(0, (int)(targetTimerange.Duration * speed.Value));
}
if (sourceTimerange.End > material.Duration)
throw new ArgumentOutOfRangeException("sourceTimerange 超出了素材时长");
// 初始化父类
base.SourceTimerange = sourceTimerange;
base.TargetTimerange = targetTimerange;
base.Speed.Value = speed.Value;
this.MaterialInstance = material;
this.MaterialSize = (material.Width, material.Height);
}
public VideoSegment AddAnimation(
AnimationMeta animationMeta, // 可以是 IntroType, OutroType, 或 GroupAnimationType
object duration = null // 可以是 int 或 string 或 null
)
{
long? resolvedDuration = null;
long start = 0;
// 假设有类似 tim() 的解析方法
if (duration != null)
{
if (duration is string strDuration)
{
resolvedDuration = Tim(strDuration);
}
else if (duration is int intDuration)
{
resolvedDuration = intDuration;
}
else
{
throw new ArgumentException("duration must be int or string");
}
}
var category = AnimationCategoryResolver.GetCategory(animationMeta);
var categoryName = "in";
switch (category)
{
case AnimationCategory.Intro:
start = 0;
resolvedDuration ??= animationMeta.Duration;
categoryName = "in";
break;
case AnimationCategory.Outro:
resolvedDuration ??= animationMeta.Duration;
start = this.TargetTimerange.Duration - resolvedDuration.Value;
categoryName = "out";
break;
case AnimationCategory.Group:
start = 0;
resolvedDuration ??= this.TargetTimerange.Duration;
categoryName = "group";
break;
default:
throw new InvalidOperationException("无法识别动画类型。");
}
if (this.AnimationsInstance == null)
{
this.AnimationsInstance = new SegmentAnimations();
this.ExtraMaterialRefs.Add(this.AnimationsInstance.AnimationId);
}
this.AnimationsInstance.AddAnimation(
new VideoAnimation(animationMeta,categoryName, start, resolvedDuration.Value)
);
return this;
}
public VideoSegment AddEffect(EffectMeta effectType, List<float?> parameters = null)
{
var effect = new VideoEffect(effectType, parameters);
Effects.Add(effect);
ExtraMaterialRefs.Add(effect.GlobalId);
return this;
}
public VideoSegment AddFilter(EffectMeta meta, float intensity = 100.0f)
{
var filter = new Filter(meta, intensity / 100.0f);
Filters.Add(filter);
ExtraMaterialRefs.Add(filter.GlobalId);
return this;
}
public VideoSegment AddMask(MaskMeta type, float centerX = 0.0f, float centerY = 0.0f, float size = 0.5f,
float rotation = 0.0f, float feather = 0.0f, bool invert = false,
double? rectWidth = null, float? roundCorner = null)
{
if (Mask != null)
throw new InvalidOperationException("当前片段已有蒙版");
if ((rectWidth != null || roundCorner != null) && type != MaskType.矩形)
throw new ArgumentException("仅矩形蒙版可设置 rectWidth 或 roundCorner");
rectWidth ??= size;
roundCorner ??= 0f;
var widthRatio = MaterialSize.Height * type.DefaultAspectRatio / MaterialSize.Width;
var mask = new Mask(type, centerX, centerY,
rectWidth.Value * widthRatio, size,
type.DefaultAspectRatio, rotation, invert,
feather / 100, roundCorner.Value / 100);
this.Mask = mask;
ExtraMaterialRefs.Add(mask.GlobalId);
return this;
}
public VideoSegment AddTransition(TransitionMeta type, object duration = null)
{
if (Transition != null)
throw new InvalidOperationException("当前片段已有转场");
int dur = duration is string s ? Convert.ToInt32(TimeUtil.SrtTstamp(s)) : Convert.ToInt32(duration);
this.Transition = new Transition(type, dur);
ExtraMaterialRefs.Add(Transition.GlobalId);
return this;
}
public VideoSegment AddBackgroundFilling(string fillType, double blur = 0.0625, string color = "#00000000")
{
if (BackgroundFilling != null)
throw new InvalidOperationException("已有背景填充");
if (fillType == "blur")
BackgroundFilling = new BackgroundFilling("canvas_blur", blur, color);
else if (fillType == "color")
BackgroundFilling = new BackgroundFilling("canvas_color", blur, color);
else
throw new ArgumentException($"无效的背景填充类型 {fillType}");
ExtraMaterialRefs.Add(BackgroundFilling.GlobalId);
return this;
}
public override Dictionary<string, object> ExportJson()
{
var json = base.ExportJson();
json["hdr_settings"] = new Dictionary<string, object>
{
{ "intensity", 1.0 },
{ "mode", 1 },
{ "nits", 1000 }
};
return json;
}
}
/// <summary>
/// 安放在轨道上的一个贴纸片段
/// </summary>
public class StickerSegment : VisualSegment
{
/// <summary>
/// 贴纸资源id
/// </summary>
public string ResourceId { get; set; }
/// <summary>
/// 根据贴纸resource_id构建一个贴纸片段, 并指定其时间信息及图像调节设置
/// 片段创建完成后, 可通过ScriptFile.AddSegment方法将其添加到轨道中
/// </summary>
/// <param name="resourceId">贴纸resource_id, 可通过ScriptFile.InspectMaterial从模板中获取</param>
/// <param name="targetTimerange">片段在轨道上的目标时间范围</param>
/// <param name="clipSettings">图像调节设置, 默认不作任何变换</param>
public StickerSegment(string resourceId, Timerange targetTimerange, ClipSettings clipSettings = null)
: base(Guid.NewGuid().ToString(), null, targetTimerange, 1.0, 1.0, clipSettings)
{
this.ResourceId = resourceId;
}
/// <summary>
/// 创建极简的贴纸素材对象, 以此不再单独定义贴纸素材类
/// </summary>
/// <returns>字典形式的贴纸素材对象</returns>
public Dictionary<string, object> ExportMaterial()
{
return new Dictionary<string, object>
{
{ "id", this.MaterialId },
{ "resource_id", this.ResourceId },
{ "sticker_id", this.ResourceId },
{ "source_platform", 1 },
{ "type", "sticker" }
};
}
}
}