我正在尝试从JPEG图像创建时间轴。我读到M-JPEG的结构相当简单,基本上只是串联的JPEG图像,大多数浏览器都可以播放。
var result = new List<byte>();
var converter = new ImageConverter();
foreach (var file in Directory.GetFiles(srcFolder, "*.jpg", SearchOption.TopDirectoryOnly)) {
using (var image = Image.FromFile(file) as Bitmap) {
var bytes = converter.ConvertTo(image, typeof(byte[])) as byte[];
result.AddRange(bytes);
image.Dispose();
}
}
File.WriteAllBytes($"{destFolder}video.mjpeg", result.ToArray());
然而,它并没有起作用。任何浏览器都无法播放生成的视频。可以在VLC中播放,显示正确的第一张图片,但在其十秒持续时间内不会更改为任何后续图像。
有人能说出我做错了什么吗?此外,是否可以通过标题调整帧速率?
更新:感谢建议,我已经在VLC中使其正常工作。仍然无法在浏览器中播放,但至少是朝着正确方向迈出的一步。
var result = new List<byte>();
var converter = new ImageConverter();
var header = "HTTP/1.1 200 OK\r\n" +
"Content-Type: multipart/x-mixed-replace; boundary=" +
"--boundary" +
"\r\n";
result.AddRange(Encoding.ASCII.GetBytes(header));
foreach (var file in Directory.GetFiles(srcFolder, "*.jpg", SearchOption.TopDirectoryOnly)) {
using (var image = Image.FromFile(file) as Bitmap) {
var bytes = converter.ConvertTo(image, typeof(byte[])) as byte[];
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("--boundary");
sb.AppendLine("Content-Type: image/jpeg");
sb.AppendLine("Content-Length: " + image.Size.ToString());
sb.AppendLine();
result.AddRange(Encoding.ASCII.GetBytes(sb.ToString()));
result.AddRange(bytes);
result.AddRange(Encoding.ASCII.GetBytes("\r\n"));
image.Dispose();
}
}
File.WriteAllBytes($"{destFolder}hat.mjpeg", result.ToArray());