public string Cut(string ffmpegPath, string videoPath, string savePath, string imgSize, int sleepTime = 0xbb8)
{
if (File.Exists(ffmpegPath) && File.Exists(videoPath))
{
string str = savePath;
string str2 = imgSize;
string path = savePath.Substring(0, savePath.LastIndexOf(@"\"));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegPath)
{
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = " -i " + videoPath + " -y -f image2 -t 0.001 -s " + str2 + " " + savePath
};
try
{
Process.Start(startInfo);
Thread.Sleep(sleepTime);
}
catch
{
return string.Empty;
}
if (File.Exists(savePath))
{
return savePath;
}
}
return string.Empty;
}
public string Cut(string ffmpegPath, string videoPath, string savePath, string imgSize, int sleepTime = 0xbb8, float startPos = 10, float cutTime = 1)
{
if (File.Exists(ffmpegPath) && File.Exists(videoPath))
{
string str = savePath;
string str2 = imgSize;
string path = savePath.Substring(0, savePath.LastIndexOf(@"\"));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegPath)
{
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = string.Format(" -i {0} -t {1} -ss {2} -s {3} {4}", new object[] { videoPath, cutTime, startPos, str2, savePath })
};
try
{
Process.Start(startInfo);
Thread.Sleep(sleepTime);
}
catch
{
return string.Empty;
}
if (File.Exists(savePath))
{
return savePath;
}
}
return string.Empty;
}
protected void btnFetch_Click(object sender, EventArgs e)
{
try
{
string imgDir = Server.MapPath(@"ffmpeg\\");
string ffmpegPath = imgDir + "ffmpeg.exe";
string imgSize = txtImgSize.Text.Trim();
int sleepTime = 500;
string ret = string.Empty;
float startPos = Convert.ToSingle(txtStartPos.Text.Trim());
float cutTime = Convert.ToSingle(txtCutTime.Text.Trim());
string temp = @"" + System.Configuration.ConfigurationManager.AppSettings["filepath"] + @"" + txtDownloadUrl.Text.Trim().Replace(@"/", @"\");
temp = temp.Replace(@"\\", @"\");
if (System.IO.File.Exists(temp))
{
ret = Cut(ffmpegPath, temp, imgDir + "img\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg", imgSize, sleepTime);
if (string.IsNullOrEmpty(ret))
{
ret = Cut(ffmpegPath, temp, imgDir + "img\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg", imgSize, sleepTime, startPos, cutTime);
}
}
litMsg.Text = ret;
imgDiagram.ImageUrl = ret.Replace(@"D:\site\20150914\", "/").Replace(@"\", "/");
}
catch (Exception ex) {
litMsg.Text = ex.Message;
}
}