Код: Выделить всё
using System;
using System.Diagnostics;
using System.IO;
namespace WebApplication16
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCompress_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
string inputFilePath = Path.GetTempFileName();
string outputFilePath = Path.GetTempFileName();
fileUpload.SaveAs(inputFilePath);
CompressVideo(inputFilePath, outputFilePath, "640x480", "200k");
Response.ContentType = "video/mp4";
Response.AppendHeader("Content-Disposition", "attachment; filename=\"compressed_video.mp4\"");
Response.TransmitFile(outputFilePath);
Response.End();
}
else
{
Response.Write("No file uploaded");
}
}
static void CompressVideo(string inputFilePath, string outputFilePath, string resolution , string bitrate )
{
try
{
if (!File.Exists(inputFilePath))
{
Console.WriteLine($"Error: Input file '{inputFilePath}' does not exist.");
return;
}
if (!IsValidVideoFile(inputFilePath))
{
Console.WriteLine($"Error: Input file '{inputFilePath}' is not a valid video file.");
return;
}
string ffmpegPath = @"C:\Users\abhin\Downloads\ffmpeg-master-latest-win64-gpl-shared\bin\ffmpeg.exe"; // Assuming ffmpeg is in PATH
string args = $"-i \"{inputFilePath}\" -s {resolution} -b:v {bitrate} \"{outputFilePath}\" -y";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = ffmpegPath,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine($"Error: {e.Data}");
}
};
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine($"Output: {e.Data}");
}
};
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine($"Error: ffmpeg process exited with code {process.ExitCode}");
}
}
if (!File.Exists(outputFilePath))
{
Console.WriteLine($"Error: Output file '{outputFilePath}' was not generated.");
}
else
{
Console.WriteLine($"Output file '{outputFilePath}' generated successfully.");
FileInfo fileInfo = new FileInfo(outputFilePath);
Console.WriteLine($"Output file size: {fileInfo.Length} bytes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
}
}
static bool IsValidVideoFile(string filePath)
{
string extension = Path.GetExtension(filePath);
return extension.ToLower() == ".mp4" || extension.ToLower() == ".avi" || extension.ToLower() == ".mov";
}
}
}
Код: Выделить всё
Video Compression
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/786 ... -net-frame