IIS服务器配置flv格式视频处理程序实现进度条随意拖动

视频网站如果选用flv为视频格式,对于flv格式视频的进度条能够随意拖动和视频未加载完可以实现边加载边播放的实现还需要进行额外的处理。

一、实现条件

1、视频未加载完且能够边加载边播放的条件

       a、视频的元数据信息(metadata信息)要放到视频开始的头部。

       b、采用的网页播放器要支持边加载边播放。

2、视频进度条可以拖动到未加载部分的条件

       a、播放器要支持

       b、flv视频要有关键帧和meta信息

       c、服务器端要支持

二、解决方案

       1、播放器

             网上能够免费使用的网页播放器还是比较多的,可以搜个只要支持视频进度条拖动设置的就可以。我这里用的ckplayer播放器,关于这个播放的下载和使用请百度。

        2、给flv视频加入关键帧和meta信息

              采用的是yamdi:补全meta信息的工具,地址:http://yamdi.sourceforge.net/有windows版本,下载了直接用就好。

        3、使用一般处理程序对返回的视频参数进行处理,新建一个一般处理程序,代码如下

              

    using System;  
    using System.IO;  
    using System.Web;  
    public class FLVStreaming : IHttpHandler  
    {  
        private static readonly byte[] _flvheader = HexToByte("464C5601010000000900000009");  
        public FLVStreaming()  
        {  
        }  
        public void ProcessRequest(HttpContext context)  
        {  
            try  
            {  
                int pos;  
                int length;  
                // Check start parameter if present  
                string filename = Path.GetFileName(context.Request.FilePath);  
                using (FileStream fs = new FileStream(context.Server.MapPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read))  
                {  
                    string qs = context.Request.Params["start"];  
                    if (string.IsNullOrEmpty(qs))  
                    {  
                        pos = 0;  
                        length = Convert.ToInt32(fs.Length);  
                    }  
                    else  
                    {  
                        pos = Convert.ToInt32(qs);  
                        length = Convert.ToInt32(fs.Length - pos) + _flvheader.Length;  
                    }  
                    // Add HTTP header stuff: cache, content type and length         
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);  
                    context.Response.Cache.SetLastModified(DateTime.Now);  
                    context.Response.AppendHeader("Content-Type", "video/x-flv");  
                    context.Response.AppendHeader("Content-Length", length.ToString());  
                    // Append FLV header when sending partial file  
                    if (pos > 0)  
                    {  
                        context.Response.OutputStream.Write(_flvheader, 0, _flvheader.Length);  
                        fs.Position = pos;  
                    }  
                    // Read buffer and write stream to the response stream  
                    const int buffersize = 16384;  
                    byte[] buffer = new byte[buffersize];  
                     
                    int count = fs.Read(buffer, 0, buffersize);  
                    while (count > 0)  
                    {  
                        if (context.Response.IsClientConnected)  
                        {  
                            context.Response.OutputStream.Write(buffer, 0, count);  
            
          //Starts sending to client right away  
          context.Response.Flush();  
         
                            count = fs.Read(buffer, 0, buffersize);  
                        }  
                        else  
                        {  
                            count = -1;  
                        }  
                    }  
                }  
            }  
            catch (Exception ex)  
            {  
                System.Diagnostics.Debug.WriteLine(ex.ToString());  
            }  
        }  
        public bool IsReusable  
        {  
            get { return true; }  
        }  
        private static byte[] HexToByte(string hexString)  
        {  
            byte[] returnBytes = new byte[hexString.Length / 2];  
            for (int i = 0; i < returnBytes.Length; i++)  
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);  
            return returnBytes;  
        }  
    }  

        4、配置config文件,可以手动添加以下代码,也可以在IIS中设置(推荐)

               a、手动配置

                    
 <?xml version="1.0" encoding="UTF-8"?>  
    <configuration>  
        <system.webServer>  
            <handlers>  
                <add name="FLVStreaming" path="*.flv" verb="*" type="FLVStreaming" resourceType="Unspecified" preCondition="integratedMode" />  
            </handlers>  
        </system.webServer>  
    </configuration> 
               b、IIS中设置

                    1、双击站点的处理程序映射。

                     2、点击添加托管处理程序。

                     3、请求路径:*.flv,类型:点击下拉按钮会显示已经编译好的一般处理程序(在使用此设置前需要先编译),名称:FLVStreaming(写一般处理程序的类名或自定义)

         5、第四步配置完成后如果不能实现,可以按照以下配置步骤添加IIS中的MIME类型。

               双击MIME类型,点击添加,文件扩展名:.flv,MIME类型:application/octet-stream

原文地址:https://www.cnblogs.com/wangzl1163/p/6341120.html