kenchan0752

(转))C#视频转换为flv代码 .

http://blog.csdn.net/binbin_520/article/details/4059396

 

 在上次随笔 Asp.net FMS 开发视频网站 中,好多朋友提到其他视频格式转化成FLV格式的问题,经过网上搜索资料研习整理,现经我的一点思路分享给大家:
        我添加了一个转换FLV工程 VideoConvert:

         1。配置文件里添加

  1. <appSettings>  
  2.   
  3.     <!--convert tools path-->  
  4.     <add key="FfmpegPath" value="D:/tools/"/>      
  5.   
  6.     <!-- setting -->  
  7.     <add key="ThreadCount" value="5" />  
  8.     <add key="BatchSize" value="10" />  
  9.    <add key="QueueTimeout" value="20" />  
  10.   <add key="TransactionTimeout" value="30" />  
  11.  </appSettings>  

 

  2。添加一个接口 

  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Text;  
  4.    
  5.  namespace VideoConvert  
  6.  {  
  7.      public interface IConvert  
  8.     {  
  9.    
  10.         /**//// <summary>  
  11.         /// 将视频文件转换为Flv格式   
  12.         /// </summary>   
  13.         /// <param name="sourceFile">要转换的文件</param>   
  14.         /// <returns></returns>   
  15.         bool Convert(string sourceFile);  
  16.   
  17.   
  18.   
  19.         /**//// <summary>  
  20.         /// 获取缩略图   
  21.         /// </summary>   
  22.         /// <param name="sourceFile"></param>   
  23.         /// <returns></returns>   
  24.         bool GetSmallImage(string sourceFile);  
  25.          
  26.   
  27.     }  
  28. }  

 

转换工具设定继承 IConvert

  1. using System;  
  2.  2using System.Collections.Generic;  
  3.  3using System.Text;  
  4.  4using System.Configuration;  
  5.  5  
  6.  6  
  7.  7namespace VideoConvert  
  8.  8{  
  9.  9    public class FfmpegConvert : IConvert  
  10. 10    {  
  11. 11  
  12. 12  
  13. 13        /**//// <summary>  
  14. 14        /// 转换软件所在的路径   
  15. 15        /// </summary>   
  16. 16        private string ConvertTool = ConfigurationManager.AppSettings["FfmpegPath"] + "ffmpeg.exe";  
  17. 17  
  18. 18  
  19. 19        /**//// <summary>  
  20. 20        /// 构造函数   
  21. 21        /// </summary>   
  22. 22        public FfmpegConvert()  
  23. 23        {  
  24. 24  
  25. 25        }  
  26. 26  
  27. 27  
  28. 28        /**//// <summary>  
  29. 29        /// 将视频文件转换为Flv格式   
  30. 30        /// </summary>   
  31. 31        /// <param name="sourceFile">要转换的文件</param>   
  32. 32        /// <returns></returns>   
  33. 33        public bool Convert(string sourceFile)  
  34. 34        {  
  35. 35            try  
  36. 36            {  
  37. 37                //文件名是否为空   
  38. 38                if (string.IsNullOrEmpty(sourceFile)) return false;  
  39. 39                //检测文件是否存在   
  40. 40  
  41. 41  
  42. 42                string TargetFile = sourceFile.Substring(0, sourceFile.Length - 4) + ".flv";  
  43. 43                string Argu = @"-i " + sourceFile + " -ab 56 -ar 22050 -b 500 -r 15 -s 480x360 " + TargetFile;  
  44. 44  
  45. 45                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ConvertTool, Argu);  
  46. 46                System.Diagnostics.Process.Start(startInfo);  
  47. 47                System.Threading.Thread.Sleep(6000);  
  48. 48                return true;  
  49. 49  
  50. 50            }  
  51. 51            catch (Exception exp)  
  52. 52            {  
  53. 53                throw exp;  
  54. 54            }  
  55. 55  
  56. 56        }  
  57. 57  
  58. 58  
  59. 59        /**//// <summary>  
  60. 60        /// 获取缩略图   
  61. 61        /// </summary>   
  62. 62        /// <param name="sourceFile"></param>   
  63. 63        /// <returns></returns>   
  64. 64        public bool GetSmallImage(string sourceFile)  
  65. 65        {  
  66. 66  
  67. 67            //文件名是否为空   
  68. 68            if (string.IsNullOrEmpty(sourceFile)) return false;  
  69. 69            //检测文件是否存在   
  70. 70  
  71. 71            try  
  72. 72            {  
  73. 73                string TargetFile = sourceFile.Substring(0, sourceFile.Length - 4) + ".jpg";  
  74. 74                string Argu = @"-i " + sourceFile + " -y -f image2 -ss 08.010 -t 0.001 -s 352x240 " + TargetFile;  
  75. 75                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(ConvertTool, Argu);  
  76. 76                System.Diagnostics.Process.Start(startInfo);  
  77. 77                System.Threading.Thread.Sleep(6000);  
  78. 78  
  79. 79  
  80. 80                //必须等待进行完成后才能返回结果   
  81. 81  
  82. 82  
  83. 83                return true;  
  84. 84  
  85. 85            }  
  86. 86            catch (Exception exp)  
  87. 87            {  
  88. 88                throw exp;  
  89. 89            }  
  90. 90  
  91. 91        }  
  92. 92  
  93. 93    }  
  94. 94}  

 

