-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
444 lines (364 loc) · 16.4 KB
/
Program.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using HtmlAgilityPack;
using DotNetEnv;
using OpenAI.Managers;
using OpenAI;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.ObjectModels;
using FFMpegCore;
using System.Text.Json;
using System.Diagnostics;
using static System.Net.Mime.MediaTypeNames;
using System.Globalization;
namespace NewsVideoGenerator
{
public class Article
{
public string? url { get; set; }
public string? title { get; set; }
public string? content { get; set; }
}
public class Config
{
public List<string>? articles { get; set; }
public string videoDirectory { get; set; }
public string gptModel { get; set; }
public string ttsModel { get; set; }
public string ttsVoice { get; set; }
public float ttsSpeed { get; set; }
public string gptPrompt { get; set; }
public string ffmpegDirectory { get; set; }
public string? openaiAPI { get; set; }
}
public class SubtitleElement
{
public TimeSpan start { get; set; }
public TimeSpan end { get; set; }
public string text { get; set; }
}
internal class Program
{
static async Task Main(string[] args)
{
Env.Load();
string configJsonString = "";
try
{
configJsonString = File.ReadAllText(args[0]);
}
catch {
configJsonString = File.ReadAllText("config.json");
}
Config configJson = JsonSerializer.Deserialize<Config>(configJsonString);
OpenAIService openAiService = new OpenAIService(new OpenAiOptions()
{
ApiKey = Environment.GetEnvironmentVariable("OPENAI_API") ?? configJson.openaiAPI ?? ""
});
Console.WriteLine("---NEWS VIDEO GEN---");
Random random = new Random();
int id = random.Next(10000, 100000);
Console.WriteLine($"ID: {id}");
string outputPath = $"output/{id}";
System.IO.Directory.CreateDirectory(outputPath);
List<Article> articleList = new List<Article>();
foreach (var url in configJson.articles)
{
if(string.IsNullOrWhiteSpace(url)) continue;
Article article = await ScrapeArticleAsync(url);
articleList.Add(article);
}
string script = await GenerateScriptAsync(articleList, openAiService, configJson.gptModel, configJson.gptPrompt);
await GenerateAudioAsync(script, id, openAiService, configJson.ttsModel, configJson.ttsVoice, configJson.ttsSpeed);
await GenerateSubtitlesAsync(id,openAiService, $"{outputPath}/{id}.mp3");
MakeVideo(id , configJson.videoDirectory, configJson.ffmpegDirectory, outputPath);
}
static async Task<Article> ScrapeArticleAsync(string url)
{
Article article = new Article();
Console.WriteLine($"Scraping {url}");
article.url = url;
HttpClient client = new HttpClient();
string html = await client.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
Console.WriteLine($"{url} source scraped");
switch (true)
{
case var _ when url.Contains("bbc"):
Console.WriteLine("BBC article found");
article = MineBBC(article, htmlDocument);
break;
case var _ when url.Contains("cnn"):
Console.WriteLine("CNN article found");
article = MineCNN(article, htmlDocument);
break;
default:
Console.WriteLine("Source unknown");
break;
}
return article;
}
static Article MineBBC(Article article, HtmlDocument htmlDocument)
{
article.title = htmlDocument.DocumentNode.SelectSingleNode("//title").InnerText;
//Console.WriteLine($"Title: {article.title}");
var contentNodes = htmlDocument.DocumentNode.SelectNodes("//p[@class='ssrcss-1q0x1qg-Paragraph e1jhz7w10']");
if (contentNodes != null)
{
article.content = string.Join("\n", contentNodes.Select(node => node.InnerText));
//Console.WriteLine($"Content: {article.content}");
}
else
{
Console.WriteLine("Content paragraphs not found.");
}
return article;
}
static Article MineCNN(Article article, HtmlDocument htmlDocument)
{
article.title = htmlDocument.DocumentNode.SelectSingleNode("//title").InnerText;
Console.WriteLine($"Title: {article.title}");
var contentNodes = htmlDocument.DocumentNode.SelectNodes("//p[@class='paragraph inline-placeholder']");
if (contentNodes != null)
{
article.content = string.Join("\n", contentNodes.Select(node => node.InnerText));
//Console.WriteLine($"Content: {article.content}");
}
else
{
Console.WriteLine("Content paragraphs not found.");
}
return article;
}
static async Task<string> GenerateScriptAsync(List <Article> articleList, OpenAIService openAiService,string gptModelString , string gptPrompt)
{
Console.WriteLine("Generating script");
string combinedArticles = "";
foreach (var article in articleList) {
combinedArticles = combinedArticles + article.title + article.content;
}
var gptModel = "";
switch (gptModelString)
{
case ("Gpt_3_5_Turbo"):
gptModel = Models.Gpt_3_5_Turbo;
break;
case ("Gpt_4"):
gptModel = Models.Gpt_4;
break;
}
var completionResult = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem(gptPrompt),
ChatMessage.FromUser($"{combinedArticles}"),
},
Model = gptModel,
});
string script = "";
if (completionResult.Successful)
{
script = completionResult.Choices.First().Message.Content ?? "";
Console.WriteLine(script);
}
return script;
}
static async Task GenerateAudioAsync(string script, int id, OpenAIService openAiService, string ttsModelString, string ttsVoiceString , float ttsSpeed)
{
Console.WriteLine("Generating audio");
var ttsVoice = "";
switch (ttsVoiceString)
{
case "Alloy":
ttsVoice = StaticValues.AudioStatics.Voice.Alloy;
break;
case "Echo":
ttsVoice = StaticValues.AudioStatics.Voice.Echo;
break;
case "Fable":
ttsVoice = StaticValues.AudioStatics.Voice.Fable;
break;
case "Onyx":
ttsVoice = StaticValues.AudioStatics.Voice.Onyx;
break;
case "Nova":
ttsVoice = StaticValues.AudioStatics.Voice.Nova;
break;
case "Shimmer":
ttsVoice = StaticValues.AudioStatics.Voice.Shimmer;
break;
}
var ttsModel = "";
switch (ttsModelString)
{
case "Tts_1":
ttsModel = Models.Tts_1;
break;
case "Tts_1_hd":
ttsModel = Models.Tts_1_hd;
break;
}
var completionResult = await openAiService.Audio.CreateSpeech<Stream>(new AudioCreateSpeechRequest
{
Model = ttsModel,
Input = script,
Voice = ttsVoice,
ResponseFormat = StaticValues.AudioStatics.CreateSpeechResponseFormat.Mp3,
Speed = ttsSpeed
});
if (completionResult.Successful)
{
var audio = completionResult.Data!;
await using var fileStream = File.Create($"output/{id}/{id}.mp3");
await audio.CopyToAsync(fileStream);
Console.WriteLine($"Audio generated ");
}
}
static async Task GenerateSubtitlesAsync(int id, OpenAIService openAiService, string fileName)
{
Console.WriteLine("Generating subtitles");
var sampleFile = await File.ReadAllBytesAsync($"{fileName}");
var audioResult = await openAiService.Audio.CreateTranscription(new AudioCreateTranscriptionRequest
{
FileName = fileName,
File = sampleFile,
Model = Models.WhisperV1,
ResponseFormat = StaticValues.AudioStatics.ResponseFormat.Srt
});
if (audioResult.Successful)
{
string transcripton = string.Join("\n", audioResult.Text);
//Console.WriteLine(transcripton);
File.WriteAllText($"output/{id}/{id}.srt", transcripton);
AtomizeSubtitles(id, transcripton);
}
else
{
if (audioResult.Error == null)
{
throw new Exception("Unknown Error");
}
Console.WriteLine($"{audioResult.Error.Code}: {audioResult.Error.Message}");
}
}
static void AtomizeSubtitles(int id, string originalSubtitles)
{
//save srt file to originalSubtitleList
//not perfect but working (i hope)
List<SubtitleElement> originalSubtitleList = [];
using (StringReader reader = new StringReader(originalSubtitles))
{
string line;
int index = 1;
int mode = 0; // 0 = index, 1 = time, 2 = text
SubtitleElement subtitleElement = null;
while ((line = reader.ReadLine()) != null)
{
if(line == index.ToString())
{
if (subtitleElement != null)
{
originalSubtitleList.Add(subtitleElement);
}
subtitleElement = new SubtitleElement();
index++;
mode = 1;
}
else if(mode == 1)
{
string[] parts = line.Split(new string[] { " --> " }, StringSplitOptions.None);
TimeSpan startTime = TimeSpan.ParseExact(parts[0].Replace(',', '.'), "hh\\:mm\\:ss\\.fff", CultureInfo.InvariantCulture);
subtitleElement.start = startTime;
TimeSpan endTime = TimeSpan.ParseExact(parts[1].Replace(',', '.'), "hh\\:mm\\:ss\\.fff", CultureInfo.InvariantCulture);
subtitleElement.end = endTime;
mode = 2;
}
else if(mode == 2)
{
subtitleElement.text = subtitleElement.text + line;
}
}
originalSubtitleList.Add(subtitleElement); //this line is needed because for loop ends before adding the last element to the list
}
//atomize the subtitles
List<SubtitleElement> newSubtitleList = [];
foreach (SubtitleElement oldSubtitle in originalSubtitleList)
{
string[] words = oldSubtitle.text.Split(' ');
int numberOfWords = words.Length;
float lettersInText = oldSubtitle.text.Length;
TimeSpan oldSubtitleLength = oldSubtitle.end - oldSubtitle.start;
TimeSpan newSubtitleLength = oldSubtitleLength / numberOfWords;
TimeSpan totalSkipLength = TimeSpan.Zero;
for (int i = 0; i < numberOfWords; i++)
{
float lettersInWord = words[i].Length;
SubtitleElement newSubtitle = new SubtitleElement();
newSubtitle.text = words[i];
newSubtitle.start = oldSubtitle.start + totalSkipLength;
totalSkipLength = totalSkipLength + (oldSubtitleLength * (lettersInWord / lettersInText));
Console.WriteLine(totalSkipLength);
newSubtitle.end = oldSubtitle.start + totalSkipLength;
newSubtitleList.Add(newSubtitle);
}
}
//Write to file
string newSubtitles = "";
for (int i = 0; i < newSubtitleList.Count; i++)
{
string newStartString = newSubtitleList[i].start.ToString(@"hh\:mm\:ss\,fff");
string newEndString = newSubtitleList[i].end.ToString(@"hh\:mm\:ss\,fff");
newSubtitles = newSubtitles + $"{i + 1}\n{newStartString} --> {newEndString}\n{newSubtitleList[i].text}\n\n";
}
File.WriteAllText($"output/{id}/{id}Atomised.srt", newSubtitles);
Console.WriteLine(newSubtitles);
}
static string ffmpeg(string ffmpegPath, string arguments)
{
string result = String.Empty;
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = ffmpegPath;
proc.StartInfo.Arguments = arguments;
proc.Start();
proc.WaitForExit();
result = proc.StandardOutput.ReadToEnd();
}
return result;
}
static void MakeVideo(int id, string videoPath,string ffmpegPath, string outputPath)
{
Console.WriteLine("Making final video");
string audioPath = $"{outputPath}/{id}.mp3";
string subtitlesPath = $"{outputPath}/{id}.srt";
string subtitlesAtomisedPath = $"{outputPath}/{id}Atomised.srt";
string videoTrimmedPath = $"{outputPath}/{id}Trimmed.mp4";
string videoNoAudioPath = $"{outputPath}/{id}NoAudio.mp4";
string videoSubtitlesPath = $"{outputPath}/{id}Subtitles.mp4";
string videoFinalPath = $"{outputPath}/{id}.mp4";
var audioInfo = FFProbe.Analyse(audioPath);
TimeSpan audioDuration = audioInfo.Duration;
double audioTotalSeconds = audioDuration.TotalSeconds;
Console.WriteLine($"Audio duration: {audioTotalSeconds}");
Console.Write($"{TimeSpan.FromSeconds(audioTotalSeconds)}");
var videoInfo = FFProbe.Analyse(videoPath);
TimeSpan videoDuration = videoInfo.Duration;
double videoTotalSeconds = videoDuration.TotalSeconds;
Console.WriteLine($"Video duration: {videoTotalSeconds}");
Random random = new Random();
int randomTime = random.Next(10, (int)videoTotalSeconds - ((int)audioTotalSeconds) + 10);
ffmpeg(ffmpegPath, $" -i {videoPath} -ss {randomTime} -t {audioTotalSeconds} -c copy {videoTrimmedPath}");
Console.WriteLine("Video trimmed");
ffmpeg(ffmpegPath, $" -i {videoTrimmedPath} -an {videoNoAudioPath}");
Console.WriteLine("Video muted");
ffmpeg(ffmpegPath, $" -i {videoNoAudioPath} -vf \"subtitles={subtitlesAtomisedPath}:force_style='Alignment=10,Fontname=Haettenschweiler,FontSize=24,PrimaryColour=&HFFFFFF,SecondaryColour=&H000000'\" {videoSubtitlesPath}");
Console.WriteLine("Subtitles added");
ffmpeg(ffmpegPath, $"-i {videoSubtitlesPath} -i {audioPath} -c:v copy -c:a aac -strict experimental {videoFinalPath}");
Console.WriteLine("Video combined with audio");
Console.WriteLine($"Final video: {videoFinalPath}");
}
}
}