Upload video to generate thumbnail

DEMO

     ///  <summary> 
        /// Upload video to generate thumbnail
         ///  </summary> 
        ///  <param name="vFileName"></param> 
        ///  <returns></returns> 
        public  string CatchImg( string vFileName)
        {
            try
            {
                string ffmpeg = Setting.SiteSettings.FilePath + "/ffmpeg.exe";
                if ((!System.IO.File.Exists(ffmpeg)) || (!System.IO.File.Exists(vFileName)))
                {
                    return "Error";
                }
                string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");
                string flv_img_p = flv_img;
                string FlvImgSize = "140x110";
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ffmpeg);
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                startInfo.Arguments = " -i " + vFileName + " -y -f image2 -t 0.1 -s " + FlvImgSize + " " + flv_img_p;
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return "Error";
                }
                System.Threading.Thread.Sleep(1000);
                if (System.IO.File.Exists(flv_img_p))
                {
                    var imagePath = vFileName.Substring(0, vFileName.LastIndexOf("/"));
                    imagePath = imagePath.Substring( 0 , imagePath.LastIndexOf( " / " ))+ " /Image " ;
                     if (System.IO.Directory.Exists(imagePath) == false ) // Create a file folder if it does not exist 
                    {
                        System.IO.Directory.CreateDirectory(imagePath);
                    }
                    System.IO.File.Move(flv_img_p, flv_img.Replace("Video", "Image")); //移动文件  
                    return flv_img_p.Replace("Video", "Image");
                }
                return "Error";
            }
            catch
            {
                return "Error";
            }
        }

Need to pay attention:

This needs to download a file called ffmpeg.exe and install it in the path you specified

Then which frame is taken in the above code is the first frame that is written to death

Finally, there is System.Threading.Thread.Sleep(1000); it is the time left for software processing. I saw the source code for 3s, but my actual project stipulates that the video is relatively small, so I only left 1s. I feel the best is to set Into a parameter, it is automatically adjusted according to the size of the incoming video, to be optimized~

 

There are also a few codes that I found easily at that time, about file processing, please post it

       ///  <summary>   
       /// Delete all pictures under the folder  
        ///  </summary>   
       ///  <param name="oldpngPath"> File path to be deleted </param>   
       ///  <param name="newpngPath "> The new directory to be deleted </param>   
       ///  <param name="geshi"> The format of the operation file, such as: *.png, *.xml </param>   
       public  void deleteAll( string delpath, string format )  
       {  
           string[] files = Directory.GetFiles(delpath, format[0]);  
           foreach (string file in files)  
           {  
               File.Delete(file);  
           }  
       }  
       ///  <summary>   
       /// Copy all pictures under the folder  
        ///  </summary>   
       ///  <param name="oldpngPath">The path of the file to be copied </param>   
       ///  <param name="newpngPath "> Copy to the new directory </param>   
       ///  <param name="geshi"> The format of the operation file, such as: *.png, *.xml </param>   
       public  void copyALl( string oldpngPath, string newpngPath, string format )  
       {  
           string[] files = Directory.GetFiles(oldpngPath, format[0]);  
           foreach (string file in files)  
           {  
               string otherFile = Path.Combine(oldpngPath, Path.GetFileName(file));  
               File.Copy(file, newpngPath + "\\" + Path.GetFileName(file));  
           }  
       }  
       ///  <summary>   
       /// Move all files in the folder  
        ///  </summary>   
       ///  <param name="olderpath"> File directory to be moved </param>   
       ///  <param name= "topath"> new directory </param>   
       public  void moveAll( string olderpath, string topath)  
       {  
           string[] files = Directory.GetFiles(delpath, format[0]);  
           foreach (string file in files)  
           {  
               File.Move(file, topath); // Move the file   
           }  
       }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024645&siteId=291194637