C implements the method of taking screenshots from flv video files via ffmpeg
- 2021-01-03 21:01:40
- OfStack
This article is an example of how C# implements taking screenshots from flv video files via ffmpeg. Share to everybody for everybody reference. The specific analysis is as follows:
You need to download ffmpeg, which is open source, and the code is as follows:
using System;
using System.Configuration;
public class PublicMethod:System.Web.UI.Page
{
public PublicMethod()
{
}
// The file path
public static string ffmpegtool = "ffmpeg/ffmpeg.exe";
public static string mencodertool = "mencoder/mencoder.exe";
public static string flvtool = "flvtool/flvtool2.exe";//flv Markup tool
public static string upFile = "UpFiles" + "/";// Upload folder
public static string imgFile = "ImgFile" + "/";// Picture folder
public static string playFile = "PlayFiles" + "/";//flv folder
public static string xmlFile = "xmlFiles" + "/";//xml folder
public static string sizeOfImg = "240x180";// The width and height of the picture
public static string widthOfFile = "400";//flv File width
public static string heightOfFile = "350";//flv Height of file
//public static string ffmpegtool = ConfigurationManager.AppSettings["ffmpeg"];
//public static string mencodertool = ConfigurationManager.AppSettings["mencoder"];
//public static string upFile = ConfigurationManager.AppSettings["upfile"] + "/";
//public static string imgFile = ConfigurationManager.AppSettings["imgfile"] + "/";
//public static string playFile = ConfigurationManager.AppSettings["playfile"] + "/";
// File image size
//public static string sizeOfImg = ConfigurationManager.AppSettings["CatchFlvImgSize"];
// The file size
//public static string widthOfFile = ConfigurationManager.AppSettings["widthSize"];
//public static string heightOfFile = ConfigurationManager.AppSettings["heightSize"];
// // // Gets the name of the file
private System.Timers.Timer myTimer = new System.Timers.Timer(3000);// chronograph
public static string flvName = "";
public static string imgName = "";
public static string flvXml = "";
public static int pId = 0;
public static string GetFileName(string fileName)
{
int i = fileName.LastIndexOf("\") + 1;
string Name = fileName.Substring(i);
return Name;
}
// Gets the file extension
public static string GetExtension(string fileName)
{
int i = fileName.LastIndexOf(".")+1;
string Name = fileName.Substring(i);
return Name;
}
//
#region // run FFMpeg Video decoding, ( This is the absolute path )
/// <summary>
/// Convert the file and save it under the specified folder ( This is the absolute path )
/// </summary>
/// <param name="fileName"> Path to upload video file (original file) </param>
/// <param name="playFile"> Path to the converted file (network player file) </param>
/// <param name="imgFile"> The path to a captured image from a video file </param>
/// <returns> successful : Returns the image virtual address ; failure : Returns an empty string </returns>
public void ChangeFilePhy(string fileName, string playFile, string imgFile)
{
// achieve ffmpeg.exe The path of the , Path configuration in Web.Config In the , Such as :<add key="ffmpeg" value="E:aspx1ffmpeg.exe" />
string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return;
}
// Get the picture and (.flv) File relative path / The final path to the database , Such as :/Web/User1/00001.jpg
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
// The size of the screenshot , The configuration in Web.Config In the , Such as :<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = PublicMethod.sizeOfImg;
System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
//ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -t 0.05 -s " + FlvImgSize + " " + flv_img;
try
{
// conversion
System.Diagnostics.Process.Start(FilestartInfo);
// screenshots
CatchImg(fileName, imgFile);
//System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
}
}
#endregion
#region screenshots
public string CatchImg(string fileName,string imgFile)
{
//
string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
//
string flv_img =imgFile+".jpg";
//
string FlvImgSize = PublicMethod.sizeOfImg;
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
ImgstartInfo.Arguments = " -i " + fileName + " -y -f image2 -ss 2 -vframes 1 -s " + FlvImgSize + " " + flv_img;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
return "";
}
//
catchFlvTool(fileName);
if (System.IO.File.Exists(flv_img))
{
return flv_img;
}
return "";
}
#endregion
#region // run FFMpeg Video decoding, ( Here is the ( virtual ) Relative paths )
/// <summary>
/// Convert the file and save it under the specified folder ( Here's the relative path )
/// </summary>
/// <param name="fileName"> Path to upload video file (original file) </param>
/// <param name="playFile"> Path to the converted file (network player file) </param>
/// <param name="imgFile"> The path to a captured image from a video file </param>
/// <returns> successful : Returns the image virtual address ; failure : Returns an empty string </returns>
public void ChangeFileVir(string fileName, string playFile, string imgFile)
{
// achieve ffmpeg.exe The path of the , Path configuration in Web.Config In the , Such as :<add key="ffmpeg" value="E:\aspx1\ffmpeg.exe" />
string ffmpeg = Server.MapPath(PublicMethod.ffmpegtool);
if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(fileName)))
{
return;
}
// Get the picture and (.flv) File relative path / The final path to the database , Such as :/Web/User1/00001.jpg
string flv_img = System.IO.Path.ChangeExtension(Server.MapPath(imgFile), ".jpg");
string flv_file = System.IO.Path.ChangeExtension(Server.MapPath(playFile), ".flv");
// The size of the screenshot , The configuration in Web.Config In the , Such as :<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = PublicMethod.sizeOfImg;
System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// Here the combination is ffmpeg.exe The parameters required for the file are enough , The command here is ffmpeg 0.4.9 Debugging through
//ffmpeg -i F:\01.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
FilestartInfo.Arguments = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
try
{
System.Diagnostics.Process ps = new System.Diagnostics.Process();
ps.StartInfo = FilestartInfo;
ps.Start();
Session.Add("ProcessID", ps.Id);
Session.Add("flv", flv_file);
Session.Add("img", imgFile);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Test);
myTimer.Enabled = true;
}
catch
{
}
}
#endregion
#region // run mencoder Video decoder conversion ( Here is the ( An absolute path ))
public void MChangeFilePhy(string vFileName, string playFile, string imgFile)
{
string tool = Server.MapPath(PublicMethod.mencodertool);
//string mplaytool = Server.MapPath(PublicMethod.ffmpegtool);
if ((!System.IO.File.Exists(tool)) || (!System.IO.File.Exists(vFileName)))
{
return;
}
string flv_file = System.IO.Path.ChangeExtension(playFile, ".flv");
// The size of the screenshot , The configuration in Web.Config In the , Such as :<add key="CatchFlvImgSize" value="240x180" />
string FlvImgSize = PublicMethod.sizeOfImg;
System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(tool);
FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
FilestartInfo.Arguments = " " + vFileName + " -o " + flv_file + " -of lavf -lavfopts i_certify_that_my_video_stream_does_not_use_b_frames -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=200:mbd=2:mv0:trell:v4mv:cbp:last_pred=1:dia=-1:cmp=0:vb_strategy=1 -vf scale=" + widthOfFile + ":" +heightOfFile + " -ofps 12 -srate 22050";
try
{
System.Diagnostics.Process ps = new System.Diagnostics.Process();
ps.StartInfo = FilestartInfo;
ps.Start();
Session.Add("ProcessID", ps.Id);
Session.Add("flv", flv_file);
Session.Add("img", imgFile);
//pId = ps.Id;
//flvName = flv_file;
//imgName = imgFile;
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Test);
myTimer.Enabled = true;
}
catch
{
}
}
/// <summary>
/// Chronograph function, automatic saving screenshots
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void myTimer_Test(object sender, System.Timers.ElapsedEventArgs e)
{
if (!object.Equals(null, Session["ProcessID"]))
{
try
{
System.Diagnostics.Process prs = System.Diagnostics.Process.GetProcessById(int.Parse(Session["ProcessID"].ToString()));
if (prs.HasExited)
{
CatchImg(Session["flv"].ToString(), Session["img"].ToString());
catchFlvTool(Session["flv"].ToString());
myTimer.Enabled = false;
myTimer.Close();
myTimer.Dispose();
Session.Abandon();
}
}
catch
{
CatchImg(Session["flv"].ToString(), Session["img"].ToString());
catchFlvTool(Session["flv"].ToString());
myTimer.Enabled = false;
myTimer.Close();
myTimer.Dispose();
Session.Abandon();
}
}
}
#endregion
public string catchFlvTool(string fileName)
{
//
string flvtools = Server.MapPath(PublicMethod.flvtool);
//
string flv_xml = fileName.Replace(".flv", ".xml").Replace(PublicMethod.upFile.Replace("/", ""), PublicMethod.xmlFile.Replace("/", ""));
//
System.Diagnostics.ProcessStartInfo ImgstartInfo = new System.Diagnostics.ProcessStartInfo(flvtools);
ImgstartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//
ImgstartInfo.Arguments = " " + fileName + " -UPx " + fileName + " > " + flv_xml;
try
{
System.Diagnostics.Process.Start(ImgstartInfo);
}
catch
{
return "";
}
//
if (System.IO.File.Exists(flv_xml))
{
return flv_xml;
}
return "";
}
}
Hopefully this article has helped you with your C# programming.