保存将视频拆分到数据库并检索它们

时间:2017-05-16 08:17:21

标签: c# .net database video encoding

我正在使用Microsoft编码器来分割视频。这是Microsoft网站上的示例:

=TEXT('Junk Sheet'!A2,"MM/DD/YYYY")

有没有办法可以将分割视频保存到数据库中?是否可以从数据库中获取分割视频到客户端?

来源:https://blogs.msdn.microsoft.com/deanro/2009/03/16/editing-files-with-encoder/

亲切的问候

1 个答案:

答案 0 :(得分:2)

There are two ways to achieve this.

  1. You upload the video files directly into your DB (You need a BLOB field in your database's video table)
  2. You save your video files on the filesystem and store the paths to those files inside the database (preferred).

Why do I prefer the second approach?
Databases are designed to handle small objects very, very fast. On the other hand larger objects inside the database degrade over time, access speeds suffer. The second approach is also more trivial but let's get to the first.

Encoding:

private void Encode()
{
    Job j = new Job();
    MediaItem m = new MediaItem(txtBxVideoFilePath.Text);
    Source s = m.Sources[0];
    s.Clips[0].StartTime = new TimeSpan(0, 0, 5);
    s.Clips[0].EndTime = new TimeSpan(0, 0, 10);
    j.OutputDirectory= @"C:\Users\MyOutputDir\";
    j.MediaItems.Add(m);
    j.Encode();
    txtBxOutputDir.Text = j.ActualOutputDirectory; //Path to your videofile
}

After the encoding you'll get a directory string in your Job's ActualOutputDirectory which you can access then to get your encoded video files, either save them elsewhere (second approach) or save them as a BLOB inside your db.

Storing as (LONG)BLOB (in my case I use MySQL as my DBMS, but other DBMS shouldn't be too different):

Creating the table:

CREATE TABLE `video` (
  `VideoID` int(11) NOT NULL,
  `VideoName` varchar(255) NOT NULL,
  `VideoSize` int(11) NOT NULL,
  `VideoFile` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Of course there's much missing e.g indexes, primary keys but that's not relevant to get the overall idea.

To store the file inside your DB you have to read it as a BLOB compatible object, which is a byte array (byte[]).

byte[] video = File.ReadAllBytes(filepath);

You can get the filepath by iterating through the ActualOutputDirectory:

foreach (string filepath in Directory.GetFiles(ActualOutputDirectory))
{
   StoreBLOBInDB(filepath);
}

The code of inserting your video file into a DB may look like this:

MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "INSERT INTO video (VideoName, VideoSize, VideoFile) VALUES (?videoname, ?videosize, ?videofile);";

byte[] video = File.ReadAllBytes(filepath);

MySqlParameter pVideoName= new MySqlParameter("?videoname", MySqlDbType.VarChar);
pVideoName.Value = Path.GetFileName(filepath);
MySqlParameter pVideoSize = new MySqlParameter("?videosize", MySqlDbType.Int32);
pVideoSize.Value = video.Length;
MySqlParameter pVideoBlob= new MySqlParameter("?videofile", MySqlDbType.Blob, video.Length);
pVideoBlob.Value = video;

command.Parameters.Add(pVideoName);
command.Parameters.Add(pVideoSize);
command.Parameters.Add(pVideoBlob);
command.ExecuteNonQuery();  

Opening/Closing of the connection:

string myConnectionString = "SERVER=localhost;" +
                         "DATABASE=encodingdb;" +
                         "UID=encoder;" +
                         "PASSWORD=encoder;";
this.connection = new MySqlConnection(myConnectionString);
this.connection.Open();

this.connection.Close();

The retrieval of the file BLOB is also quite easy:

MySqlCommand command = new MySqlCommand("", connection);
command.CommandText = "SELECT VideoName, VideoSize, VideoFile FROM video WHERE VideoName=?videoname;";
MySqlParameter pVideoname = new MySqlParameter("?videoname", MySqlDbType.VarChar);
pVideoname.Value = Path.GetFileName(videoName);
command.Parameters.Add(pVideoname);
MySqlDataReader videofileReader;
videofileReader = command.ExecuteReader();
byte[] videoBlob = new byte[0];
while (videofileReader.Read())
{
    int videoSize = videofileReader.GetInt32("VideoSize");
    videoBlob = new byte[videoSize];
    videofileReader.GetBytes(videofileReader.GetOrdinal("VideoFile"), 0, videoBlob, 0, videoSize);
}
File.WriteAllBytes(@"C:\Encoding\export.wmv", videoBlob);
videofileReader.Close();
CloseConnection();
相关问题