/// <summary> /// Append silence audio to the list. /// </summary> /// <param name="length">Length of the silence audio in milliseconds.</param> /// <returns>The modified list.</returns> public List <AudioTaskItem> Append(long length) { var item = new AudioTaskItem(length); NumberStack++; // Generate silence audio with ffmpeg var ffmpeg = new Ffmpeg("./ffmpeg/ffmpeg.exe"); // var taskBeep = ffmpeg.StartFfmpeg(FFMPEG_ARGS_SILENCE // .Replace("$SECS$", (length * 1000).ToString() // .Replace("$FILE_TEMP$", $"{TempDir}\\{NumberStack}.m4a")), 1000); var taskBeep = ffmpeg.StartFfmpeg(string.Format(FFMPEG_ARGS_SILENCE, length / 1000, $"{TempDir}\\{NumberStack}.mp3"), 1500); if (!taskBeep.Result) { return(null); } item.AssignNumber(NumberStack, $"{TempDir}/{NumberStack}.mp3"); Items.Add(item); totalDuration += item.Duration; IsSaved = false; return(Items); }
/// <summary> /// Export the list to a mp3 file. /// </summary> /// <param name="output">The Output object for modifying frmExport.</param> /// <param name="destination">The full path of the audio file.</param> /// <returns>Returns true if process is successful, false instead.</returns> public bool ExportFile(Output output, string destination) { output.AddLine("開始匯出"); output.SetTotalSteps(Items.Count + 2); // Creates temp dir output.AddLine("正在建立暫存檔案..."); var dirNum = 0; var outputDir = ""; while (Directory.Exists($"{TempDir}/output{dirNum}")) { dirNum++; } outputDir = $"{TempDir}/output{dirNum}".Replace("\\", "/"); Directory.CreateDirectory(outputDir); output.AddLine($"暫存資料夾位置:{outputDir}"); // Creates ffmpeg var ffmpeg = new Ffmpeg("./ffmpeg/ffmpeg.exe"); // Trim output.AddLine("準備裁剪音訊"); foreach (var taskItem in Items) { output.AddLine($"開始剪接第{taskItem.Number}段音訊,名稱:{taskItem.Name}"); output.MoveOneStep(); var taskTrim = ffmpeg.StartFfmpeg(string.Format(FFMPEG_ARGS_TRIM, taskItem.FilePathInTemp, taskItem.MsIn, taskItem.MsOut, $"{outputDir}/{Items.IndexOf(taskItem) + 1}.mp3")); // Check status if (taskTrim.Result) { // Success output.AddLine("成功"); } else { // Fail output.AddLine("出現未知錯誤,已停止作業"); return(false); // Terminates work } } // Generate join_list.txt output.AddLine("正在產生合併清單"); var listLines = new List <string>(); var index = 1; while (File.Exists($"{outputDir.Replace("/","\\")}\\{index}.mp3")) { listLines.Add($"file {outputDir.Replace("/","\\\\")}\\\\{index}.mp3"); output.AddLine(listLines[index - 1]); index++; } File.WriteAllLines($"{outputDir}/join_list.txt", listLines); // Join audios output.AddLine("開始合併"); output.MoveOneStep(); var taskCombine = ffmpeg.StartFfmpeg(string.Format(FFMPEG_ARGS_JOIN, $"{outputDir.Replace("/","\\")}\\join_list.txt", $"{outputDir.Replace("/","\\")}\\Output.mp3")); // Check for failure if (taskCombine.Result) { // Success output.AddLine("成功合併"); } else { output.AddLine("出現未知錯誤,已停止作業"); return(false); } // Copies the file File.Copy($"{outputDir}/Output.mp3", destination); output.AddLine("正在儲存檔案"); output.MoveOneStep(); return(true); }