Upload any video and convert to .mp4 online in .net
Asked Answered
U

3

12

I have a strange requirement. User can upload their video of any format (or a limited format). We have to store them and convert them to .mp4 format so we can play that in our site.

Same requirement also for audio files.

I have googled but I can't get any proper idea. Any help or suggestions....??

Thanks in advance

Understanding answered 22/5, 2013 at 14:9 Comment(7)
i don't know if this will help but there's an online converter - maybe view the source? video.online-convert.com/convert-to-mp4Palatalized
what .net language are you using asp/c sharp?Palatalized
you might wanna check out code.google.com/p/ffmpeg-sharpPalatalized
look at this blog jasonjano.wordpress.com/2010/02/09/…Palatalized
doyourself.org/ffmpeg/588-ffmpeg-x264-encoding-guidePalatalized
google ffmpeg-x264-encoding-guide and you'll get a lot of stuffPalatalized
thanks I will try that and after that give updates..Understanding
S
13

You can convert almost any video/audio user files to mp4/mp3 with FFMpeg command line utility. From .NET it can be called using wrapper library like Video Converter for .NET (this one is nice because everything is packed into one DLL):

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

Note that video conversion requires significant CPU resources; it's good idea to run it in background.

Shyamal answered 8/9, 2013 at 17:22 Comment(1)
This throws an exception "An exception of type 'System.ComponentModel.Win32Exception' occurred in NReco.VideoConverter.dll but was not handled in user code Additional information: The specified executable is not a valid application for this OS platform."Optometrist
S
8

Your Answer

please Replace .flv to .mp4 you will get your answer

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());
    }
Sikang answered 3/12, 2013 at 11:50 Comment(1)
@Sikang what is the second conversion forBondstone
C
5

I know it's a bit old thread but if I get here other people see this too. You shoudn't use it process info to start ffmpeg. It is a lot to do with it. Xabe.FFmpeg you could do this by running just

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

This is one of easiest usage. This library provide fluent API to FFmpeg.

Cresol answered 17/8, 2018 at 19:20 Comment(1)
This shows a warning message. Conversion.Convert(string, string, bool) is obselete. This will be deleted in next major version. Please use FFmpeg.Conversions.FromSnippet intead of that. New usage is like this: string output = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".mp4"); var snippet = FFmpeg.Conversions.FromSnippet.Convert(Resources.MkvWithAudio, output); IConversionResult result = await snippet.Start();Perkoff

© 2022 - 2024 — McMap. All rights reserved.