上传任何视频并在.net中在线转换为.mp4

时间:2013-05-22 14:09:59

标签: c# .net video mp4

我有一个奇怪的要求。用户可以上传任何格式(或有限格式)的视频。我们必须存储它们并将它们转换为 .mp4 格式,以便我们可以在我们的网站中播放它。

同样要求音频文件。

我用谷歌搜索但我无法得到任何正确的想法。任何帮助或建议.... ??

提前致谢

3 个答案:

答案 0 :(得分:11)

您可以使用FFMpeg command line utility将几乎所有视频/音频用户文件转换为mp4 / mp3。从.NET可以使用像Video Converter for .NET这样的包装器库来调用它(这个很好,因为所有内容都打包到一个DLL中):

(new NReco.VideoConverter.FFMpegConverter()).ConvertMedia(pathToVideoFile, pathToOutputMp4File, Formats.mp4)

请注意,视频转换需要大量CPU资源;在后台运行它是个好主意。

答案 1 :(得分:8)

Your Answer

请将.flv替换为.mp4,您将得到答案

private bool ReturnVideo(string fileName)
    {
        string html = string.Empty;
        //rename if file already exists

        int j = 0;
        string AppPath;
        string inputPath;
        string outputPath;
        string imgpath;
        AppPath = Request.PhysicalApplicationPath;
        //Get the application path
        inputPath = AppPath + "OriginalVideo";
        //Path of the original file
        outputPath = AppPath + "ConvertVideo";
        //Path of the converted file
        imgpath = AppPath + "Thumbs";
        //Path of the preview file
        string filepath = Server.MapPath("~/OriginalVideo/" + fileName);
        while (File.Exists(filepath))
        {
            j = j + 1;
            int dotPos = fileName.LastIndexOf(".");
            string namewithoutext = fileName.Substring(0, dotPos);
            string ext = fileName.Substring(dotPos + 1);
            fileName = namewithoutext + j + "." + ext;
            filepath = Server.MapPath("~/OriginalVideo/" + fileName);
        }
        try
        {
            this.fileuploadImageVideo.SaveAs(filepath);
        }
        catch
        {
            return false;
        }
        string outPutFile;
        outPutFile = "~/OriginalVideo/" + fileName;
        int i = this.fileuploadImageVideo.PostedFile.ContentLength;
        System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));
        while (a.Exists == false)
        {

        }
        long b = a.Length;
        while (i != b)
        {

        }


        string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
        ConvertNow(cmd);
        string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
        ConvertNow(imgargs);


        return true;
    }
    private void ConvertNow(string cmd)
    {
        string exepath;
        string AppPath = Request.PhysicalApplicationPath;
        //Get the application path
        exepath = AppPath + "ffmpeg.exe";
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.FileName = exepath;
        //Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"
        proc.StartInfo.Arguments = cmd;
        //The command which will be executed
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = false;
        proc.Start();

        while (proc.HasExited == false)
        {

        }
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        ReturnVideo(this.fileuploadImageVideo.FileName.ToString());
    }

答案 2 :(得分:3)

我知道这是一个有点旧的线程,但是如果我到达这里,其他人也会看到这个。您不应该使用它来启动ffmpeg的过程信息。这与它有很多关系。 Xabe.FFmpeg您可以只运行

await Conversion.Convert("inputfile.mkv", "file.mp4").Start()

这是最简单的用法之一。该库为FFmpeg提供了流畅的API。