在C#中使用ffmpeg遇到麻烦,如何正确格式化字符串以升级高档视频? [英] Trouble using ffmpeg in c# how to correctly format string to upscale videos?

查看:63
本文介绍了在C#中使用ffmpeg遇到麻烦,如何正确格式化字符串以升级高档视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在用C#编写一个应用程序,以将视频升级到特定分辨率.它使用ffmpeg来做到这一点.选择视频文件后,单击1080p会创建目录文件夹,但实际上没有将放大的视频写入其中.

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.

推荐答案

据我所知, ffmpeg的scale命令具有两个维度.如果希望缩放比例保持比例,则将选项之一指定为-1.由于您的身高是1080,因此您的scale命令将为 scale = -1:1080

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

要调试这样的事情,请在参数行之后放置一个断点,指向变量(或使用locals窗口),以便出现工具提示,然后右键单击它并复制值.将该值粘贴到命令提示符下,然后查看ffmpeg是否符合您的期望.如果没有,请在命令提示符处修复参数,然后将更改返回到您的代码中

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

这篇关于在C#中使用ffmpeg遇到麻烦,如何正确格式化字符串以升级高档视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