微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 c# 中使用 ffmpeg 时遇到问题如何正确格式化字符串以升级视频?

如何解决在 c# 中使用 ffmpeg 时遇到问题如何正确格式化字符串以升级视频?

所以我正在用 c# 编写一个应用程序来将视频升级到特定的分辨率。它使用 ffmpeg 来做到这一点。选择视频文件并单击 1080p 后会发生什么情况,它会创建目录文件夹,但实际上并未将放大的视频写入其中。

我想我一定是字符串格式有问题:

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();
                }
            }
        
    

我想知道字符串格式错误可能在哪里?谢谢。

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。