[Sloved]-Converting Video with FFMPEG With ASP.Net


This article aims to guide .NET programmers on how to convert video formats (such as FLV, MKV, MP4, and others) using code snippets. Let's start with a basic introduction.

In this article, I'll explain how to use FFMPEG executable for converting video files from one format to another. You can also use it to convert audio files between different formats.

Furthermore, I'll provide an FFMPEG command for generating HTML5-compatible MP4 videos.

Now, let's assume we have a website with one ASPX page and a folder named "Video". I've downloaded the FFMPEG executable from the internet and placed it in the "Video" folder along with an MKV video file. Our objective is to convert this MKV video into MP4 format.

To convert videos using FFmpeg within an ASP.NET application, you'll generally follow these steps: 

Install FFmpeg on your server, call it from your application with the appropriate arguments for your conversion task, and handle the output. 

This process allows you to automate video conversions, integrate video processing functionalities into your ASP.NET apps, and serve the converted videos to your users.

First, ensure FFmpeg is installed on your server. You can download FFmpeg from the official website and install it, or use a package manager if available on your platform. Ensure that the ffmpeg executable is in your system's PATH so that it can be called from any location.

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Converter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        ConvertVideoFileFormat("Video/TestMkv.mkv", ".mp4");
    }

    public static string ConvertVideoFileFormat(string file, string convertedExtension)
    {
        string result = string.Empty;
        string input = string.Empty;
        string output = string.Empty;
        try
        {
            string ffmpegFilePath = HttpContext.Current.Server.MapPath("Video/ffmpeg.exe");// path of ffmpeg.exe -replace it for your options.
            FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(file));
            string filename = Path.GetFileNameWithoutExtension(fi.Name) + "output";//output video name
            string extension = Path.GetExtension(fi.Name);
            input = HttpContext.Current.Server.MapPath(file);
            output = HttpContext.Current.Server.MapPath("Video/" + filename + convertedExtension); //path where you want to save your video
            string cmd = " -i " + input + " -acodec aac -strict experimental -ac 2 -ab 128k -vcodec libx264 -f mp4 -crf 22 -s 640x360 " + output;
            var processInfo = new ProcessStartInfo(ffmpegFilePath, cmd)
            {
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            try
            {
                Process process = System.Diagnostics.Process.Start(processInfo);
                result = process.StandardError.ReadToEnd();
                process.WaitForExit();
                process.Close();

            }
            catch (Exception)
            {
                result = string.Empty;
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
}

Example 2: Implement the Video Conversion Method

using System.Diagnostics;

public class VideoConverter
{
    public static void ConvertVideo(string inputFilePath, string outputFilePath)
    {
        try
        {
            var psi = new ProcessStartInfo
            {
                FileName = "ffmpeg",
                Arguments = $"-i \"{inputFilePath}\" -s hd720 -c:v libx264 -crf 23 -c:a aac -strict -2 \"{outputFilePath}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
            };

            using (var process = Process.Start(psi))
            {
                process.WaitForExit();

                string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();

                if (process.ExitCode != 0)
                {
                    // Handle error
                    Console.WriteLine($"Error: {error}");
                }
                else
                {
                    Console.WriteLine("Video conversion completed successfully.");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }
    }
}