kad1r
12/12/2013 - 12:35 AM

Converting video with FFMPEG with ASP.Net

Converting video with FFMPEG with ASP.Net

public static string ConvertVideo(string file, string convertedExtension)
{
	string result = string.Empty;
	string input = string.Empty;
	string output = string.Empty;
	try
	{
		string ffmpegFilePath = "~/video/ffmpeg/ffmpeg.exe"; // path of ffmpeg.exe - please replace it for your options.
		FileInfo fi = new FileInfo(HttpContext.Current.Server.MapPath(file));
		string filename = Path.GetFileNameWithoutExtension(fi.Name);
		string extension = Path.GetExtension(fi.Name);
		input = HttpContext.Current.Server.MapPath(file);
		output = HttpContext.Current.Server.MapPath("/video/" + filename + convertedExtension);

		var processInfo = new ProcessStartInfo(HttpContext.Current.Server.MapPath(ffmpegFilePath), " -i \"" + input + "\" -ar 22050 \"" + output + "\"")
		{
			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;
}