问题
So I am writing an app in c# to upscale videos to a certain resolution. It uses ffmpeg to do this. What happens is after selecting the video file, and clicking 1080p it creates the directory folder but does not actually write the upscaled video to it.
I think I must have a string format issue:
private void HD_Click(object sender, EventArgs e)
{
if (textBox1.Text == null)
{
MessageBox.Show("You've not selected your video file yet. Please do so before continuing, cheers.");
}
else
{
var originFilePath = textBox1.Text;
string name = Path.GetFileName(originFilePath);
byte[] bytes = null;
using (FileStream fileStream = new FileStream(originFilePath, FileMode.Open, FileAccess.Read))
{
using (MemoryStream ms = new MemoryStream())
{
fileStream.CopyTo(ms);
bytes = ms.ToArray();
}
var localStoragePath = Path.Combine(Path.GetTempPath(), name);
var directoryPath = Path.GetDirectoryName(localStoragePath);
Directory.CreateDirectory(directoryPath);
File.WriteAllBytes(localStoragePath, bytes);
Console.WriteLine($"File copy successful: {File.Exists(localStoragePath)}");
var readBack = File.ReadAllBytes(localStoragePath);
Console.WriteLine($"Read file Back: {readBack.Length}, {localStoragePath}");
var resizedFolderPath = @"C:\upscaledvideohere";
Directory.CreateDirectory(resizedFolderPath);
var resizedFiePath = Path.Combine(resizedFolderPath, Path.GetFileName(localStoragePath));
var psi = new ProcessStartInfo();
psi.FileName = @"C:\ffmpeg-2020-12-27-git-bff6fbead8-full_build\binffmpeg.exe";
psi.Arguments = $"-i \"{localStoragePath}\" -vf scale=1080 \"{resizedFiePath}\"";
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
psi.UseShellExecute = true;
Console.WriteLine($"Args: {psi.Arguments}");
try
{
using (Process exeProcess = Process.Start(psi))
{
Console.WriteLine($"process started with processId: {exeProcess.Id}");
exeProcess.WaitForExit();
Console.WriteLine($"Exit Code: {exeProcess.ExitCode}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace.ToString());
Console.WriteLine(ex.Message.ToString());
return;
}
Console.WriteLine($"process completed");
Console.WriteLine($"Temp Out Exists: {File.Exists(resizedFiePath)}");
}
Console.ReadLine();
}
}
I am wondering where the string format error could be? Thank you.
回答1:
As far as I am aware, the scale command of ffmpeg takes two dimensions. If you want scaling to remain proportional you specify one of the options as -1. As 1080 is your height, your scale command would be scale=-1:1080
To debug things like this, put a breakpoint after your arguments line, point to the variable(or use the locals window) so the tooltip appears and then right click it and copy the value. Paste the value into a command prompt and see whether ffmpeg does what you expect. Fix the arguments in the command prompt if it doesn't, and carry the changes back into your code
来源:https://stackoverflow.com/questions/65502499/trouble-using-ffmpeg-in-c-sharp-how-to-correctly-format-string-to-upscale-videos