4.

  1. using System;  
  2.   2using System.Collections.Generic;  
  3.   3using System.Configuration;  
  4.   4using System.Text;  
  5.   5using System.Threading;  
  6.   6using System.Transactions;  
  7.   7using VideoConvert;  
  8.   8  
  9.   9namespace VideoConvert  
  10.  10{  
  11.  11    class Program  
  12.  12    {  
  13.  13  
  14.  14        //threadCount    
  15.  15        private static int threadCount = int.Parse(ConfigurationManager.AppSettings["ThreadCount"]);  
  16.  16             
  17.  17        private static IConvert tool = new FfmpegConvert();  
  18.  18  
  19.  19        //finished count   
  20.  20        private static int completeCount = 0;  
  21.  21  
  22.  22        static void Main(string[] args)  
  23.  23        {             
  24.  24  
  25.  25  
  26.  26            Thread workTicketThread;  
  27.  27            Thread[] workerThreads = new Thread[threadCount];  
  28.  28  
  29.  29            for (int i = 0; i < threadCount; i++)  
  30.  30            {  
  31.  31  
  32.  32                workTicketThread = new Thread(new ThreadStart(ProcessVideo));  
  33.  33  
  34.  34                // Make this a background thread, so it will terminate when the main thread/process is de-activated   
  35.  35                workTicketThread.IsBackground = true;  
  36.  36                workTicketThread.SetApartmentState(ApartmentState.STA);  
  37.  37  
  38.  38                // Start the Work   
  39.  39                workTicketThread.Start();  
  40.  40                workerThreads[i] = workTicketThread;  
  41.  41            }  
  42.  42  
  43.  43            Console.WriteLine("Converting begin. press Enter stop");  
  44.  44            Console.ReadLine();  
  45.  45            Console.WriteLine("cancel");  
  46.  46  
  47.  47            //abort all threads   
  48.  48            for (int i = 0; i < workerThreads.Length; i++)  
  49.  49            {  
  50.  50  
  51.  51                workerThreads[i].Abort();  
  52.  52            }  
  53.  53  
  54.  54            Console.WriteLine();  
  55.  55            Console.WriteLine(" Processed" + completeCount + "video files");  
  56.  56            Console.WriteLine(" Process compeleted. press Enter to exit");  
  57.  57            Console.ReadLine();  
  58.  58  
  59.  59  
  60.  60        }  
  61.  61  
  62.  62  
  63.  63        /**//// <summary>  
  64.  64        /// Convert    
  65.  65        /// </summary>   
  66.  66        /// <returns></returns>   
  67.  67        private static void ProcessVideo()  
  68.  68        {   
  69.  69              
  70.  70  
  71.  71            while (true)  
  72.  72            {  
  73.  73                  
  74.  74                  
  75.  75                                  
  76.  76                    if (!string.IsNullOrEmpty(waitConvertFile))  
  77.  77                    {  
  78.  78                        //Convert   
  79.  79                        Console.WriteLine("start to convert file:" + waitConvertFile + "");  
  80.  80                        try  
  81.  81                        {  
  82.  82                            if (tool.Convert(waitConvertFile.PhysicalPath) && tool.GetSmallImage(waitConvertFile.PhysicalPath))  
  83.  83                            {  
  84.  84                                completeCount++;  
  85.  85  
  86.  86                                //Change waitConvertFile status if need   
  87.  87                                
  88.  88                            }  
  89.  89                        }  
  90.  90                        catch (Exception exp)  
  91.  91                        {  
  92.  92                            //setting Convert failure   
  93.  93                            Console.WriteLine("文件" + waitConvertFile.VideoID + "Convert failure");  
  94.  94                        }  
  95.  95                        Console.WriteLine("文件" + waitConvertFile.VideoID + "Convert ending");  
  96.  96                        Thread.Sleep(1000);  
  97.  97                    }  
  98.  98                    Thread.Sleep(1000 * 60);  
  99.  99  
  100. 100                               
  101. 101                  
  102. 102                                   
  103. 103                
  104. 104            }           
  105. 105  
  106. 106             
  107. 107        }  
  108. 108    }  
  109. 109}  

 

通过四步,我们视频转换工程就创建完了,这里的主要思路是服务器端调用视频转换工具 ffmpeg.exe,设置参数,通过Main来实现转换。

分类:

技术点:

相关文章: