[英]How can I create a video file from separate images on a c# project
我想從c#中的列表創建一個視頻文件,它可以是媒體播放器可以打開的任何格式。
我已經嘗試過Aforge和Avi文件包裝器,但遺憾的是它們只能在x86中工作,並且我有很多依賴項,因此我無法更改項目類型。 所以,它必須是x64。
我的所有位圖都在一個列表中(大約50左右)public List tv_ImageData = new List();
我是c#的新手,並不了解我的方式。 我用谷歌搜索,找不到任何解決方案。 如果有人能指出我正確的方向(或圖書館),我將不勝感激。
(我覺得這樣評論會更好,但我還沒有這個名聲。如果這是不好的做法,我很抱歉!)
因為你對AForge的唯一問題似乎是它是為x86編譯的,所以我會提到看起來你可以自己為x64目標重新編譯它。
https://github.com/andrewkirillov/AForge.NET
快速搜索發現此鏈接指向包含64位版本的AForge重新編譯:
https://archive.codeplex.com/?p=aforgeffmpeg
我不知道這是否是最新的,所以我可能會建議你自己編譯。
我希望有所幫助!
在與SharpAvi混合之后,我解決了我的問題。 我有一個名為的名單
List<ushort[]> tv_data = new List<ushort> tv_data();
其中包含幀作為原始數據(值在0-255范圍內)。 我試圖使用文檔提供的示例,但它給了我一個好處avi(我猜它是因為SharpAvi期望DIB位圖)。 所以我改了一下,從這里借了一點( 如何從字節數組創建位圖? )top得到一個有效的解決方案。
這是我的功能:
using SharpAvi;
using SharpAvi.Output;
這可能不是最好的方法,但它有效。 希望有人會覺得它很有用。
private void SaveAsVideo(object sender, RoutedEventArgs e)
{
if (loadedFileName != "")
{
try
{
var writer = new AviWriter(string.Format("{0}.avi", fullPath))
{
FramesPerSecond = (decimal)VI.FrameRate,
EmitIndex1 = true
};
var stream = writer.AddVideoStream();
stream.Width = (int)VI.VideoWidth;
stream.Height = (int)VI.VideoHeight;
stream.Codec = KnownFourCCs.Codecs.Uncompressed;
stream.BitsPerPixel = BitsPerPixel.Bpp8;
var frameData = new byte[stream.Width * stream.Height];
int frameNo = 0;
foreach (ushort[] data in tv_Data)
{
byte[] byteData = tv_Data.ElementAt(frameNo);
byte[] newbytes = PadLines(byteData, stream.Height, stream.Width);
stream.WriteFrame(true, newbytes, 0, frameData.Length);
frameNo++;
}
writer.Close();
MessageBox.Show("Video file saved.");
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to save video. \n {0}", ex.Message));
}
}
}
static byte[] PadLines(byte[] bytes, int rows, int columns)
{
int currentStride = columns;
int newStride = columns;
byte[] newBytes = new byte[newStride * rows];
byte[] tempBytes = new byte[newStride];
for (int i = 0; i < rows; i++)
{
Buffer.BlockCopy(bytes, currentStride * i, tempBytes, 0, currentStride);
Array.Reverse(tempBytes);
Buffer.BlockCopy(tempBytes, 0, newBytes, newStride * i, currentStride);
}
Array.Reverse(newBytes);
return newBytes;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.