-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMux.cs
More file actions
183 lines (164 loc) · 7.85 KB
/
Copy pathMux.cs
File metadata and controls
183 lines (164 loc) · 7.85 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
using System;
using FFmpeg.AutoGen;
using FFmpeg.Sharp;
namespace FFmpeg.Sharp.Example
{
/// <summary>
/// Maps to FFmpeg example: mux.c
/// Generate a synthetic audio + video stream and mux them into an output
/// container. Default output: output_mux.mp4 (MPEG-4).
/// </summary>
public unsafe class Mux : ExampleBase
{
private const double StreamDuration = 10.0;
private const int FrameRate = 25;
private const int VideoWidth = 352;
private const int VideoHeight = 288;
public Mux() { Index = 19; Enable = false; }
public override void Execute()
{
var outFile = args.Length > 0 ? args[0] : "output_mux.mp4";
// ── Video encoder ─────────────────────────────────────────────────
var videoCodecId = AVCodecID.AV_CODEC_ID_MPEG4;
var videoCodec = MediaCodec.FindEncoder(videoCodecId);
using var videoEncoder = MediaEncoder.Video()
.Codec(videoCodec)
.Size(VideoWidth, VideoHeight)
.Fps(FrameRate)
.PixelFormat(AVPixelFormat.AV_PIX_FMT_YUV420P)
.Bitrate(400000)
.Flags(ffmpeg.AV_CODEC_FLAG_GLOBAL_HEADER)
.Configure(ctx =>
{
ctx.Ref.gop_size = 12;
if (videoCodecId == AVCodecID.AV_CODEC_ID_MPEG1VIDEO)
ctx.Ref.mb_decision = 2;
})
.Build();
// ── Audio encoder ─────────────────────────────────────────────────
var audioCodecId = AVCodecID.AV_CODEC_ID_MP2;
var audioCodec = MediaCodec.FindEncoder(audioCodecId);
int audioSampleRate = 44100;
foreach (var rate in audioCodec.GetSupportedSamplerates())
{
if (rate == 44100) { audioSampleRate = 44100; break; }
audioSampleRate = rate; // take first supported if 44100 unavailable
}
var audioSampleFmt = audioCodec.GetSampleFormats()[0];
var audioChLayout = 2.ToDefaultChLayout();
using var audioEncoder = MediaEncoder.Audio()
.Codec(audioCodec)
.SampleRate(audioSampleRate)
.ChannelLayout(audioChLayout)
.SampleFormat(audioSampleFmt)
.Bitrate(64000)
.Flags(ffmpeg.AV_CODEC_FLAG_GLOBAL_HEADER)
.Build();
int audioFrameSize = audioEncoder.Ref.frame_size > 0 ? audioEncoder.Ref.frame_size : 10000;
// ── Muxer ─────────────────────────────────────────────────────────
using var muxer = MediaMuxer.Create(outFile);
muxer.AddStream(videoEncoder);
muxer.AddStream(audioEncoder);
muxer.DumpFormat();
muxer.WriteHeader();
using var videoFrame = MediaFrame.CreateVideoFrame(VideoWidth, VideoHeight, AVPixelFormat.AV_PIX_FMT_YUV420P);
using var audioFrame = MediaFrame.CreateAudioFrame(audioChLayout, audioFrameSize, audioSampleFmt, audioSampleRate);
using var tmpPacket = new MediaPacket();
long videoNextPts = 0, audioNextPts = 0;
bool encodeMoreVideo = true, encodeMoreAudio = true;
while (encodeMoreVideo || encodeMoreAudio)
{
bool videoTime = encodeMoreVideo &&
(!encodeMoreAudio || FFmpegUtil.CompareTs(videoNextPts, videoEncoder.Ref.time_base,
audioNextPts, audioEncoder.Ref.time_base) <= 0);
if (videoTime)
{
if (FFmpegUtil.CompareTs(videoNextPts, videoEncoder.Ref.time_base,
(long)(StreamDuration * ffmpeg.AV_TIME_BASE), new AVRational { num = 1, den = ffmpeg.AV_TIME_BASE }) > 0)
{
// Flush video.
foreach (var p in videoEncoder.EncodeFrame(null, tmpPacket))
{
p.Ref.stream_index = 0;
muxer.WritePacket(p);
}
encodeMoreVideo = false;
}
else
{
FillYuv(videoFrame, (int)videoNextPts);
videoFrame.Ref.pts = videoNextPts++;
foreach (var p in videoEncoder.EncodeFrame(videoFrame, tmpPacket))
{
p.Ref.stream_index = 0;
muxer.WritePacket(p, videoEncoder.Ref.time_base);
}
}
}
else
{
if (FFmpegUtil.CompareTs(audioNextPts, audioEncoder.Ref.time_base,
(long)(StreamDuration * ffmpeg.AV_TIME_BASE), new AVRational { num = 1, den = ffmpeg.AV_TIME_BASE }) > 0)
{
foreach (var p in audioEncoder.EncodeFrame(null, tmpPacket))
{
p.Ref.stream_index = 1;
muxer.WritePacket(p);
}
encodeMoreAudio = false;
}
else
{
FillAudio(audioFrame, audioNextPts, audioSampleRate);
audioFrame.Ref.pts = audioNextPts;
audioNextPts += audioFrameSize;
foreach (var p in audioEncoder.EncodeFrame(audioFrame, tmpPacket))
{
p.Ref.stream_index = 1;
muxer.WritePacket(p, audioEncoder.Ref.time_base);
}
}
}
}
muxer.WriteTrailer();
Console.WriteLine($"Muxing complete: '{outFile}'");
}
private static void FillYuv(MediaFrame frame, int frameIndex)
{
int w = frame.Ref.width, h = frame.Ref.height;
byte* y = frame.Ref.data[0];
byte* cb = frame.Ref.data[1];
byte* cr = frame.Ref.data[2];
int ls0 = frame.Ref.linesize[0], ls1 = frame.Ref.linesize[1], ls2 = frame.Ref.linesize[2];
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++)
y[j * ls0 + i] = (byte)(i + j + frameIndex * 3);
for (int j = 0; j < h / 2; j++)
for (int i = 0; i < w / 2; i++)
{
cb[j * ls1 + i] = (byte)(128 + j + frameIndex * 2);
cr[j * ls2 + i] = (byte)(64 + i + frameIndex * 5);
}
}
private static double _audioT = 0, _audioTIncr, _audioTIncr2;
private static bool _audioInit = false;
private static void FillAudio(MediaFrame frame, long pts, int sampleRate)
{
if (!_audioInit)
{
_audioTIncr = 2 * Math.PI * 110.0 / sampleRate;
_audioTIncr2 = 2 * Math.PI * 110.0 / sampleRate / sampleRate;
_audioInit = true;
}
int nb_channels = frame.Ref.ch_layout.nb_channels;
short* q = (short*)frame.Ref.data[0];
for (int j = 0; j < frame.Ref.nb_samples; j++)
{
short v = (short)(Math.Sin(_audioT) * 10000);
for (int i = 0; i < nb_channels; i++) *q++ = v;
_audioT += _audioTIncr;
_audioTIncr += _audioTIncr2;
}
}
}
}