Skip to content

Commit c8bcd7e

Browse files
committed
0428
1 parent 7f23167 commit c8bcd7e

File tree

7 files changed

+228
-79
lines changed

7 files changed

+228
-79
lines changed

AI.Labs.Module/BusinessObjects/AudioBooks/AudioBookTextAudioItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private VoiceSolution GetFinalSolution()
258258
//this.State = TTSState.Generatting;
259259
var vs = item.GetFinalSolution();
260260
var p = Path.Combine(item.AudioBook.OutputPath, $"{item.Index}.wav");
261-
await vs.GenerateAudioToFile(item.ArticleText, p);
261+
await vs.GenerateAudioToFile(item.Subtitle.CnText, p);
262262
//OutputFileName = p;
263263
//this.State = TTSState.Generated;
264264
var audioDuration = (int)FFmpegHelper.GetDuration(p).Value;

AI.Labs.Module/BusinessObjects/FilterComplexScripts/FilterComplexScript.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,19 @@ public FilterComplexCommand InputVideo(string filename)
9191
public List<FilterComplexCommand> VideoProductClips { get; set; } = new List<FilterComplexCommand>();
9292

9393

94-
public void ImportAudioClip(AudioParameter p)
94+
public void ImportAudioClip(AudioParameter audioParameter)
9595
{
96+
ArgumentNullException.ThrowIfNull(audioParameter,nameof(audioParameter));
9697
#warning 简单判断如果是mp3文件
97-
var filename = p.FileName;
98+
var filename = audioParameter.FileName;
9899
if (!string.IsNullOrEmpty(filename) && Path.GetExtension(filename).ToLower() == ".mp3")
99100
{
100101
var fn = filename;
101102
fn = filename + ".wav";
102103
FFmpegHelper.Mp32Wav(filename, fn);
103-
p.FileName = fn;
104+
audioParameter.FileName = fn;
104105
}
105-
AudioParameters.Add(p);
106+
AudioParameters.Add(audioParameter);
106107
}
107108

108109
public FilterComplexCommand InputAudio(string filename)
@@ -164,7 +165,8 @@ public void Export()
164165
filterComplex: filterComplex,
165166
outputFiles: OutputFileName,//
166167
outputOptions: $"-c:v libx264 -crf 18 -y -map \"{videoProductV2_WithDrawTexts.OutputLable}\" -map \"{InputAudioCommands.First().OutputLable[1..^1]}\" ",
167-
showWindow: true
168+
showWindow: true,
169+
writeDebugBat:true
168170
);
169171

170172
Console.WriteLine($"时长:{FFmpegHelper.GetDuration(OutputFileName)}");
@@ -224,6 +226,7 @@ public DrawTextOption DrawCurrentTime(int x = 10, int y = 450, int fontSize = 24
224226
Top = y.ToString(),
225227
Option = DefaultTextOption,
226228
StartTime = TimeSpan.Zero,
229+
227230
EndTime = TimeSpan.FromSeconds(90)
228231
};
229232

AI.Labs.Module/BusinessObjects/FilterComplexScripts/VideoSubtitleOption.cs

Lines changed: 134 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,46 @@
99
using Xabe.FFmpeg;
1010

