This commit is contained in:
owenchen 2026-06-02 11:01:00 +08:00
parent bcd8ebab67
commit 5f222a23b3
6 changed files with 207 additions and 44 deletions

View File

@ -59,6 +59,8 @@
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="NAudio" Version="2.3.0" />
<PackageReference Include="NAudio.Lame" Version="2.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="CliWrap" Version="3.6.6" />

Binary file not shown.

View File

@ -2,7 +2,7 @@
"profiles": {
"CloudBuilder.Topshelf": {
"commandName": "Project",
"commandLineArgs": "task:ChineseNameExtractorTask book_id:B000008"
"commandLineArgs": "task:TtsTask book_id:B000008 voice:Sherpa2 actor:澈蕾 actor_voice:Sherpa1 save_path:D:\\\\Net8\\\\FileServer\\\\Backup\\\\DmsFile\\\\voice"
}
}
}

View File

@ -42,7 +42,7 @@ namespace CloudBuilder.Topshelf.Task
List<HanlpConstituencyNode> cons = null;
SpeakerAnalysisHelper speakerFeatureLearner = new SpeakerAnalysisHelper();
SpeakerAnalysisHelper speakerAnalysisHelper = new SpeakerAnalysisHelper();
AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == guid).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray();
if (ents == null || ents.Length == 0) return;
@ -54,11 +54,12 @@ namespace CloudBuilder.Topshelf.Task
int totalLines = ents.Length;
int processedLines = 0; // 已处理行数计数器
AiChapterPersonEntity aiChapterPerson;
List<AiChapterPersonEntity> cps = new List<AiChapterPersonEntity>();
foreach (var ent in ents)
{
processedLines++;
person = speakerFeatureLearner.GetPerson(hanlp, ent.Content);
person = speakerAnalysisHelper.GetPerson(hanlp, ent.Content);
paragraphIndex = ent.ParagraphId;
if (!string.IsNullOrEmpty(person))
@ -72,8 +73,9 @@ namespace CloudBuilder.Topshelf.Task
aiChapterPerson.PersonName = p;
aiChapterPerson.ChapterId = ent.ChapterId;
aiChapterPerson.ParagraphIndex = ent.ParagraphId;
aiChapterPerson.SentenceIndex= ent.SentenceIndex;
aiChapterPerson.OrderIndex = orderIndex++;
cps.Add(aiChapterPerson);
repositoryAiChapterPersonEntity.InsertNow(aiChapterPerson);
}
}
@ -81,6 +83,9 @@ namespace CloudBuilder.Topshelf.Task
UpdateProgress(processedLines, totalLines);
}
speakerAnalysisHelper.AiChapterPersons = cps.ToArray();
speakerAnalysisHelper.MatchPerson(ents, repositoryAiSentenceEntity);
}
catch (Exception ex)
{

View File

@ -1,9 +1,14 @@
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.AI.Entity;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Topshelf.Utility;
using Microsoft.IdentityModel.Tokens;
using NAudio.Lame;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
@ -12,6 +17,7 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
namespace CloudBuilder.Topshelf.Task.TTS
{
//task:TtsTask book_id:B000008 voice:Sherpa2 actor:韩立 actor_voice:Sherpa1 save_path:D:\\Net8\\FileServer\\Backup\\DmsFile\\voice
//task:TtsTask voice:Sherpa2 save_path:D:\\Net8\\FileServer\\Backup\\DmsFile\\voice\\20260528112739.mp3 text:二愣子睁大着双眼,直直望着茅草和烂泥糊成的黑屋顶,身上盖着的旧棉被,已呈深黄色,看不出原来的本来面目,还若有若无的散发着淡淡的霉味。
public class TtsTask : IScheduleTask
{
@ -24,19 +30,173 @@ namespace CloudBuilder.Topshelf.Task.TTS
public void Run(Dictionary<string, string> bodyDict)
{
string save_path = string.Empty, text = string.Empty, voice = ChineseTtsVoice.Sherpa2.ToString();
IRepository<AiSentenceViewEntity> repository = service.GetRepository<IRepository<AiSentenceViewEntity>>();
string bookId = string.Empty;
if (bodyDict.ContainsKey("book_id"))
bookId = bodyDict["book_id"];
AiSentenceViewEntity[] ents = repository.DetachedEntities.Where(x => x.BookId == bookId).OrderBy(x => x.ChapterId).ThenBy(x => x.ParagraphId).ThenBy(x => x.SentenceIndex).ToArray();
if (ents == null || ents.Length == 0) return;
string save_path = string.Empty, text = string.Empty, voice = ChineseTtsVoice.Sherpa2.ToString(), actor = string.Empty, actor_voice = ChineseTtsVoice.Sherpa1.ToString();
if (bodyDict.ContainsKey("actor"))
actor = bodyDict["actor"];
if (bodyDict.ContainsKey("voice"))
voice = bodyDict["voice"];
if (bodyDict.ContainsKey("actor_voice"))
actor_voice = bodyDict["actor_voice"];
if (bodyDict.ContainsKey("save_path"))
save_path = bodyDict["save_path"];
if (bodyDict.ContainsKey("text"))
text = bodyDict["text"];
GenerateAndMergeChapterAudio(ents, actor, actor_voice, voice, save_path);
}
if (string.IsNullOrEmpty(voice) || string.IsNullOrEmpty(save_path) || string.IsNullOrEmpty(text)) return;
public void GenerateAndMergeChapterAudio(AiSentenceViewEntity[] ents, string actor, string actor_voice, string mainVoice, string save_path)
{
// 1. 按 BookId + ChapterId 分组(关键!)
var chapterGroups = ents
.Where(ent => ent.Content.Any(c => c >= 0x4E00 && c <= 0x9FFF)) // 只保留含中文的句子
.GroupBy(ent => new { ent.BookId, ent.ChapterId })
.ToList();
SaveAudioToFileHelper.AudioToFile(EnumHelper.ToEnum<ChineseTtsVoice>(voice), text, save_path);
int totalLines = chapterGroups.Count();
int processedLines = 0; // 已处理行数计数器
foreach (var chapter in chapterGroups)
{
processedLines++;
string bookId = chapter.Key.BookId;
int chapterId = chapter.Key.ChapterId;
List<string> tempMp3List = new List<string>(); // 本章所有碎片音频
// 2. 生成本章所有句子音频
foreach (AiSentenceViewEntity ent in chapter)
{
string voice = ChineseTtsVoice.Sherpa2.ToString();
if (ent.DialogueIndc == YesNoPolicy.YES)
{
if (ent.PersonName == actor)
voice = actor_voice;
else
voice = AI.Policy.ChineseTtsVoiceRandom.GetRandom().ToString();
}
else
{
voice = mainVoice;
}
if (string.IsNullOrEmpty(voice) || string.IsNullOrEmpty(ent.Content))
continue;
// 碎片文件名BookId_ChapterId_ParagraphId_SentenceId.mp3
string fileName = string.Format("{0}_{1}_{2}_{3}.mp3", ent.BookId, ent.ChapterId, ent.ParagraphId, ent.SentenceId);
string filePath = Path.Combine(save_path, fileName);
// 生成音频
SaveAudioToFileHelper.AudioToFile(EnumHelper.ToEnum<ChineseTtsVoice>(voice), ent.Content, filePath);
tempMp3List.Add(filePath); // 加入合并列表
}
Thread.Sleep(100);
// 3. 合并为BookId_ChapterId.mp3
string outputFileName = string.Format("{0}_{1}.mp3", bookId, chapterId);
string outputFilePath = Path.Combine(save_path, outputFileName);
MergeChapterMp3Files(tempMp3List, outputFilePath);
// 4. 可选:合并后删除碎片文件(节省空间)
foreach (var chunk in tempMp3List)
{
try { File.Delete(chunk); } catch { }
}
Console.WriteLine($"章节合并完成:{outputFileName}");
UpdateProgress(processedLines, totalLines);
}
}
/// <summary>
/// 最终修复:完全解决 No fmt chunk / 采样率不兼容 / 只有一截声音
/// </summary>
public void MergeChapterMp3Files(List<string> chunkFiles, string outputFilePath)
{
var validFiles = chunkFiles.Where(File.Exists).ToList();
if (validFiles.Count == 0) return;
try
{
// 目标格式32000Hz 单声道 16bit
WaveFormat targetFormat = new WaveFormat(32000, 16, 1);
// 直接生成 MP3不经过 WAV 文件,彻底避免 fmt 错误
using (var mp3Writer = new LameMP3FileWriter(outputFilePath, targetFormat, 128))
{
foreach (var file in validFiles)
{
try
{
// 释放文件占用
GC.Collect();
GC.WaitForPendingFinalizers();
// 用Windows系统解码器读取任何MP3
using (var reader = new MediaFoundationReader(file))
using (var resampler = new MediaFoundationResampler(reader, targetFormat))
{
byte[] buffer = new byte[8192];
int read;
while ((read = resampler.Read(buffer, 0, buffer.Length)) > 0)
{
mp3Writer.Write(buffer, 0, read);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"跳过文件:{file},错误:{ex.Message}");
}
}
}
Console.WriteLine($"✅ 合并成功:{outputFilePath},大小:{new FileInfo(outputFilePath).Length} 字节");
}
catch (Exception ex)
{
Console.WriteLine($"❌ 合并失败:{ex.Message}");
}
}
// 辅助方法WAV 转 MP3
private void WaveFileToMp3(byte[] wavData, string mp3Path)
{
using (var ms = new MemoryStream(wavData))
using (var waveReader = new WaveFileReader(ms))
using (var mp3Writer = new LameMP3FileWriter(mp3Path, waveReader.WaveFormat, 128))
{
waveReader.CopyTo(mp3Writer);
}
}
private static void UpdateProgress(int processed, int total)
{
double progressPercent = (double)processed / total * 100;
int progressBarLength = 50; // 进度条总长度
int filledLength = (int)(progressPercent / 100 * progressBarLength);
// 构建进度条(如:[██████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░]
string progressBar = "[" + new string('█', filledLength) + new string('░', progressBarLength - filledLength) + "]";
string progressInfo = $"{progressBar} {progressPercent:F2}% | 已处理:{processed}/{total} ";
Console.Write($"\r{progressInfo}");
Console.Out.Flush();
}
}
}

View File

@ -1,4 +1,5 @@
using CloudBuilder.AI.Entity;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Topshelf.Python;
using System;
using System.Collections.Generic;
@ -184,7 +185,35 @@ namespace CloudBuilder.Topshelf.Utility
return arg0;
}
//public AiSentenceEntity[] AiParagraphs { get; set; }
public void MatchPerson(AiSentenceViewEntity[] aiSentences, IRepository<AiSentenceEntity> repositoryAiSentenceEntity)
{
if (AiChapterPersons == null || AiChapterPersons.Length == 0) return;
AiSentenceEntity aiSentence;
string dialogueName = string.Empty;
foreach (AiSentenceViewEntity aiSentenceView in aiSentences)
{
dialogueName= string.Empty;
if (aiSentenceView.DialogueIndc != YesNoPolicy.YES) continue;
if (aiSentences.Where(x => x.ChapterId == aiSentenceView.ChapterId && x.ParagraphId == aiSentenceView.ParagraphId).Count() > 1)
{
if (AiChapterPersons.Where(x => x.ChapterId == aiSentenceView.ChapterId && x.ParagraphIndex == aiSentenceView.ParagraphId && aiSentenceView.SentenceIndex == x.SentenceIndex).Any())
{
dialogueName = AiChapterPersons.Where(x => x.ChapterId == aiSentenceView.ChapterId && x.ParagraphIndex == aiSentenceView.ParagraphId && aiSentenceView.SentenceIndex == x.SentenceIndex).FirstOrDefault().PersonName;
}
if (AiChapterPersons.Where(x => x.ChapterId == aiSentenceView.ChapterId && x.ParagraphIndex == aiSentenceView.ParagraphId && aiSentenceView.SentenceIndex != x.SentenceIndex && x.PersonName != dialogueName).Any())
{
aiSentenceView.PersonName = AiChapterPersons.Where(x =>x.ChapterId==aiSentenceView.ChapterId && x.ParagraphIndex == aiSentenceView.ParagraphId && aiSentenceView.SentenceIndex != x.SentenceIndex && x.PersonName != dialogueName).OrderBy(x => x.OrderIndex).FirstOrDefault().PersonName;
}
}
aiSentence = new AiSentenceEntity();
ObjectCopyHelper.Copy(aiSentenceView, aiSentence);
repositoryAiSentenceEntity.UpdateNow(aiSentence);
}
}
public AiSentenceEntity[] AiSentences { get; set; }
@ -203,37 +232,4 @@ D -->|匹配失败| E{规则3段落内最近人物};
E -->|| C;
E -->|| F[+];
1
/
- 使
2 1-2
1 2 3
3
3.
使 +
1
2
3
3
IsSuspected
2
GuidContentPersonName /
A/B 线
*/