-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.cs
56 lines (43 loc) · 1.42 KB
/
Extensions.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
using YoutubeExplode.Videos.Streams;
using System.Text.RegularExpressions;
using FFMpegCore;
using FFMpegCore.Pipes;
public static class Extensions
{
public static async Task<Stream> GetMp3Stream(this IStreamInfo streamInfo)
{
var res = new MemoryStream();
await FFMpegArguments.FromUrlInput(new Uri(streamInfo.Url))
.OutputToPipe(new StreamPipeSink(res),
options => options.ForceFormat("mp3"))
.ProcessAsynchronously();
res.Position = 0;
return res;
}
public static Stream AnnotateMp3Tag(this Stream stream, string title, string? comment)
{
TagLib.Id3v2.Tag.DefaultVersion = 4;
TagLib.Id3v2.Tag.ForceDefaultVersion = true;
string pattern = @"\[(.*?)\]";
var matches = Regex.Matches(title, pattern);
try
{
using var mp3 = TagLib.File.Create(new StreamFileAbstraction(title, stream));
if (matches.Any())
{
var contributors = matches.Select(match => match.Groups[1].Value);
mp3.Tag.Performers = contributors.ToArray();
}
if (comment != null)
{
mp3.Tag.Comment = comment;
}
mp3.Save();
}
catch (System.Exception)
{
throw;
}
return stream;
}
}