1

在我的项目中,我正在保存视频,使用 ffmpeg 将格式转换为 .swf。转换和保存视频工作正常,但创建缩略图时遇到问题。它保存了“picture.jpg”,但没有图像。查看缩略图,它只有正常的 photoviewer 标志,这很好,但是当我尝试打开图像时,它会显示 Windows 照片查看器的消息:Windows 照片查看器无法打开此图片,因为文件似乎已损坏、损坏或太大。(这张图片是 2.7MB) - 从我的相机拍摄的照片大约是 5MB 并且可以打开。

PictureViewer:无法显示“image.jpg”,因为找不到合适的图形导入器。

油漆:油漆无法读取此文件。这不是一个有效的位图文件或其格式目前不受支持。我为保存图像所做的任何操作都不会打开图像:

    protected void btnUploadVideo_Click(object sender, EventArgs e)
    {
        string dtMonth = DateTime.Now.ToString("MMM");
        string dtYear = DateTime.Now.ToString("yyyy");
        lblMsg.Visible = false;

        lblMsg.Visible = true;

        // Before attempting to save the file, verify
        // that the FileUpload control contains a file.
        if (fuPath.HasFile)
        {
            // Get the size in bytes of the file to upload.
            int fileSize = fuPath.PostedFile.ContentLength;

            // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
            if (fileSize < 10497717)
            {
                // Call a helper method routine to save the file.
                SaveFile2();
            }
        }
        else
            // Notify the user that a file was not uploaded.
            lblMsg.Text = "You did not specify a file to upload.";
    }

    private void SaveFile2()
    {
        string dtMonth = DateTime.Now.ToString("MMM");
        string dtYear = DateTime.Now.ToString("yyyy");

        if (fuPath != null || fuPath.PostedFile != null)
        {
            postedfilename = fuPath.PostedFile.FileName;
            SavePath = Server.MapPath("~\\Video\\");
            string NewFName = postedfilename;
            NewFName = NewFName.Substring(NewFName.LastIndexOf("\\") + 1, NewFName.Length - NewFName.LastIndexOf(".")) + "." + NewFName.Substring(NewFName.LastIndexOf(".") + 1);
            Filenamewithpath = SavePath + NewFName;
            string outputPath = Server.MapPath("~\\uploads\\" + dtYear + "\\" + dtMonth + "\\SWF\\");
            CreateDirectoryIfNotExists(SavePath);
            CreateDirectoryIfNotExists(outputPath);

            //Save The file
            fuPath.PostedFile.SaveAs(Filenamewithpath);

            //Start Converting
            string inputfile, outputfile, filargs;
            string withoutext;

            //Get the file name without Extension
            withoutext = Path.GetFileNameWithoutExtension(Filenamewithpath);

            //Input file path of uploaded image
            inputfile = SavePath + NewFName;

            //output file format in swf

            outputfile = outputPath + withoutext + ".swf";
            Session["outputfile"] = withoutext + ".swf";
            //file orguments for FFMEPG

            // Create the path and file name to check for duplicates.
            string pathToCheck = outputfile;

            // Create a temporary file name to use for checking duplicates.
            string tempfileName = "";

            // Check to see if a file already exists with the
            // same name as the file to upload.        
            if (System.IO.File.Exists(pathToCheck))
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempfileName = outputPath + counter.ToString() + withoutext + ".swf";
                    pathToCheck = tempfileName;
                    counter++;
                }

                outputfile = tempfileName;
                // Notify the user that the file name was changed.
                lblMsg.Text = "A file with the same name already exists." +
                    "<br />Your file was saved as " + counter.ToString() + withoutext + ".swf";
            }

            filargs = "-i " + inputfile + " -ar 22050 " + outputfile;

            string spath;
            spath = Server.MapPath(".");
            Process proc;
            proc = new Process();
            proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
            proc.StartInfo.Arguments = filargs;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.RedirectStandardOutput = false;
            try
            {

                proc.Start();
                fuPath.PostedFile.SaveAs(outputfile);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            proc.WaitForExit();
            proc.Close();

            //Create Thumbs 

            string thumbPath, thumbName;
            string thumbargs;

            thumbPath = Server.MapPath("~\\uploads\\" + dtYear + "\\" + dtMonth + "\\Thumb\\");
            CreateDirectoryIfNotExists(thumbPath);

            thumbName = thumbPath + withoutext + ".jpg";

            // Create the path and file name to check for duplicates.
            string thumbPathToCheck = thumbName;

            // Create a temporary file name to use for checking duplicates.
            string thumbTempfileName = "";

            // Check to see if a file already exists with the
            // same name as the file to upload.        
            if (System.IO.File.Exists(thumbPathToCheck))
            {
                int counter = 2;
                while (System.IO.File.Exists(thumbPathToCheck))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    thumbTempfileName = thumbPath + counter.ToString() + withoutext + ".jpg";
                    thumbPathToCheck = thumbTempfileName;
                    counter++;
                }

                thumbName = thumbTempfileName;
                // Notify the user that the file name was changed.
                lblMsg.Text = "A file with the same name already exists." +
                    "<br />Your file was saved as " + counter.ToString() + withoutext + ".jpg";
            }

            //thumbargs = "-i " + inputfile + " -an -ss 00:00:03 -s 120×90 -vframes 1 -f mjpeg " + thumbName;
           // thumbargs = "-i " + inputfile + "-f image2 -ss 1.000 -vframes 1 " + thumbName;
            thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:10 -s 150x150 " + thumbName;

            //  thumbargs = "ffmpeg -i" + inputfile + " -ss 0:00:01.000 -sameq -vframes 1 " + withoutext + ".jpg";
            //  thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:07 -s 150x150 " + thumbName;
            Process thumbproc = new Process();
            thumbproc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
            thumbproc.StartInfo.Arguments = thumbargs;
            thumbproc.StartInfo.UseShellExecute = false;
            thumbproc.StartInfo.CreateNoWindow = true;
            thumbproc.StartInfo.RedirectStandardOutput = false;

            string data = "";                

            try
            {
                fuPath.PostedFile.SaveAs(thumbName);
                thumbproc.Start();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

            thumbproc.WaitForExit();
            thumbproc.Close();
            File.Delete(inputfile);
            lblMsg.Text = "Video Uploaded Successfully";
            hyp.Visible = true;
            hyp.NavigateUrl = "WatchVideo.aspx";

            SaveToDB(outputfile, thumbName);
        }
    }

