-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathExamples.cs
235 lines (213 loc) · 8.19 KB
/
Examples.cs
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
using Sdcb.FFmpeg.Codecs;
using Sdcb.FFmpeg.Formats;
using Sdcb.FFmpeg.Raw;
using Sdcb.FFmpeg.Swscales;
using Sdcb.FFmpeg.Toolboxs.Extensions;
using Sdcb.FFmpeg.Toolboxs.FilterTools;
using Sdcb.FFmpeg.Toolboxs.Generators;
using Sdcb.FFmpeg.Utils;
using System;
using System.Diagnostics;
using System.IO;
using Xunit;
using Xunit.Abstractions;
namespace Sdcb.FFmpeg.Tests;
public class Examples : IDisposable
{
private readonly ITestOutputHelper _console;
public Examples(ITestOutputHelper console)
{
_console = console;
}
[Fact]
public void CreatePng()
{
using Frame frame = Frame.CreateVideo(800, 600, AVPixelFormat.Yuv420p);
VideoFrameGenerator.FillYuv420p(frame, 0);
byte[] pngData = frame.EncodeToBytes(formatName: "apng");
//File.WriteAllBytes("test.png", pngData);
Assert.NotEmpty(pngData);
}
private byte[] MakeMp4(Codec codec, int width, int height, int frameCount = 30)
{
using FormatContext fc = FormatContext.AllocOutput(formatName: "mp4");
fc.VideoCodec = codec;
MediaStream vstream = fc.NewStream(fc.VideoCodec);
using CodecContext vcodec = new CodecContext(fc.VideoCodec)
{
Width = width,
Height = height,
TimeBase = new AVRational(1, 15),
PixelFormat = AVPixelFormat.Yuv420p,
Flags = AV_CODEC_FLAG.GlobalHeader,
};
vcodec.Open(fc.VideoCodec, new MediaDictionary
{
["preset"] = "ultrafast"
});
vstream.Codecpar!.CopyFrom(vcodec);
using DynamicIOContext io = IOContext.OpenDynamic();
fc.Pb = io;
fc.WriteHeader();
foreach (Packet packet in VideoFrameGenerator.Yuv420pSequence(vcodec.Width, vcodec.Height, frameCount)
.EncodeFrames(vcodec))
{
try
{
packet.RescaleTimestamp(vcodec.TimeBase, vstream.TimeBase);
packet.StreamIndex = vstream.Index;
fc.InterleavedWritePacket(packet);
}
finally
{
packet.Unref();
}
}
fc.WriteTrailer();
return io.GetBuffer().ToArray();
}
[Fact]
public void CreateMp4()
{
FFmpegLogger.LogWriter = (level, msg) => _console.WriteLine(msg?.Trim());
byte[] mp4 = MakeMp4(Codec.CommonEncoders.Libx264, width: 320, height: 240);
Assert.NotEmpty(mp4);
_console.WriteLine($"mp4 size: {mp4.Length}");
}
[Fact]
public void DecodeMp4()
{
FFmpegLogger.LogWriter = (level, msg) => { };
byte[] mp4 = MakeMp4(Codec.CommonEncoders.Libx264, width: 320, height: 240);
_console.WriteLine($"mp4 size: {mp4.Length}");
FFmpegLogger.LogWriter = (level, msg) => _console.WriteLine(msg?.Trim());
using IOContext io = IOContext.ReadStream(new MemoryStream(mp4));
using FormatContext fc = FormatContext.OpenInputIO(io);
MediaStream videoStream = fc.GetVideoStream();
using CodecContext videoDecoder = new CodecContext(Codec.FindDecoderById(videoStream.Codecpar!.CodecId));
videoDecoder.FillParameters(videoStream.Codecpar);
videoDecoder.Open();
using VideoFrameConverter sws = new();
using Frame dest = Frame.CreateVideo(videoStream.Codecpar.Width, videoStream.Codecpar.Height, AVPixelFormat.Rgb0);
foreach (Frame frame in fc.ReadPackets()
.DecodePackets(videoDecoder))
{
Stopwatch sw = Stopwatch.StartNew();
sws.ConvertFrame(frame, dest);
sw.Stop();
_console.WriteLine($"dts: {frame.PktDts}, pts: {frame.Pts}, sws-elapse: {sw.Elapsed.TotalMilliseconds:F2}ms");
}
}
[Fact]
public void MakeGif()
{
using FormatContext fc = FormatContext.AllocOutput(formatName: "gif");
fc.VideoCodec = Codec.CommonEncoders.Gif;
MediaStream vstream = fc.NewStream(fc.VideoCodec);
using CodecContext vcodec = new CodecContext(fc.VideoCodec)
{
Width = 400,
Height = 50,
TimeBase = new AVRational(1, 20),
PixelFormat = AVPixelFormat.Rgb8,
};
vcodec.Open(fc.VideoCodec);
vstream.Codecpar!.CopyFrom(vcodec);
using DynamicIOContext io = IOContext.OpenDynamic();
fc.Pb = io;
fc.WriteHeader();
foreach (Packet packet in VideoFrameGenerator.Yuv420pSequence(vcodec.Width, vcodec.Height, 40)
.ConvertFrames(vcodec)
.EncodeFrames(vcodec))
{
try
{
packet.RescaleTimestamp(vcodec.TimeBase, vstream.TimeBase);
long after = packet.Dts;
packet.StreamIndex = vstream.Index;
fc.InterleavedWritePacket(packet);
}
finally
{
packet.Unref();
}
}
fc.WriteTrailer();
byte[] gif = io.GetBuffer().ToArray();
Assert.NotEmpty(gif);
//File.WriteAllBytes("test.gif", gif);
}
[Fact]
public void MakeGifWithFilter()
{
using FormatContext fc = FormatContext.AllocOutput(formatName: "gif");
fc.VideoCodec = Codec.CommonEncoders.Gif;
MediaStream vstream = fc.NewStream(fc.VideoCodec);
using CodecContext vcodec = new CodecContext(fc.VideoCodec)
{
Width = 150,
Height = 100,
TimeBase = new AVRational(1, 10),
PixelFormat = AVPixelFormat.Pal8,
};
vcodec.Open(fc.VideoCodec);
vstream.Codecpar!.CopyFrom(vcodec);
using DynamicIOContext io = IOContext.OpenDynamic();
fc.Pb = io;
fc.WriteHeader();
string filter = $"fps={vcodec.TimeBase.Inverse().ToDouble()},scale={vcodec.Width}:{vcodec.Height}:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse";
VideoFrameGenerator.Yuv420pSequence(150, 100, 30)
.ApplyVideoFilters(new AVRational(1, 30), vcodec.PixelFormat, filter)
.EncodeAllFrames(fc, videoEncoder: vcodec)
.WriteAll(fc);
fc.WriteTrailer();
byte[] gif = io.GetBuffer().ToArray();
Assert.NotEmpty(gif);
//File.WriteAllBytes("test.gif", gif);
}
[Fact]
public void RemuxMp4WithFilter()
{
FFmpegLogger.LogWriter = (level, msg) => { };
byte[] mp4 = MakeMp4(Codec.CommonEncoders.Libx264, width: 320, height: 240, frameCount: 30);
_console.WriteLine($"mp4 size: {mp4.Length}");
//FFmpegLogger.LogWriter = (level, msg) => _console.WriteLine(msg?.Trim());
using IOContext input = IOContext.ReadStream(new MemoryStream(mp4));
using FormatContext inFc = FormatContext.OpenInputIO(input);
inFc.LoadStreamInfo();
MediaStream inVideoStream = inFc.GetVideoStream();
using CodecContext videoDecoder = new CodecContext(Codec.FindDecoderById(inVideoStream.Codecpar!.CodecId));
videoDecoder.FillParameters(inVideoStream.Codecpar);
videoDecoder.Open();
using VideoFilterContext filter = VideoFilterContext.Create(inVideoStream, "scale=480:-1");
using FormatContext outFc = FormatContext.AllocOutput(formatName: "mp4");
outFc.VideoCodec = Codec.CommonEncoders.Libx264;
MediaStream outVideoStream = outFc.NewStream(outFc.VideoCodec);
using CodecContext videoEncoder = new CodecContext(outFc.VideoCodec)
{
Flags = AV_CODEC_FLAG.GlobalHeader,
};
filter.ConfigureEncoder(videoEncoder);
videoEncoder.Open(inFc.VideoCodec, new MediaDictionary
{
["preset"] = "ultrafast"
});
outVideoStream.Codecpar!.CopyFrom(videoEncoder);
using DynamicIOContext io = IOContext.OpenDynamic();
outFc.Pb = io;
outFc.WriteHeader();
inFc.ReadPackets()
.DecodePackets(videoDecoder)
.ApplyVideoFilters(filter)
.EncodeAllFrames(outFc, null, videoEncoder)
.WriteAll(outFc);
outFc.WriteTrailer();
byte[] remuxMp4 = io.GetBuffer().ToArray();
Assert.NotEmpty(remuxMp4);
//File.WriteAllBytes("test.mp4", remuxMp4);
}
public void Dispose()
{
FFmpegLogger.LogWriter = null;
}
}