如何创建视频缩略图(extension.wmv,.avi)

时间:2012-04-04 11:15:40

标签: asp.net

视频路径(extension.wmv,.avi)存储在SQL Server中。我必须在ASPX页面上创建缩略图和显示它。

如果有人知道,请分享其代码。

先谢谢

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我有一些用于生成缩略图视频的代码。

为此您需要有名为“ffmpeg.exe”的第三方exe,它是免费提供的。您可以从here

下载

现在我创建了一个功能,在其下面生成原始视频的缩略图视频。 你可以根据你的要求改变变量。

private bool SaveVideo(FileUpload fupload)
{

    string VideoResolutionWidth = "400" ; //define video res. width
    string VideoResolutionHeight = "280";  //define video res. height
    string VideoThumbWidth = "100";//define jpg res. width
    string VideoThumbHeight = "100"; //define jpg res. height

    string VideoLocation = HttpContext.Current.Server.MapPath("~/ConverterFolder/Video/");
    string ConverterPath = HttpContext.Current.Server.MapPath("~/ConverterFolder/");
    String LargeFileName = "TestVideo.wmv"; // Thumb Video file name
    string ThumbFileName= "Testvideo.jpg";

    // Save Original video file first

    if (fupload.PostedFile.FileName != "")
    {
        fupload.PostedFile.SaveAs(ConverterPath + "\\" + System.IO.Path.GetFileName(fupload.PostedFile.FileName.Replace(" ", "_")));
    }

    if (System.IO.File.Exists(ConverterPath + "\\" + System.IO.Path.GetFileName(fupload.PostedFile.FileName.Replace(" ", "_"))))
    {
        string inipath = ConverterPath;
        string flvCMD, flvArg;
        string a_res_width, a_res_height, a_audioRate, a_frameRate, i_res_width, i_res_height;

        a_res_width = VideoResolutionWidth;
        a_res_height = VideoResolutionHeight;
          i_res_width = VideoThumbWidth;
        i_res_height = VideoThumbHeight;

        a_audioRate = "22050";
        a_frameRate = "15";

        string VideoThumbResolution = i_res_width + "x" + i_res_height;
        string VideoResolution = a_res_width + "x" + a_res_height;           
        String videoPATH = VideoLocation +  "\\" + LargeFileName.Replace(" ", "_");

        flvCMD = ConverterPath + "ffmpeg.exe";
        flvArg = "-i " + ConverterPath + "\\" + System.IO.Path.GetFileName(fupload.PostedFile.FileName.Replace(" ", "_")) + " -s " + VideoResolution + " -r " + a_frameRate + " -b 7500000 -ar " + a_audioRate + "  -ab 48 " + videoPATH;


        try
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.FileName = flvCMD;
            process.StartInfo.Arguments = flvArg;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            process.Close();
            process.Dispose();

            if (System.IO.File.Exists(videoPATH))
            {

                Process process1 = new Process();
                process1.StartInfo.UseShellExecute = false;
                process1.StartInfo.RedirectStandardOutput = true;
                process1.StartInfo.RedirectStandardError = true;
                process1.StartInfo.CreateNoWindow = true;
                process1.StartInfo.FileName = flvCMD;
                process1.StartInfo.Arguments = "-i " + ConverterPath + "\\" + System.IO.Path.GetFileName(fupload.PostedFile.FileName.Replace(" ", "_")) + " -an -ss 00:00:01 -r 1 -vframes 1 -s " + VideoThumbResolution + " -f mjpeg -y " + VideoLocation "\\" + ThumbFileName;
                process1.Start();
                string outputMeta1 = process1.StandardOutput.ReadToEnd();
                process1.Close();
                process1.Dispose();
            }
            else
            {
                return false;
            }
        }
        catch (Exception)
        {

        }


    }
    return true;
}

希望这会对你有所帮助。快乐的编码......