1111
namespace AI.Labs.Module.BusinessObjects.FilterComplexScripts
12-
{
13-
public enum FontAlignment
12+
{
13+
// 枚举定义
14+
public enum TextAlignment
1415
{
15-
TopLeft = 1,
16-
TopCenter,
17-
TopRight,
18-
CenterLeft,
19-
Center,
20-
CenterRight,
21-
BottomLeft,
22-
BottomCenter,
23-
BottomRight
16+
BottomLeft = 1,
17+
BottomCenter = 2,
18+
BottomRight = 3,
19+
MiddleLeft = 4,
20+
MiddleCenter = 5,
21+
MiddleRight = 6,
22+
TopLeft = 7,
23+
TopCenter = 8,
24+
TopRight = 9
25+
}
26+
27+
public enum BorderStyle
28+
{
29+
Normal = 1,
30+
NoBorderShadow = 3
2431
}
2532
public class VideoSubtitleOption
2633
{
2734
public string SrtFileName { get; set; }
2835
public int? FontSize { get; set; }
29-
public string FontName { get; set; } = "微软雅黑";
30-
public string PrimaryColour { get; set; }
31-
public string SecondaryColour { get; set; }
32-
public string TertiaryColour { get; set; }
33-
public string BackColour { get; set; }
34-
public string OutlineColour { get; set; }
35-
public int? OutlineThickness { get; set; }
36-
public FontAlignment? Alignment { get; set; }
36+
public string FontName { get; set; }
37+
public string PrimaryColor { get; set; }
38+
public string SecondaryColor { get; set; }
39+
public string OutlineColor { get; set; }
40+
public string BackColor { get; set; }
41+
public bool? Bold { get; set; }
42+
public bool? Italic { get; set; }
43+
public bool? Underline { get; set; }
44+
public bool? StrikeOut { get; set; }
45+
public int? Outline { get; set; }
46+
public int? Shadow { get; set; }
3747
public int? MarginL { get; set; }
3848
public int? MarginR { get; set; }
3949
public int? MarginV { get; set; }
40-
50+
public TextAlignment? Alignment { get; set; }
51+
public BorderStyle? BorderStyle { get; set; }
4152
public string GetStyle()
4253
{
4354
var styleOptions = new List<string>();
@@ -48,44 +59,124 @@ public string GetStyle()
4859
if (!string.IsNullOrEmpty(FontName))
4960
styleOptions.Add($"Fontname={FontName}");
5061

51-
if (!string.IsNullOrEmpty(PrimaryColour))
52-
styleOptions.Add($"PrimaryColour={PrimaryColour}");
53-
54-
if (!string.IsNullOrEmpty(SecondaryColour))
55-
styleOptions.Add($"SecondaryColour={SecondaryColour}");
56-
57-
if (!string.IsNullOrEmpty(TertiaryColour))
58-
styleOptions.Add($"TertiaryColour={TertiaryColour}");
59-
60-
if (!string.IsNullOrEmpty(BackColour))
61-
styleOptions.Add($"BackColour={BackColour}");
62-
63-
if (!string.IsNullOrEmpty(OutlineColour))
64-
styleOptions.Add($"OutlineColour={OutlineColour}");
65-
66-
if (OutlineThickness.HasValue)
67-
styleOptions.Add($"OutlineThickness={OutlineThickness.Value}");
68-
69-
if (Alignment.HasValue)
70-
styleOptions.Add($"Alignment={(int)Alignment.Value}");
71-
62+
if (!string.IsNullOrEmpty(PrimaryColor))
63+
styleOptions.Add($"PrimaryColour={PrimaryColor}");
64+
if (!string.IsNullOrEmpty(SecondaryColor))
65+
styleOptions.Add($"SecondaryColour={SecondaryColor}");
66+
if (!string.IsNullOrEmpty(OutlineColor))
67+
styleOptions.Add($"OutlineColour={OutlineColor}");
68+
if (!string.IsNullOrEmpty(BackColor))
69+
styleOptions.Add($"BackColour={BackColor}");
70+
if (Bold.HasValue)
71+
styleOptions.Add($"Bold={(Bold.Value ? -1 : 0)}");
72+
if (Italic.HasValue)
73+
styleOptions.Add($"Italic={(Italic.Value ? -1 : 0)}");
74+
if (Underline.HasValue)
75+
styleOptions.Add($"Underline={(Underline.Value ? -1 : 0)}");
76+
if (StrikeOut.HasValue)
77+
styleOptions.Add($"StrikeOut={(StrikeOut.Value ? -1 : 0)}");
78+
if (Outline.HasValue)
79+
styleOptions.Add($"Outline={Outline.Value}");
80+
if (Shadow.HasValue)
81+
styleOptions.Add($"Shadow={Shadow.Value}");
7282
if (MarginL.HasValue)
7383
styleOptions.Add($"MarginL={MarginL.Value}");
74-
7584
if (MarginR.HasValue)
7685
styleOptions.Add($"MarginR={MarginR.Value}");
77-
7886
if (MarginV.HasValue)
7987
styleOptions.Add($"MarginV={MarginV.Value}");
88+
if (Alignment.HasValue)
89+
styleOptions.Add($"Alignment={(int)Alignment.Value}");
90+
if (BorderStyle.HasValue)
91+
styleOptions.Add($"BorderStyle={(int)BorderStyle.Value}");
8092

8193
return string.Join(",", styleOptions);
8294
}
8395

8496
public string GetScript()
8597
{
98+
// Assuming DrawTextOption.FixText is a method to sanitize SrtFileName
8699
return $"subtitles='{DrawTextOption.FixText(SrtFileName)}':force_style='{GetStyle()}'";
87100
}
88101
}
102+
103+
104+
//public enum FontAlignment
105+
//{
106+
// TopLeft = 1,
107+
// TopCenter,
108+
// TopRight,
109+
// CenterLeft,
110+
// Center,
111+
// CenterRight,
112+
// BottomLeft,
113+
// BottomCenter,
114+
// BottomRight
115+
//}
116+
//public class VideoSubtitleOption
117+
//{
118+
// public string SrtFileName { get; set; }
119+
// public int? FontSize { get; set; }
120+
// public string FontName { get; set; } = "微软雅黑";
121+
// public string PrimaryColour { get; set; }
122+
// public string SecondaryColour { get; set; }
123+
// public string TertiaryColour { get; set; }
124+
// public string BackColour { get; set; }
125+
// public string OutlineColour { get; set; }
126+
// public int? OutlineThickness { get; set; }
127+
// public FontAlignment? Alignment { get; set; }
128+
// public int? MarginL { get; set; }
129+
// public int? MarginR { get; set; }
130+
// public int? MarginV { get; set; }
131+
132+
// public string GetStyle()
133+
// {
134+
// var styleOptions = new List<string>();
135+
136+
// if (FontSize.HasValue)
137+
// styleOptions.Add($"Fontsize={FontSize.Value}");
138+
139+
// if (!string.IsNullOrEmpty(FontName))
140+
// styleOptions.Add($"Fontname={FontName}");
141+
142+
// if (!string.IsNullOrEmpty(PrimaryColour))
143+
// styleOptions.Add($"PrimaryColour={PrimaryColour}");
144+
145+
// if (!string.IsNullOrEmpty(SecondaryColour))
146+
// styleOptions.Add($"SecondaryColour={SecondaryColour}");
147+
148+
// if (!string.IsNullOrEmpty(TertiaryColour))
149+
// styleOptions.Add($"TertiaryColour={TertiaryColour}");
150+
151+
// if (!string.IsNullOrEmpty(BackColour))
152+
// styleOptions.Add($"BackColour={BackColour}");
153+
154+
// if (!string.IsNullOrEmpty(OutlineColour))
155+
// styleOptions.Add($"OutlineColour={OutlineColour}");
156+
157+
// if (OutlineThickness.HasValue)
158+
// styleOptions.Add($"OutlineThickness={OutlineThickness.Value}");
159+
160+
// if (Alignment.HasValue)
161+
// styleOptions.Add($"Alignment={(int)Alignment.Value}");
162+
163+
// if (MarginL.HasValue)
164+
// styleOptions.Add($"MarginL={MarginL.Value}");
165+
166+
// if (MarginR.HasValue)
167+
// styleOptions.Add($"MarginR={MarginR.Value}");
168+
169+
// if (MarginV.HasValue)
170+
// styleOptions.Add($"MarginV={MarginV.Value}");
171+
172+
// return string.Join(",", styleOptions);
173+
// }
174+
175+
// public string GetScript()
176+
// {
177+
// return $"subtitles='{DrawTextOption.FixText(SrtFileName)}':force_style='{GetStyle()}'";
178+
// }
179+
//}
89180
}
90181

91182
//5.Style Lines, [v4 + Styles] section

AI.Labs.Module/BusinessObjects/Helper/FFmpegHelper.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ public static void CreateEmptyVideo(string outputFile, int durationMS, string im
238238
);
239239
}
240240

