Я пытаюсь ввести поток и уменьшить битрейт видео, не сохраняя его нигде, поэтому я надеюсь передать его как поток, получить выходные данные в виде потока и отправить его в цифровые океанские пространства для сохранения.
/>Но я пробовал много вещей, но мой выходной поток становится пустым. и файл пуст.
public async Task VideoOperationAsync(Stream inputStream)
{
try
{
// Ensure the input stream is at the beginning
inputStream.Position = 0;
// Create a memory stream to hold the output data
using (var outputStream = new MemoryStream())
{
var arguments = $"-i pipe:0 -b:v 2000k -f mp4 pipe:1";
Cli.Wrap("ffmpeg")
.WithArguments(arguments)
.WithStandardInputPipe(PipeSource.FromStream(inputStream))
.WithStandardOutputPipe(PipeTarget.ToStream(outputStream))
.ExecuteAsync();
// Ensure the output stream is at the beginning before reading
outputStream.Position = 0;
using (var fileStream = new FileStream(@"D:\Gremlin-data\VideoResized\output_cropped.mp4", FileMode.Create, FileAccess.Write))
{
await outputStream.CopyToAsync(fileStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
throw; // Re-throw the exception if needed
}
}
Я также ранее пробовал это:
public async Task VideoOperationAsync(Stream inputStream)
{
попробуй
{
// Убедитесь, что входной поток находится в начале
inputStream.Position = 0;
var ffmpegProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ffmpeg", // Ensure ffmpeg is in your PATH or provide the full path
Arguments = $"-i pipe:0 -b:v 2000k -f mp4 pipe:1", // Correct bitrate format
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true, // Capture standard error
UseShellExecute = false,
CreateNoWindow = true
}
};
ffmpegProcess.Start();
// Write input stream to ffmpeg's standard input asynchronously
Task writingTask = Task.Run(async () =>
{
await inputStream.CopyToAsync(ffmpegProcess.StandardInput.BaseStream);
ffmpegProcess.StandardInput.BaseStream.Close();
});
// Read ffmpeg's standard output to a memory stream asynchronously
using (MemoryStream ms = new MemoryStream())
{
Task readingTask = Task.Run(async () =>
{
await ffmpegProcess.StandardOutput.BaseStream.CopyToAsync(ms);
});
Подробнее здесь: https://stackoverflow.com/questions/787 ... -in-ffmpeg