编辑:如果是拇指参数,我尝试了几种方法,但没有成功:

     //thumbargs = "-i " + inputfile + " -an -ss 00:00:03 -s 120×90 -vframes 1 -f mjpeg " + thumbName;
     // thumbargs = "-i " + inputfile + "-f image2 -ss 1.000 -vframes 1 " + thumbName;
      thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:10 -s 150x150 " + thumbName;
     //  thumbargs = "ffmpeg -i" + inputfile + " -ss 0:00:01.000 -sameq -vframes 1 " + withoutext + ".jpg";

这是保存后的样子,但是打不开...... 在此处输入图像描述

编辑:

我尝试将参数添加到命令提示符以查看它是否在那里工作,但它给了我一条消息:[NULL @ 000000000025f7a0] Unable to find a suitable output format for 'vframes' vframes: invalid argument 我使用的行:

    ffmpeg -i VID2012.3GP vframes 1 VID2012.jpg

如果是在 IIS 中设置不正确,我该如何设置?

编辑:我更改了一些代码。通过我的代码进行调试没有抛出异常,但是如果我将鼠标指向进程(在它启动之后),它“显示 System.InvalidOperationException 类型的异常”

    thumbargs = "-i " + postedfilename + " vframes 1" + thumbName;
            ProcessStartInfo thumbProcstartIfo = new ProcessStartInfo();
            thumbProcstartIfo.FileName = @"\ffmpeg\ffmpeg.exe";
            thumbProcstartIfo.Arguments = thumbargs;
            thumbProcstartIfo.UseShellExecute = false;
            thumbProcstartIfo.CreateNoWindow = true;
            thumbProcstartIfo.RedirectStandardOutput = false;

            try
            {
                using (var process = new Process())
                {
                    process.StartInfo = thumbProcstartIfo;
                    process.Start();
                    process.WaitForExit();
                }
            }
            catch (InvalidOperationException ex)
            {
                lblMsg.Text = ex.ToString();
            }
4

1 回答 1

1

我认为这与以下几行有关:

thumbproc.Start();
fuPath.PostedFile.SaveAs(thumbname);

我希望第二行将上传的文件保存到您的数据目录,并且第一行开始处理文件。不应该交换这些行吗?

fuPath.PostedFile.SaveAs(thumbname);
thumbproc.Start();
于 2013-05-29T09:32:42.770 回答