-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (46 loc) · 2.06 KB
/
Program.cs
File metadata and controls
59 lines (46 loc) · 2.06 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
using SkiaSharp;
using FFmpeg.Wrapper;
if (args.Length < 1) {
Console.WriteLine("Usage: SkiaInterop <output path>");
return;
}
using var muxer = new MediaMuxer(args[0]);
int frameRate = 30;
using var encoder = new VideoEncoder(CodecIds.H264, new PictureFormat(1280, 720, PixelFormats.YUV420P), frameRate, bitrate: 1200_000);
using var frame = new VideoFrame(encoder.FrameFormat);
var stream = muxer.AddStream(encoder);
muxer.Open();
using var bitmap = new SKBitmap(frame.Width, frame.Height, SKColorType.Rgba8888, SKAlphaType.Premul);
using var canvas = new SKCanvas(bitmap);
using var scaler = new SwScaler(new PictureFormat(bitmap.Width, bitmap.Height, PixelFormats.RGBA), frame.Format, InterpolationMode.Bilinear);
int numFrames = (frameRate * 10 + 1); //encode 10s of video
for (int i = 0; i < numFrames; i++) {
Console.Write($"Generating frame {i}/{numFrames}\r");
//Draw some weird stuff
using var paint = new SKPaint() {
IsAntialias = true,
Color = SKColors.Black,
TextAlign = SKTextAlign.Right,
TextSize = 48
};
canvas.Clear(SKColors.White);
canvas.DrawText("Frame #" + i, bitmap.Width - 4, paint.TextSize + 4, paint);
paint.ImageFilter = SKImageFilter.CreateDropShadow(2f, 2f, 4f, 4f, 0x70_000000);
for (int j = 0; j < 40; j++) {
float t = i / (float)frameRate * 0.8f + j / 40.0f;
float x = j / 40.0f * bitmap.Width;
float y = bitmap.Height * 0.7f + MathF.Sin(t * 5) * 150;
paint.Color = SKColor.FromHsv((t * 200) % 360, 75, 90);
canvas.DrawCircle(x, y, 32, paint);
}
paint.ImageFilter = null;
paint.Shader = SKShader.CreatePerlinNoiseImprovedNoise(0.03f, 0.03f, 3, i / (float)frameRate * 1.3f);
canvas.DrawRect(32, 32, 256, 256, paint);
//Convert to YUV and encode
canvas.Flush();
scaler.Convert(bitmap.GetPixelSpan(), bitmap.RowBytes, frame);
frame.PresentationTimestamp = encoder.GetFramePts(frameNumber: i);
muxer.EncodeAndWrite(stream, encoder, frame);
}
//Flush delayed frames in the encoder
muxer.EncodeAndWrite(stream, encoder, null!);