Background The .wav format is widely used in daily work, especially when mixing audio. However, when communicating with others and clients, the .mp3 format is often preferred. Additionally, there are many other audio formats available today, such as .aac. Therefore, I need a tool to help me convert these audio files into .wav or .mp3 formats.
Structure IsAudioFile? if the files user drag in are not audio files,they are not allowed to effect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 private void FileDragBox_DragEnter (object sender, DragEventArgs e ){ if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string [] files = (string [])e.Data.GetData(DataFormats.FileDrop); bool isAudioFile = files.All(file => IsAudioFile(file )); e.Effect = isAudioFile ? DragDropEffects.Copy : DragDropEffects.None; } else { e.Effect = DragDropEffects.None; } } private void FileDragBox_DragDrop (object sender, DragEventArgs e ){ string [] files = (string [])e.Data.GetData(DataFormats.FileDrop); if (files.Length > 0 ) { var audioFiles = files.Where(file => IsAudioFile(file )).ToArray(); if (audioFiles.Length > 0 ) { FileDragBox.Text = string .Join(Environment.NewLine, audioFiles); } else { MessageBox.Show("只支持音频文件!" ); } } } private bool IsAudioFile (string file ) { string [] supportedExtensions = { ".wav" , ".mp3" , ".flac" , ".aac" , ".ogg" }; return supportedExtensions.Contains(Path.GetExtension(file ).ToLower()); }
GetIntoConvertion 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 private void convertButton_Click (object sender, EventArgs e ){ string [] files = FileDragBox.Text.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (files.Length == 0 ) { MessageBox.Show("请先拖拽文件!" ); return ; } string selectedFormat = formatComboBox.SelectedItem.ToString().ToLower(); string outputExtension = selectedFormat == "wav" ? ".wav" : ".mp3" ; foreach (var file in files) { string outputFilePath = Path.ChangeExtension(file , outputExtension); ConvertAudioFile(file , outputFilePath); } }
Accroding to the selection of itemBox,generate a path that we need,which is the prepare for the convertion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 public static void ConvertAudioFile (string inputFilePath, string outputFilePath ){ try { if (Path.GetExtension(inputFilePath).ToLower() == Path.GetExtension(outputFilePath).ToLower()) { MessageBox.Show("输入和输出文件格式相同,跳过转换。" ); return ; } if (File.Exists(outputFilePath)) { var result = MessageBox.Show($"文件 {outputFilePath} 已存在,是否覆盖?" , "确认覆盖" , MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { File.Delete(outputFilePath); } else { return ; } } string ffmpegPath = "ffmpeg" ; string arguments = $"-i \"{inputFilePath} \" \"{outputFilePath} \"" ; ProcessStartInfo startInfo = new ProcessStartInfo { FileName = ffmpegPath, Arguments = arguments, CreateNoWindow = true , UseShellExecute = false , RedirectStandardOutput = true , RedirectStandardError = true }; Process process = Process.Start(startInfo); bool exited = process.WaitForExit(30000 ); if (!exited) { process.Kill(); MessageBox.Show("转换超时!" ); return ; } string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); Console.WriteLine("FFmpeg Output: " + output); Console.WriteLine("FFmpeg Error: " + error); if (process.ExitCode != 0 ) { if (!string .IsNullOrEmpty(error)) { MessageBox.Show($"转换失败: {error} " , "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("转换失败,但没有提供错误信息。" , "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show($"转换完成: {outputFilePath} " , "成功" , MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show($"错误: {ex.Message} " ); } }
Based on the selection of the itemBox, generate the necessary paths, which will prepare for the conversion. Using the input and output paths, FFmpeg will automatically generate the desired audio file by starting an external process.