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

标签 c# .net database video encoding

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

MediaItem item = new MediaItem(@"C:\myvideo.wmv");
item.SourceClips[0].EndTime = new TimeSpan(0, 0, 15);

TimeSpan timeStart = new TimeSpan(0, 0, 30);
TimeSpan timeEnd = new TimeSpan(0, 0, 45);
item.SourceClips.Add(new SourceClip(timeStart, timeEnd));

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

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

亲切的问候

最佳答案

有两种方法可以实现这一点。

  1. 您将视频文件直接上传到您的数据库(您的数据库视频表中需要一个 BLOB 字段)
  2. 您将视频文件保存在文件系统中,并将这些文件的路径存储在数据库中(首选)。

为什么我更喜欢第二种方法?
数据库旨在非常非常快地处理小对象。另一方面,数据库中较大的对象会随着时间的推移而退化,访问速度也会受到影响。第二种方法也更简单,但让我们来看看第一种。

编码:

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
}

编码后,您将在JobActualOutputDirectory 中获得一个目录字符串,然后您可以访问该目录字符串以获取编码后的视频文件,或者将它们保存在其他地方(第二种方法)或将它们保存为数据库中的 BLOB

存储为 (LONG)BLOB(在我的例子中,我使用 MySQL 作为我的 DBMS,但其他 DBMS 应该没有太大的不同):

创建表:

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;

当然还有很多缺失,例如索引、主键,但这与获得整体思路无关。

要将文件存储在您的数据库中,您必须将其作为 BLOB 兼容对象读取,这是一个字节数组 (byte[])。

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

您可以通过遍历 ActualOutputDirectory 来获取文件路径:

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

将视频文件插入数据库的代码可能如下所示:

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();  

打开/关闭连接:

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

this.connection.Close();

文件 BLOB 的检索也很容易:

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();

关于c# - 将拆分视频保存到数据库中并检索它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995980/

相关文章:

database - 从 MySQL 转行的 Web 开发人员,您现在不能没有哪些 postgresql 功能?

c# - 是否可以限制 EventWaitHandle 的设置/重置?

mysql - 比较 MySQL 中的变量 VS 数组

c# - 如何跳过 DevExpress ColumnView 的 CellValueChanging 状态?

c# - 如何将特定输出值从 exe (c#) 发送到 powershell 脚本

.net - 如何正确构建 netstandard1.0 NuGet 包的依赖关系?

javascript - X-Content-Type-Options = nosniff 解决方法

php - 根据值比较并删除行 [MYSQLi]

c# - 正则表达式获取我的文件的父目录在字符串文件路径中

c# - CookieContainer 手动 cookie 覆盖