C#视频转换为flv代码

 

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

         1。配置文件里添加
view plaincopy to clipboardprint?
<appSettings>  
 
    <!--convert tools path-->  
    <add key="FfmpegPath" value="D:/tools/"/>      
 
    <!-- setting -->  
    <add key="ThreadCount" value="5" />  
    <add key="BatchSize" value="10" />  
   <add key="QueueTimeout" value="20" />  
  <add key="TransactionTimeout" value="30" />  
 </appSettings> 
 <appSettings>
 
     <!--convert tools path-->
     <add key="FfmpegPath" value="D:/tools/"/>   
 
     <!-- setting -->
     <add key="ThreadCount" value="5" />
     <add key="BatchSize" value="10" />
    <add key="QueueTimeout" value="20" />
   <add key="TransactionTimeout" value="30" />
  </appSettings>

  2。添加一个接口
view plaincopy to clipboardprint?
using System;  
 using System.Collections.Generic;  
 using System.Text;  
   
 namespace VideoConvert  
 {  
     public interface IConvert  
    {  
   
        /** <summary>  
        /// 将视频文件转换为Flv格式  
        /// </summary>  
        /// <param name="sourceFile">要转换的文件</param>  
        /// <returns></returns>  
        bool Convert(string sourceFile);  
 
 
 
        /** <summary>  
        /// 获取缩略图  
        /// </summary>  
        /// <param name="sourceFile"></param>  
        /// <returns></returns>  
        bool GetSmallImage(string sourceFile);  
         
 
    }  

using System;
 using System.Collections.Generic;
 using System.Text;
 
 namespace VideoConvert
 {
     public interface IConvert
    {
 
        /** <summary>
        /// 将视频文件转换为Flv格式
        /// </summary>
        /// <param name="sourceFile">要转换的文件</param>
        /// <returns></returns>
        bool Convert(string sourceFile);

 

        /** <summary>
        /// 获取缩略图
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <returns></returns>
        bool GetSmallImage(string sourceFile);
      

    }
}
 

转换工具设定继承 IConvert

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值