SoFunction
Updated on 2025-03-06

C# implements the method of screenshotting from flv video files through ffmpeg

using System;
using ;
public class PublicMethod:
{
    public PublicMethod()
    {
    }
//File path
    public static string ffmpegtool = "ffmpeg/";        
    public static string mencodertool = "mencoder/";
public static string flvtool = "flvtool/";//flv tag tool
public static string upFile = "UpFiles" + "/";//Upload folder
public static string imgFile = "ImgFile" + "/";// Image 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";//The width of flv file
public static string heightOfFile = "350";//The height of flv file
    //public static string ffmpegtool = ["ffmpeg"];
    //public static string mencodertool = ["mencoder"];
    //public static string upFile = ["upfile"] + "/";
    //public static string imgFile = ["imgfile"] + "/";
    //public static string playFile = ["playfile"] + "/";
//File picture size
    //public static string sizeOfImg = ["CatchFlvImgSize"];
//File size
    //public static string widthOfFile = ["widthSize"];
    //public static string heightOfFile = ["heightSize"];
//   // // Get the file name
private myTimer = new (3000);//Timer
    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 = ("\") + 1;
        string Name = (i);
        return Name;
    }
//Get the file extension
    public static string GetExtension(string fileName)
    {
        int i = (".")+1;
        string Name = (i);
        return Name;
    }
    //
#region // Run FFMpeg's video decoding, (here is the absolute path)
    /// <summary>
/// Convert the file and save it under the specified folder (here is the absolute path)
    /// </summary>
/// <param name="fileName">Path to upload video file (original file)</param>
/// <param name="playFile">Path of the converted file (network playback file)</param>
/// <param name="imgFile">Image path captured from video file</param>
/// <returns>Success: Returns the virtual address of the picture;  Failed: Returns the empty string</returns>
    public void ChangeFilePhy(string fileName, string playFile, string imgFile)
    {
//The path obtained is configured in the path, such as:<add   key="ffmpeg"   value="E:"  />
        string ffmpeg = ();
        if ((!(ffmpeg)) || (!(fileName)))
        {
            return;
        }
//Get the relative path of the image and (.flv) file/The path that is last stored to the database, such as: /Web/User1/
        string flv_file = (playFile, ".flv");
//The size of the screenshot is configured, such as:<add   key="CatchFlvImgSize"   value="240x180"  />
        string FlvImgSize = ;
        FilestartInfo = new (ffmpeg);
        = ;
        = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
        // = "   -i   " + fileName + "   -y   -f   image2   -t   0.05   -s   " + FlvImgSize + "   " + flv_img;
        try
        {
//Convert
            (FilestartInfo);
//screenshot
            CatchImg(fileName, imgFile);
            //(ImgstartInfo);
        }
        catch
        {
        }
    }
    #endregion
#region Screenshot
    public string CatchImg(string fileName,string imgFile)
    {
        //
        string ffmpeg = ();
        //
        string flv_img =imgFile+".jpg";
        //
        string FlvImgSize = ;
        //
        ImgstartInfo = new (ffmpeg);
        = ;
        //
        = "   -i   " + fileName + "  -y  -f  image2   -ss 2 -vframes 1  -s   " + FlvImgSize + "   " + flv_img;
        try
        {
            (ImgstartInfo);
        }
        catch
        {
            return "";
        }
        //
        catchFlvTool(fileName);
        if ((flv_img))
        {
            return flv_img;
        }
        return "";
    }
    #endregion
#region // Run FFMpeg's video decoding, (here is the (virtual) relative path)
    /// <summary>
/// Convert the file and save it under the specified folder (here is the relative path)
    /// </summary>
/// <param name="fileName">Path to upload video file (original file)</param>
/// <param name="playFile">Path of the converted file (network playback file)</param>
/// <param name="imgFile">Image path captured from video file</param>
/// <returns>Success: Returns the virtual address of the picture;  Failed: Returns the empty string</returns>
    public void ChangeFileVir(string fileName, string playFile, string imgFile)
    {
//The path obtained is configured in the path, such as:<add   key="ffmpeg"   value="E:\aspx1\"  />
        string ffmpeg = ();
        if ((!(ffmpeg)) || (!(fileName)))
        {
            return;
        }
//Get the relative path of the image and (.flv) file/The path that is last stored to the database, such as: /Web/User1/
        string flv_img = ((imgFile), ".jpg");
        string flv_file = ((playFile), ".flv");
//The size of the screenshot is configured, such as:<add   key="CatchFlvImgSize"   value="240x180"  />
        string FlvImgSize = ;
        FilestartInfo = new (ffmpeg);
        = ;
//This is the required parameters to be combined into the file. The command here is debugged and passed in ffmpeg   0.4.9
        //ffmpeg -i F:\ -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\
        = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
        try
        {
            ps = new ();
            = FilestartInfo;
            ();
            ("ProcessID", );
            ("flv", flv_file);
            ("img", imgFile);
            += new (myTimer_Test);
            = true;
        }
        catch
        {
        }
    }
    #endregion
#region // Run the video decoder conversion of mencoder (here is (absolute path))
    public void MChangeFilePhy(string vFileName, string playFile, string imgFile)
    {
        string tool = ();
        //string mplaytool = ();
        if ((!(tool)) || (!(vFileName)))
        {
            return;
        }
        string flv_file = (playFile, ".flv");
//The size of the screenshot is configured, such as:<add   key="CatchFlvImgSize"   value="240x180"  />
        string FlvImgSize = ;
        FilestartInfo = new (tool);
        = ;
        = " " + 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
        {
            ps = new ();
            = FilestartInfo;
            ();
            ("ProcessID", );
            ("flv", flv_file);
            ("img", imgFile);
            //pId = ;
            //flvName = flv_file;
            //imgName = imgFile;
            += new (myTimer_Test);
            = true;
        }
        catch
        {
        }
    }
    /// <summary>
/// Timer function, automatically save screenshots
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void myTimer_Test(object sender, e)
    {
        if (!(null, Session["ProcessID"]))
        {
            try
            {
                prs = ((Session["ProcessID"].ToString()));
                if ()
                {
                    CatchImg(Session["flv"].ToString(), Session["img"].ToString());
                    catchFlvTool(Session["flv"].ToString());
                    = false;
                    ();
                    ();
                    ();
                }
            }
            catch
            {
                CatchImg(Session["flv"].ToString(), Session["img"].ToString());
                catchFlvTool(Session["flv"].ToString());
                = false;
                ();
                ();
                ();
            }
        }
    }
    #endregion
    public string catchFlvTool(string fileName)
    {
        //
        string flvtools = ();
        //
        string flv_xml = (".flv", ".xml").Replace(("/", ""), ("/", ""));
        //
        ImgstartInfo = new (flvtools);
        = ;
        //
        = "   " + fileName + "   -UPx   " + fileName + "  >  " + flv_xml;
        try
        {
            (ImgstartInfo);
        }
        catch
        {
            return "";
        }
        //
        if ((flv_xml))
        {
            return flv_xml;
        }
        return "";
    }
}