Ffmpeg 实现视频截图

Ffmpeg
得到文件

下载好之后首先就是配置一下环境变量

我下载的放在了D盘跟目录

D:oracleproduct10.2.0client_1bin;D:oracleproduct10.2.0db_1bin;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;C:Program FilesJavajdk1.7.0bin;C:Program FilesATI TechnologiesATI.ACECore-Static;C:Program FilesCommon FilesThunder NetworkKanKanCodecs;D:Program FilesTortoiseSVNbin;C:Program FilesMicrosoft SQL Server90Toolsbinn;D:ffmpeg

配的有点多,最后那个就是我的ffmpeg

配置好之后就先在Dos中测试下

打开Dos界面 输入 ffmpeg

如果成功的话会显示好多的命令

如果失败则显示 你输入的不是内部命令之类的。

如果失败就看看自己的环境变量是否配置OK

OK之后先在Dos下测试是否能成功转换

首先在一个目录放入一个视频

我在D盘跟目录放入了Demo.avi 视频,我将要转换为Flv

输入命令:ffmpeg -i D:/Demo.avi  D:/Demo..flv

-i 后面紧跟的是要转换的文件地址 在后是你要把文件转换到哪里以及相对应的文件名和格式

输入命令之后回车,看看相对应的目录是否出现了你需要的转换后的文件。

如果失败检查命令是否错误。

PHP中执行转换的命令

//转换为Flv
function makeFlv($video_file,$flv_file)
{
//判断给定的文件是否正常
if(!is_file($video_file)){
  return false;
}

global $flv_msg;
    $flv_cmd="ffmpeg -i ".$video_file." ".$flv_file;
    exec($flv_cmd,$flv_msg);
}

//创建flv视频的图片
function makeFlvPic($flv_file,$flv_pic_file)
{
global $flv_msg;

    $flv_pic_cmd="ffmpeg -i ".$flv_file.
       " -y -f image2 ".
       " -ss 1 ".
       " -t 0.001 ".
       " -s 350x240 ".$flv_pic_file;

    exec($flv_pic_cmd,$flv_msg);
}

 

 

 

代码
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

namespace  WebApplication1
{
    
public   class  makePic
    {

    
public   static   string  CatchImg( string  vFileName)
        {
           
string  defPic = " images/nopic.gif " ; // 无法生成时显示的图片
     string  picPath = " UploadImage/ "  ; // 生成图片输出位置
     string  movPath = " case/ " ; // 视频文件位置
             string  ffmpeg  =  HttpContext.Current.Server.MapPath( " bin/ffmpeg.exe " );
        
            
if  ( ! System.IO.File.Exists(ffmpeg))
            {
                
return  defPic;
            }
            
if  ( ! System.IO.File.Exists(HttpContext.Current.Server.MapPath( " case/ " + vFileName)))
            {
                
return  defPic;
            }
          
            
string  flv_img  = picPath  +  System.IO.Path.ChangeExtension(vFileName,  " .jpg " );

            
           
string  flv_img_p  =  HttpContext.Current.Server.MapPath(flv_img);
          
           
if  (System.IO.File.Exists(flv_img_p))
            {
                
return  flv_img;
            }

            
            
string  FlvImgSize  =   " 240x180 " ;

            System.Diagnostics.ProcessStartInfo startInfo 
=   new  System.Diagnostics.ProcessStartInfo(ffmpeg);
            startInfo.WindowStyle 
=  System.Diagnostics.ProcessWindowStyle.Normal;

          
            startInfo.Arguments 
=   "  -i  "   +  HttpContext.Current.Server.MapPath(movPath + vFileName)  +   "  -y -f image2 -t 10 -s  "   +  FlvImgSize  +   "   "   +   flv_img_p;

            
try
            {
                System.Diagnostics.Process.Start(startInfo);
            }
            
catch
            {
                
return  defPic;
            }

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

            
return  defPic;
        }

}


 

 

你可能感兴趣的:(ffmpeg)