2017-05-16 64 views
1

我正在使用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/

親切的問候

回答

2

有兩種方法來實現這一目標。

  1. 您上傳的視頻文件直接到您的DB(你需要在你的數據庫的視頻表中的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(); 
+0

謝謝你的時間!這正是我想要的,祝你有美好的一天。 – Fearcoder

+0

不客氣,祝你有美好的一天。快樂編碼:-) – jAC

+0

在MSSQL中,您可以使用FileStreams來保存Blob數據。通過將文件系統上的varbinary(max)二進制大對象(BLOB)數據存儲爲文件,FILESTREAM將SQL Server數據庫引擎與NTFS文件系統集成在一起。在這裏查看更多詳情https://technet.microsoft.com/zh-cn/library/bb933993(v=sql.105).aspx –

相關問題