241-
public static void ExecuteFFmpegCommand(string inputOptions = "", string inputFiles = "", string filterComplex = "", string outputOptions = "", string outputFiles = "", bool showWindow = false)
241+
public static void ExecuteFFmpegCommand(
242+
string inputOptions = "", string inputFiles = "", string filterComplex = "", string outputOptions = "", string outputFiles = "",
243+
bool showWindow = false,
244+
bool writeDebugBat = false)
242245
{
243246
if (!string.IsNullOrEmpty(filterComplex))
244247
{
@@ -249,7 +252,13 @@ public static void ExecuteFFmpegCommand(string inputOptions = "", string inputFi
249252

250253
filterComplex = $"-/filter_complex {filterComplexFile}";
251254
}
252-
ExecuteFFmpegCommand($"{inputOptions} {inputFiles} {filterComplex} {outputOptions} {outputFiles}", showWindow);
255+
var cmd = $"{inputOptions} {inputFiles} {filterComplex} {outputOptions} {outputFiles}";
256+
if (!string.IsNullOrEmpty(outputFiles) && writeDebugBat)
257+
{
258+
File.WriteAllText(outputFiles + ".bat", $"{ffmpegFile} {cmd}");
259+
}
260+
261+
ExecuteFFmpegCommand(cmd, showWindow);
253262
}
254263
/// <summary>
255264
/// 执行ffmpeg
@@ -265,7 +274,6 @@ public static void ExecuteFFmpegCommand(string command, bool showWindow)
265274
p.StartInfo.CreateNoWindow = !showWindow;
266275
p.StartInfo.UseShellExecute = false;
267276
Debug.WriteLine($"{ffmpegFile} {command}");
268-
269277
Debug.WriteLine($"{ffmpegFile} {command.Replace(";", ";\n")}");
270278

271279

AI.Labs.Module/BusinessObjects/VideoTranslate/VideoInfo.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using AI.Labs.Module.BusinessObjects.STT;
1616
using System.Security.Cryptography;
1717
using AI.Labs.Module.BusinessObjects.Helper;
18+
using System.Text;
1819
//using SubtitlesParser.Classes.Parsers;
1920
namespace AI.Labs.Module.BusinessObjects.VideoTranslate
2021
{
@@ -347,17 +348,43 @@ public bool FixSRTIncludeContext
347348

348349
public VisualFilterComplexScript VideoScript
349350
{
350-
get
351+
get
351352
{
352353
return GetPropertyValue<VisualFilterComplexScript>(nameof(VideoScript));
353354
}
354-
set
355+
set
355356
{
356-
SetPropertyValue(nameof(VideoScript), value);
357+
SetPropertyValue(nameof(VideoScript), value);
357358
}
358359
}
359360

360-
public (string CnSRT,string EnSRT) SaveFixedSRT()
361+
public static string InsertNewlines(string text, int maxLineLength)
362+
{
363+
StringBuilder sb = new StringBuilder();
364+
int currentLineLength = 0;
365+
366+
foreach (char c in text)
367+
{
368+
if (currentLineLength >= maxLineLength && !char.IsWhiteSpace(c))
369+
{
370+
sb.Append("\n");
371+
currentLineLength = 0;
372+
}
373+
374+
sb.Append(c);
375+
currentLineLength++;
376+
377+
// Reset line length after newline
378+
if (c == '\n' || c == '\r' || c == '\\')
379+
{
380+
currentLineLength = 0;
381+
}
382+
}
383+
384+
return sb.ToString();
385+
}
386+
387+
public (string CnSRT, string EnSRT) SaveFixedSRT()
361388
{
362389
var cnSrtFile = new SRTFile() { FileName = Path.Combine(ProjectPath, "cnsrt.fix.srt"), UseIndex = true };
363390
var enSrtFile = new SRTFile() { FileName = Path.Combine(ProjectPath, "ensrt.fix.srt"), UseIndex = true };
@@ -370,7 +397,7 @@ public VisualFilterComplexScript VideoScript
370397

371398
if (!string.IsNullOrEmpty(cnText))
372399
{
373-
cnText = cnText.Replace("\n", "");
400+
cnText = InsertNewlines(cnText.Replace("\n", ""), 40);
374401
cnSrtItem.Index = item.Index;
375402
cnSrtItem.StartTime = item.Subtitle.FixedStartTime;
376403
cnSrtItem.EndTime = item.Subtitle.FixedEndTime;
@@ -641,20 +668,23 @@ public void Output(string str)
641668
this.VideoScript.Output += $"{Environment.NewLine} {DateTime.Now.TimeOfDay} {str}";
642669
}
643670

644-
public void SaveSRTToFile(SrtLanguage lang,string addationName = "",bool saveFixed = false)
671+
public void SaveSRTToFile(SrtLanguage lang, string addationName = "", bool saveFixed = false)
645672
{
646673
var t = this;
647674
//保存翻译结果
648675
var fileName = Path.Combine(t.ProjectPath, $"{t.Oid}.{lang.ToString()}{addationName}.srt");
649-
SRTHelper.SaveToSrtFile(t.Subtitles, fileName, lang,saveFixed);
650-
t.VideoChineseSRT = fileName;
676+
SRTHelper.SaveToSrtFile(t.Subtitles, fileName, lang, saveFixed);
677+
if (lang == SrtLanguage.中文)
678+
{
679+
t.VideoChineseSRT = fileName;
680+
}
651681
}
652682
}
653683

654-
public class Log:XPObject
684+
public class Log : XPObject
655685
{
656686

657-
public Log(Session s) : base(s)
687+
public Log(Session s) : base(s)
658688
{
659689

660690
}

0 commit comments

Comments
 (0)