视频转换成flv格式及视频截图

来源:互联网 发布:网络口语课程 编辑:程序博客网 时间:2024/05/16 06:46

首先判断视频的格式,然后根据不同的格式分别采用2种转换工具.示例代码:

 

#region 确定
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string strTime = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
        ff.PostedFile.SaveAs(Server.MapPath(WebDefine.UpVideoPath + ff.FileName));
        int flag = 0;
        if (ff.FileName.Split('.')[1] != "flv")
        {
            foreach (string str in WebDefine.arrFfmpeg)
            {
                if (str == ff.FileName.Split('.')[1])
                {
                    string upfile = Server.MapPath(WebDefine.UpVideoPath + ff.FileName) + " -ab 56 -ar 44100 -qscale 8 -r 29.97 ";
                    string flvfile = Server.MapPath(WebDefine.FlvPath + strTime + ".flv");
                    string picfile = WebDefine.FlvPath + strTime + ".flv";
                    string strcmd = upfile + flvfile;
                    RunFFMpeg(strcmd);
                    flag = 1;
                    pic = CatchImg(picfile);
                }
            }
            foreach (string str1 in WebDefine.arrMencoder)
            {
                if (str1 == ff.FileName.Split('.')[1])
                {
                    string upfile = " " + Server.MapPath(WebDefine.UpVideoPath + ff.FileName) + " -o ";
                    string flvfile = Server.MapPath(WebDefine.FlvPath + strTime + ".flv") + " -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=300:mbd=2:mv0:trell:v4mv:cbp:last_pred=3: -sws -1 -vf scale=512:-3 -ofps 12 -srate 11025 ";
                    string strcmd = upfile + flvfile;
                    RunMencoder(strcmd);
                    flag = 1;
                    pic = CatchImg(WebDefine.FlvPath + strTime + ".flv");
                }
            }
        }
        else
        {
            ff.PostedFile.SaveAs(Server.MapPath(WebDefine.FlvPath + ff.FileName));
            flag = 1;
        }
        if (flag == 0)
        {
            MessageBox.Show(this, "不支持该格式文件!");
            return;
        }

        Save(strTime);
    }
    #endregion

    #region 存入数据库

    /// <summary>
    /// 想数据库增加数据
    /// </summary>
    private void Save(string strTime)
    {
        T_Video_Mod model = new T_Video_Mod();
        model.TVideoId = Guid.NewGuid().ToString();
        model.VideoTitle = txtName.Text.Trim();
        model.VideoContent = txtContent.Text.Trim();
        model.VideoSize = "0";
        model.PicturePath = pic;
        model.VideoPath = strTime + ".flv";
        model.DeleteFlag = WebDefine.DeleteN;
        model.CreateTime = System.DateTime.Now.ToString();
        model.CreateUser = UserID;
        T_Video_Bll.InsertTVideoMod(WebDefine.DBConnectionName, model);
        MessageBox.ResponseScript(this, "alert('上传成功,如上传文件较大,需要等一会才能正常播放');window.location.href=window.location.href;");
    }
    #endregion

    #region RunFFMpeg
    /// <summary>
    /// 将asx, asf, mpg, wmv, 3gp, mp4, avi格式文件转换成Flv格式
    /// </summary>
    /// <param name="strCmd"></param>
    public void RunFFMpeg(string strCmd)
    {
        System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(Server.MapPath(WebDefine.ToolsPath));
        FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        FilestartInfo.Arguments = " -i " + strCmd;
        try
        {
            System.Diagnostics.Process.Start(FilestartInfo);
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message);
        }
        System.Threading.Thread.Sleep(6000);
    }
    #endregion

    #region RunMencoder
    /// <summary>
    /// 将wmv9, rm, rmvb, mov格式文件转换成Flv格式
    /// </summary>
    /// <param name="strCmd"></param>
    public void RunMencoder(string strCmd)
    {
        System.Diagnostics.ProcessStartInfo FilestartInfo = new System.Diagnostics.ProcessStartInfo(Server.MapPath(WebDefine.MToolsPath));
        FilestartInfo.UseShellExecute = false;
        FilestartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        FilestartInfo.Arguments = strCmd;
        try
        {
            System.Diagnostics.Process.Start(FilestartInfo);
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message);
        }
        System.Threading.Thread.Sleep(6000);
    }
    #endregion

    #region 截图
    /// <summary>
    /// 找到视频文件进行截图
    /// </summary>
    /// <param name="vFileName"></param>
    /// <returns></returns>
    public string CatchImg(string vFileName)
    {
        //string ffmpeg = System.Configuration.ConfigurationSettings.AppSettings["ffmpeg"];

        if ((!System.IO.File.Exists(Server.MapPath(WebDefine.ToolsPath))) || (!System.IO.File.Exists(Server.MapPath(vFileName))))
        {
            return "";
        }

        string flv_img = System.IO.Path.ChangeExtension(vFileName, ".jpg");

        string flv_img_p = HttpContext.Current.Server.MapPath(flv_img);

        string FlvImgSize = "160*110";

        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(Server.MapPath(WebDefine.ToolsPath));
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

        //此处组合成ffmpeg.exe文件需要的参数即可,此处命令在ffmpeg 0.4.9调试通过
        startInfo.Arguments = " -i " + Server.MapPath(vFileName) + " -y -f image2 -t 0.001 -s " + FlvImgSize + " " + flv_img_p;

        try
        {
            System.Diagnostics.Process.Start(startInfo);
        }
        catch
        {
            return "";
        }
        ///注意:图片截取成功后,数据由内存缓存写到磁盘需要时间较长,大概在3,4秒甚至更长;
        ///这儿需要延时后再检测,我服务器延时8秒,即如果超过8秒图片仍不存在,认为截图失败;
        System.Threading.Thread.Sleep(6000);

        if (System.IO.File.Exists(flv_img_p))
        {
            return flv_img;
        }
        return "";
    }
    #endregion

原创粉丝点击