声音在 Linux 中的工作方式与在 Windows 中的工作方式相同。但是视频只是一个黑屏,当我尝试将帧保存为 BMP 文件时,所有这些都是损坏/空文件。我正在使用 Ffmpeg.Autogen 与库交互。https://github.com/Ruslan-B/FFmpeg.AutoGen。该文件是 MKV 容器中的 VP8 和 OGG。尽管出于某种原因扩展名是 AVI。
我试着弄乱代码的顺序。我检查以确保在 Linux 上构建的 Ffmpeg 具有 VP8。我在网上搜索,但找不到另一种方法来做我正在做的事情。这是为了对 OpenVIII 项目做出贡献。我的叉子-> https://github.com/Sebanisu/OpenVIII
这只是让缩放器准备好更改像素格式,否则人们会有蓝色的脸。
private void PrepareScaler()
{
if (MediaType != AVMediaType.AVMEDIA_TYPE_VIDEO)
{
return;
}
ScalerContext = ffmpeg.sws_getContext(
Decoder.CodecContext->width, Decoder.CodecContext->height, Decoder.CodecContext->pix_fmt,
Decoder.CodecContext->width, Decoder.CodecContext->height, AVPixelFormat.AV_PIX_FMT_RGBA,
ffmpeg.SWS_ACCURATE_RND, null, null, null);
Return = ffmpeg.sws_init_context(ScalerContext, null, null);
CheckReturn();
}
将 Frame 转换为 BMP 我认为这就是问题所在。因为我在其中添加了 bitmap.save 并得到了空的 BMP。
public Bitmap FrameToBMP()
{
Bitmap bitmap = null;
BitmapData bitmapData = null;
try
{
bitmap = new Bitmap(Decoder.CodecContext->width, Decoder.CodecContext->height, PixelFormat.Format32bppArgb);
AVPixelFormat v = Decoder.CodecContext->pix_fmt;
// lock the bitmap
bitmapData = bitmap.LockBits(new Rectangle(0, 0, Decoder.CodecContext->width, Decoder.CodecContext->height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)(bitmapData.Scan0);
byte*[] srcData = { ptr, null, null, null };
int[] srcLinesize = { bitmapData.Stride, 0, 0, 0 };
// convert video frame to the RGB bitmap
ffmpeg.sws_scale(ScalerContext, Decoder.Frame->data, Decoder.Frame->linesize, 0, Decoder.CodecContext->height, srcData, srcLinesize); //sws_scale broken on linux?
}
finally
{
if (bitmap != null && bitmapData != null)
{
bitmap.UnlockBits(bitmapData);
}
}
return bitmap;
}
在我得到一个位图后,我们把它变成一个 Texture2D 以便我们可以绘制它。
public Texture2D FrameToTexture2D()
{
//Get Bitmap. there might be a way to skip this step.
using (Bitmap frame = FrameToBMP())
{
//string filename = Path.Combine(Path.GetTempPath(), $"{Path.GetFileNameWithoutExtension(DecodedFileName)}_rawframe.{Decoder.CodecContext->frame_number}.bmp");
//frame.Save(filename);
BitmapData bmpdata = null;
Texture2D frameTex = null;
try
{
//Create Texture
frameTex = new Texture2D(Memory.spriteBatch.GraphicsDevice, frame.Width, frame.Height, false, SurfaceFormat.Color); //GC will collect frameTex
//Fill it with the bitmap.
bmpdata = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);// System.Drawing.Imaging.PixelFormat.Format32bppArgb);
byte[] texBuffer = new byte[bmpdata.Width * bmpdata.Height * 4]; //GC here
Marshal.Copy(bmpdata.Scan0, texBuffer, 0, texBuffer.Length);
frameTex.SetData(texBuffer);
}
finally
{
if (bmpdata != null)
{
frame.UnlockBits(bmpdata);
}
}
return frameTex;
}
}
如果你想要的话,我可以发布更多内容几乎都在我的叉子上
视频将像在 Windows 中一样播放。像 15 fps 一样流畅。:)