獲取多個拇指的問題是時間。到目前爲止,我還沒有能夠從單次掃描中獲得多個大拇指,所以這意味着我必須對三個圖像進行三次掃描。
在我的情況下,我花了幾秒鐘的視頻時間,然後從1/4,1/2和3/4中獲得三個圖像。但對於長視頻來說,這太慢了,即使使用ffmpeg的偏移值。如果您可以從視頻的前10到20秒內進行選擇,則會更好。
因此,我從一個預處理開始,在文件搜索時掃描圖像,然後任何沒有創建的拇指創建一個從幾秒鐘到視頻,這是一個快速的過程並且在它自己的主題中並不是很明顯,除非有很多視頻需要圖標,所以第一次運行會很慢。
然後和函數形式一樣,如果拇指不是很好,我允許從我的3個大拇指中進行選擇,這些大拇指是動態生成的。就我而言,我使用MDI,因此我可以將媒體套件的各個部分結合在一起。所以下面的部分是它自己的一個表單,當用戶以另一種形式選擇一個目錄時,文件列表'videoList'已被填充。
只是爲了完整性,我將包括我的MediaItem和MediaType枚舉。
忽略我命名我的臨時文件的方式,我仍在處理這個問題,因爲我將在稍後提供名稱的服務。您可以使用C#中相同的文件來獲取問題,這些文件只是在外部進程中創建或修改的,所以您確實需要一些巧妙的文件使用約定。
public partial class ThumbSelector : Form
{
public List<MediaItem> videoList = new List<MediaItem>();
private Random ra = new Random();
int iCount = 1;
public ThumbSelector()
{
InitializeComponent();
}
public void FillList()
{
if(videoList.Count() < 1)
return;
//only get mp4 files
var videosOnly = from n in videoList
where n.mainType == MediaMainType.MMT_VIDEO
select n;
lbVideoList.Items.Clear();
foreach (MediaItem mi in videosOnly)
{
lbVideoList.Items.Add(mi.shortName);
}
}
private void lbVideoList_SelectedIndexChanged(object sender, EventArgs e)
{
if(lbVideoList.SelectedItems.Count < 1)
return;
CreateThumbs(lbVideoList.SelectedItems[0].ToString());
}
private void CreateThumbs(string fShortName)
{
string sourceVideoName;
int sourceLength = 0;
int quarter = 0;
int half = 0;
int threequarter = 0;
string destDir = @"C:\Users\robert\dwhelper\VideoIcons";
string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
var sname = (from n in videoList
where n.shortName == fShortName
select n).First();
sourceVideoName = sname.fullName;
sourceLength = GetVideoLength(ffPath, sourceVideoName);
quarter = sourceLength/4;
half = sourceLength/2;
threequarter = quarter * 3;
Image im1;
Image im2;
Image im3;
string n1;
string n2;
string n3;
while (File.Exists(string.Concat(destDir, @"\", "x1_", iCount.ToString(), ".jpg")))
{
++iCount;
}
n1 = string.Concat("x1_", iCount.ToString(), ".jpg");
++iCount;
while (File.Exists(string.Concat(destDir, @"\", "x2_", iCount.ToString(), ".jpg")))
{
++iCount;
}
n2 = string.Concat("x2_", iCount.ToString(), ".jpg");
++iCount;
while (File.Exists(string.Concat(destDir, @"\", "x3_", iCount.ToString(), ".jpg")))
{
++iCount;
}
n3 = string.Concat("x3_", iCount.ToString(), ".jpg");
pic1.Image = null;
pic2.Image = null;
pic3.Image = null;
CreateThumbs(sname, quarter, n1);
im1 = Image.FromFile(string.Concat(destDir, @"\", n1));
pic1.Image = im1;
CreateThumbs(sname, half, n2);
im2 = Image.FromFile(string.Concat(destDir, @"\", n2));
pic2.Image = im2;
CreateThumbs(sname, threequarter, n3);
im3 = Image.FromFile(string.Concat(destDir, @"\", n3));
pic3.Image = im3;
}
private void CreateThumbs(MediaItem mi, int timeIn, string outName)
{
this.Cursor = Cursors.WaitCursor;
Process pth;
string destDir = @"C:\Users\robert\dwhelper\VideoIcons";
string sourceFile;
string destFile;
string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
string strCommand;
string fullCommand;
string[] ssplit;
sourceFile = mi.fullName;
ssplit = mi.shortName.Split('.');
destFile = string.Concat(destDir, "\\", outName);
strCommand = string.Concat(
" -i \"", sourceFile, "\" -r 1 -ss ", timeIn.ToString(), " -t 00:00:01 -f image2 \"", destFile, "\"");
fullCommand = string.Concat(ffPath, strCommand);
pth = new Process();
pth.StartInfo.FileName = ffPath;
pth.StartInfo.Arguments = strCommand;
pth.StartInfo.UseShellExecute = false;
pth.StartInfo.CreateNoWindow = true;
pth.Start();
pth.WaitForExit();
this.Cursor = Cursors.Default;
}
private int GetVideoLength(string ffmpeg, string sourceFile)
{
//exec('start C:\Inetpub\ffmpeg\ffmpeg -i "D:/Jaime-flex.wmv" 2>&1 | grep "Duration" | cut -d " " -f 4 - | sed s/,//', $output);
string result = "";
string strCommand = string.Concat(
" -i \"", sourceFile, "\" 2>");
Process pth = new Process();
pth.StartInfo.FileName = ffmpeg;
pth.StartInfo.Arguments = strCommand;
pth.StartInfo.UseShellExecute = false;
pth.StartInfo.CreateNoWindow = true;
pth.StartInfo.RedirectStandardOutput = true;
pth.StartInfo.RedirectStandardError = true;
pth.Start();
pth.WaitForExit();
//result = pth.StandardOutput.ReadToEnd();
//result = pth.StandardError.ReadToEnd();
string str = "";
string[] ssplit;
char[] splitChars = new char[] { '.', ' ' };
int seconds = 0;
while (!pth.StandardError.EndOfStream)
{
str = pth.StandardError.ReadLine();
if (str.Contains("Duration"))
{
ssplit = str.Split(splitChars);
seconds = GetSeconds(ssplit[3]);
}
}
return seconds;
}
private int GetSeconds(string p)
{
string[] ssplit = p.Split(':');
int ho = Convert.ToInt32(ssplit[0]) * 120;
int mi = Convert.ToInt32(ssplit[1]) * 60;
int se = Convert.ToInt32(ssplit[2]);
return ho + mi + se;
}
}
public class MediaItem
{
public string shortName { get; set; }
public string fullName { get; set; }
public string iconName { get; set; }
public MediaMainType mainType { get; set; }
public MediaSubType subType { get; set; }
public long length { get; set; }
}
public enum MediaMainType
{
MMT_VIDEO = 0, MMT_IMAGE, MMT_ICON, MMT_NOTMEDIA
}
public enum MediaSubType
{
MST_JPG = 0, MST_GIF, MST_BMP, MST_PNG, MST_MP4, MST_WMV, MST_FLV, MST_NOTMEDIA
3
如何挑選特定視頻幀作爲縮略圖使用?選擇一個足夠好的隨機畫面,還是你想選擇一個最具代表性的視頻? – misha 2010-11-30 05:38:59