SoFunction
Updated on 2025-03-06

C# video conversion class sharing

This article shares the specific code of C# video conversion for your reference. The specific content is as follows

VideoConvert

using ;
using ;

namespace 
{

 public class VideoConvert : 
 {
  public VideoConvert()
  { }

  string[] strArrMencoder = new string[] { "wmv", "rmvb", "rm" };
  string[] strArrFfmpeg = new string[] { "asf", "avi", "mpg", "3gp", "mov" };

  #region configuration  public static string ffmpegtool = ["ffmpeg"];
  public static string mencodertool = ["mencoder"];
  public static string savefile = ["savefile"] + "/";
  public static string sizeOfImg = ["CatchFlvImgSize"];
  public static string widthOfFile = ["widthSize"];
  public static string heightOfFile = ["heightSize"];
  #endregion

  #region Get the name of the file  /// <summary>
  /// Get the file name  /// </summary>
  public static string GetFileName(string fileName)
  {
   int i = ("\\") + 1;
   string Name = (i);
   return Name;
  }
  #endregion

  #region Get file extension  /// <summary>
  /// Get the file extension  /// </summary>
  public static string GetExtension(string fileName)
  {
   int i = (".") + 1;
   string Name = (i);
   return Name;
  }
  #endregion

  #region Get file type  /// <summary>
  /// Get file type  /// </summary>
  public string CheckExtension(string extension)
  {
   string m_strReturn = "";
   foreach (string var in )
   {
    if (var == extension)
    {
     m_strReturn = "ffmpeg"; break;
    }
   }
   if (m_strReturn == "")
   {
    foreach (string var in strArrMencoder)
    {
     if (var == extension)
     {
      m_strReturn = "mencoder"; break;
     }
    }
   }
   return m_strReturn;
  }
  #endregion

  #region video format to Flv  /// <summary>
  /// Convert video format to Flv  /// </summary>
  /// <param name="vFileName">Original video file address</param>  /// <param name="ExportName">Generated Flv file address</param>  public bool ConvertFlv(string vFileName, string ExportName)
  {
   if ((!(ffmpegtool)) || (!((vFileName))))
   {
    return false;
   }
   vFileName = (vFileName);
   ExportName = (ExportName);
   string Command = " -i \"" + vFileName + "\" -y -ab 32 -ar 22050 -b 800000 -s 480*360 \"" + ExportName + "\""; //Flv format    p = new ();
    = ffmpegtool;
    = Command;
    = ("~/tools/");
    = false;
    = true;
    = true;
    = true;
    = false;
   ();
   ();
   ();
   ();
   ();
   return true;
  }
  #endregion

  #region Generate thumbnails of Flv videos  /// &lt;summary&gt;
  /// Generate thumbnails of Flv videos  /// &lt;/summary&gt;
  /// <param name="vFileName">Video file address</param>  public string CatchImg(string vFileName)
  {
   if ((!(ffmpegtool)) || (!((vFileName)))) return "";
   try
   {
    string flv_img_p = (0,  - 4) + ".jpg";
    string Command = " -i " + (vFileName) + " -y -f image2 -t 0.1 -s " + sizeOfImg + " " + (flv_img_p);
     p = new ();
     = ffmpegtool;
     = Command;
     = ;
    try
    {
     ();
    }
    catch
    {
     return "";
    }
    finally
    {
     ();
     ();
    }
    (4000);

    //Note: After the image is successfully intercepted, it takes a long time to write the data from the memory cache to disk, about 3, 4 seconds or even longer;    if (((flv_img_p)))
    {
     return flv_img_p;
    }
    return "";
   }
   catch
   {
    return "";
   }
  }
  #endregion

  #region Video decoding (absolute path) running FFMpeg  /// &lt;summary&gt;
  /// Convert the file and save it in the specified folder  /// &lt;/summary&gt;
  /// <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: Return the virtual address of the picture; Failure: Return the empty string</returns>  public string ChangeFilePhy(string fileName, string playFile, string imgFile)
  {
   string ffmpeg = ();
   if ((!(ffmpeg)) || (!(fileName)))
   {
    return "";
   }
   string flv_file = (playFile, ".flv");
   string FlvImgSize = ;
    FilestartInfo = new (ffmpeg);
    = ;
    = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
   try
   {
    (FilestartInfo);//Convert    CatchImg(fileName, imgFile); //screenshot   }
   catch
   {
    return "";
   }
   return "";
  }

  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 "";
   }
   if ((flv_img))
   {
    return flv_img;
   }
   return "";
  }
  #endregion

  #region Video decoding (relative path) running FFMpeg  /// &lt;summary&gt;
  /// Convert the file and save it in the specified folder  /// &lt;/summary&gt;
  /// <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: Return the virtual address of the picture; Failure: Return the empty string</returns>  public string ChangeFileVir(string fileName, string playFile, string imgFile)
  {
   string ffmpeg = ();
   if ((!(ffmpeg)) || (!(fileName)))
   {
    return "";
   }
   string flv_img = ((imgFile), ".jpg");
   string flv_file = ((playFile), ".flv");
   string FlvImgSize = ;

    ImgstartInfo = new (ffmpeg);
    = ;
    = " -i " + fileName + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img;

    FilestartInfo = new (ffmpeg);
    = ;
    = " -i " + fileName + " -ab 56 -ar 22050 -b 500 -r 15 -s " + widthOfFile + "x" + heightOfFile + " " + flv_file;
   try
   {
    (FilestartInfo);
    (ImgstartInfo);
   }
   catch
   {
    return "";
   }

   ///Note: After the image is successfully intercepted, it takes a long time to write the data from the memory cache to disk, about 3, 4 seconds or even longer;   ///This requires delay and then detection. My server delays 8 seconds, that is, if the picture still does not exist after more than 8 seconds, the screenshot is considered to have failed;   if ((flv_img))
   {
    return flv_img;
   }
   return "";
  }
  #endregion

  #region Video decoder conversion running menucoder (absolute path)  /// &lt;summary&gt;
  /// Run the video decoder conversion of mencoder  /// &lt;/summary&gt;
  public string MChangeFilePhy(string vFileName, string playFile, string imgFile)
  {
   string tool = ();
   if ((!(tool)) || (!(vFileName)))
   {
    return "";
   }
   string flv_file = (playFile, ".flv");
   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
   {
    (FilestartInfo);
    CatchImg(flv_file, imgFile);
   }
   catch
   {
    return "";
   }
   return "";
  }
  #endregion
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.