我希望它在应用程序继续运行时进行 ffmpeg 转换

问题描述 投票:0回答:1

用yd-dl.exe下载成功,但没有用ffmpeg将下载的视频转换为mp4。当我停止在 Visual Studio 中编译项目时,它将 ffmpeg 文件转换为 mp4。

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YouTube_MP4_indir
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
        }

        private async void btnDownload_Click(object sender, EventArgs e)
        {
            searchBox.Enabled = false;
            btnDownload.Enabled = false;
            pictureBox2.Visible = true;
            pictureBox3.Visible = false;

            string url = searchBox.Text.Trim();
            if (string.IsNullOrEmpty(url))
            {
                DialogResult result = MessageBox.Show("Lütfen geçerli bir YouTube URL'si giriniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                if (result == DialogResult.OK)
                {
                    searchBox.Text = "";
                    searchBox.Enabled = true;
                    btnDownload.Enabled = true;
                }

                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
                return;
            }

            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string videosFolderPath = Path.Combine(desktopPath, "Videolar");

            if (!Directory.Exists(videosFolderPath))
            {
                Directory.CreateDirectory(videosFolderPath);
            }

            string videoTitle = await GetVideoTitle(url);

            if (string.IsNullOrEmpty(videoTitle))
            {
                MessageBox.Show("Video başlığı alınamadı.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
                return;
            }

            string inputFilePath = Path.Combine(videosFolderPath, $"{videoTitle}.mp4");
            string outputFilePath = Path.Combine(videosFolderPath, $"{videoTitle}-dönüştürülmüş.mp4");

            try
            {
                var ytDlpPath = Path.Combine(Application.StartupPath, "files", "yt-dlp.exe");

                var startInfo = new ProcessStartInfo()
                {
                    FileName = ytDlpPath,
                    Arguments = $"-f bestvideo[height<=1080]+bestaudio/best --merge-output-format mp4 --output \"{inputFilePath}\" {url}",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                var process = Process.Start(startInfo);
                string output = await process.StandardOutput.ReadToEndAsync();
                string error = await process.StandardError.ReadToEndAsync();

                await process.WaitForExitAsync();

                if (process.ExitCode == 0)
                {
                    // Paralel olarak FFmpeg dönüştürme işlemini başlat
                    _ = Task.Run(() => ConvertToMp4(inputFilePath, outputFilePath));

                    MessageBox.Show("İndirme tamamlandı. Video dönüştürülüyor.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    searchBox.Text = "";
                    searchBox.Enabled = true;
                    btnDownload.Enabled = true;

                    pictureBox2.Visible = false;
                    pictureBox3.Visible = true;
                }
                else
                {
                    MessageBox.Show("Lütfen sadece video linki giriniz", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    pictureBox2.Visible = false;
                    pictureBox3.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hata: " + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);

                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
            }
        }

        private async Task<string> GetVideoTitle(string url)
        {
            try
            {
                var ytDlpPath = Path.Combine(Application.StartupPath, "files", "yt-dlp.exe");

                var startInfo = new ProcessStartInfo()
                {
                    FileName = ytDlpPath,
                    Arguments = $"-e {url}",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                var process = Process.Start(startInfo);
                string output = await process.StandardOutput.ReadToEndAsync();
                await process.WaitForExitAsync();

                return output.Trim();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hata: " + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        private async Task ConvertToMp4(string inputFilePath, string outputFilePath)
        {
            try
            {
                var ffmpegPath = Path.Combine(Application.StartupPath, "files", "ffmpeg.exe");

                var startInfo = new ProcessStartInfo
                {
                    FileName = ffmpegPath,
                    Arguments = $"-i \"{inputFilePath}\" -c:v libx264 -preset ultrafast -crf 23 -s hd1080 \"{outputFilePath}\"",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                var process = Process.Start(startInfo);
                string output = await process.StandardOutput.ReadToEndAsync();
                string error = await process.StandardError.ReadToEndAsync();

                await process.WaitForExitAsync();

                if (process.ExitCode == 0)
                {
                    MessageBox.Show("Dönüştürme işlemi başarılı.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    if (File.Exists(inputFilePath))
                    {
                        File.Delete(inputFilePath);
                    }
                }
                else
                {
                    MessageBox.Show("Dönüştürme sırasında bir hata oluştu: " + error, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hata: " + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Form2 form2;

        private void button2_Click(object sender, EventArgs e)
        {
            if (form2 == null || form2.IsDisposed)
            {
                form2 = new Form2();
                form2.Show();
            }
            else
            {
                form2.BringToFront();
            }
        }
    }
}

用yd-dl.exe下载成功。下载视频后,我想用 ffmpeg 将其转换为 mp4,但如果不停止项目,它就不会转换。

c# forms ffmpeg
1个回答
0
投票

您正在以错误的方式处理从 stdout 和 stderr 的读取。 获取它们的输出必须通过事件处理程序而不是使用

await
来完成。 这就是为什么应用程序在您终止之前不会执行任何操作,因为它们在尝试读取控制台输出时被阻止。

您最好按照此处的示例进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.