我正致力于使用RC4算法加密视频帧的项目,并将这些帧保存在可播放的视频文件中。
我使用了一个名为Accord.Video.FFMPEG的包。这个包有一个类(VideoFileReader,& VideoFileWriter),可以读取和写入视频帧。
第一步是阅读视频:
VideoHandler v = new VideoHandler();
OpenFileDialog newimag = new OpenFileDialog();
if (newimag.ShowDialog() == DialogResult.OK)
{
textfile = newimag.FileName;
picbox.ImageLocation = textfile;
status1.Text = "loaded";
//MessageBox.Show("your video file have been loaded seccufly"); // is an idea for viwing message
}
bytedata = v.ReadAllFrameBytes(textfile);
第二步是加密视频帧:
byte[] bn = new byte[bytedata.Length];// new array to hold the encryptred file
bn = Encrypt(bytedata, ba);
最后一步是保存加密的帧:
v.WriteFramesData(newfilepath, bn);
我的加密算法是使用相同的算法和密钥加密和解密密码。
这些stpes适用于Text和Images文件,但是当我在视频上使用它时,我无法恢复加密的视频。经过一些测试,我发现VideoFileWriter不会写相同的输入帧。 Whyyyyyyy?
这是我制作的VideoFileHandler:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
using Accord.Video.FFMPEG;
namespace imgtobyt_1_in_c
{
class VideoHandler
{
public List<byte[]> data;
public byte[] imagedata;
public int Height, Width;
public byte[] ReadAllFrameBytes(string FileName)
{
// create instance of video reader
VideoFileReader reader = new VideoFileReader();
// open video file
reader.Open(FileName);
Height = reader.Height;
Width = reader.Width;
data = new List<byte[]>();
// read video frames
for (int i = 0; i < 100; i++) //change 100 to reader.FrameCount
{
Bitmap videoFrame = reader.ReadVideoFrame();
byte[] framebytes = GetBytesFromFrame(videoFrame);
data.Add(framebytes);
// dispose the frame when it is no longer required
videoFrame.Dispose();
}
reader.Close();
imagedata = new byte[data.Count * data[0].Length];
int c = 0;
for (int i = 0; i < data.Count; i++)
{
byte[] d = data[i];
for (int x = 0; x < d.Length; x++)
{
imagedata[c] = d[x];
c++;
}
}
return imagedata;
}
public byte[] GetBytesFromFrame(Bitmap Frame)
{
LockBitmap lockBitmap = new LockBitmap(Frame);
lockBitmap.LockBits();
byte[] framebytes = new byte[Frame.Width * Frame.Height * 3];
int z = 0;
for (int x = 0; x < lockBitmap.Height; x++)
for (int y = 0; y < lockBitmap.Width; y++)
{
Color Pixel = lockBitmap.GetPixel(y, x);
framebytes[z] = Pixel.R;
z++;
framebytes[z] = Pixel.G;
z++;
framebytes[z] = Pixel.B;
z++;
}
lockBitmap.UnlockBits();
return framebytes;
//using (var stream = new MemoryStream())
//{
// Frame.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
// return stream.ToArray();
//}
}
public Bitmap GetFrameFromBytes(byte[] Framebytes, ref int offset, int Width, int Height)
{
Bitmap Frame = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
LockBitmap lockBitmap = new LockBitmap(Frame);
lockBitmap.LockBits();
for (int x = 0; x < Height; x++)
for (int y = 0; y < Width; y++)
{
Color Pixel = Color.FromArgb(Framebytes[offset], Framebytes[offset + 1], Framebytes[offset + 2]); offset += 3;
lockBitmap.SetPixel(y, x, Pixel);
}
lockBitmap.UnlockBits();
return Frame;
//Bitmap bmp;
//using (var ms = new MemoryStream(Framebytes))
//{
// bmp = new Bitmap(ms);
//}
//return bmp;
}
public void WriteFramesData(string FileName, byte[] data)
{
// create instance of video writer
VideoFileWriter writer = new VideoFileWriter();
// create new video file
writer.Open(FileName, Width, Height);
int offset = 0;
// write video frames
for (int i = 0; i < 100; i++)
{
// create a bitmap to save into the video file
Bitmap Frame = GetFrameFromBytes(data, ref offset, Width, Height);
writer.WriteVideoFrame(Frame);
}
writer.Close();
}
}
}
拜托,我需要做到这一点。