Anonymous
Проблемы при создании видеокомпрессора с использованием библиотеки ffmpeg в .net framework 4.7.1.
Сообщение
Anonymous » 29 июн 2024, 09:16
Я создавал видеокомпрессор с использованием платформы .net 4.2.1, а также использую библиотеку ffmpeg для сжатия видео. Я не получаю точную проблему: на самом деле библиотека ffmpeg не работает или запрос не ждет сжатия видео. Кстати, когда я нажимаю кнопку сжатия, чтобы начать сжатие видео, он загружает файл mp4 размером 0 КБ. Пожалуйста, помогите, если кто-нибудь знаком с этой концепцией. Спасибо
Код: Выделить всё
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";
}
}
}
Я создавал видеокомпрессор с использованием платформы .net 4.2.1, а также использую библиотеку ffmpeg для сжатия видео. Я не получаю точную проблему: на самом деле библиотека ffmpeg не работает или запрос не ждет сжатия видео. Кстати, когда я нажимаю кнопку сжатия, чтобы начать сжатие видео, он загружает файл mp4 размером 0 КБ. Пожалуйста, помогите, если кто-нибудь знаком с этой концепцией
Спасибо
Подробнее здесь:
https://stackoverflow.com/questions/786 ... -net-frame
1719641817
Anonymous
Я создавал видеокомпрессор с использованием платформы .net 4.2.1, а также использую библиотеку ffmpeg для сжатия видео. Я не получаю точную проблему: на самом деле библиотека ffmpeg не работает или запрос не ждет сжатия видео. Кстати, когда я нажимаю кнопку сжатия, чтобы начать сжатие видео, он загружает файл mp4 размером 0 КБ. Пожалуйста, помогите, если кто-нибудь знаком с этой концепцией. Спасибо [code] 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"; } } } [/code] [code] Video Compression [/code] Я создавал видеокомпрессор с использованием платформы .net 4.2.1, а также использую библиотеку ffmpeg для сжатия видео. Я не получаю точную проблему: на самом деле библиотека ffmpeg не работает или запрос не ждет сжатия видео. Кстати, когда я нажимаю кнопку сжатия, чтобы начать сжатие видео, он загружает файл mp4 размером 0 КБ. Пожалуйста, помогите, если кто-нибудь знаком с этой концепцией Спасибо Подробнее здесь: [url]https://stackoverflow.com/questions/78665653/facing-issues-while-building-video-compressor-using-ffmpeg-library-in-net-frame[/url]