twitch vod downloading done
ffmpeg essentials fix Project reorganized git lfs ffmpeg removed ffmpeg added
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace VDownload.Services.Utility.Encryption
|
||||
{
|
||||
public interface IEncryptionService
|
||||
{
|
||||
byte[] Decrypt(byte[] ciphertext);
|
||||
byte[] Encrypt(byte[] plaintext);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class EncryptionService : IEncryptionService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public byte[] Encrypt(byte[] plaintext)
|
||||
{
|
||||
return ProtectedData.Protect(plaintext, null, DataProtectionScope.CurrentUser);
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] ciphertext)
|
||||
{
|
||||
return ProtectedData.Unprotect(ciphertext, null, DataProtectionScope.CurrentUser);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,191 @@
|
||||
using FFMpegCore;
|
||||
using FFMpegCore.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Models;
|
||||
using VDownload.Services.Data.Configuration;
|
||||
using VDownload.Services.Data.Settings;
|
||||
|
||||
namespace VDownload.Services.Utility.FFmpeg
|
||||
{
|
||||
public class FFmpegBuilder
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
protected readonly IConfigurationService _configurationService;
|
||||
protected readonly ISettingsService _settingsService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region FIELDS
|
||||
|
||||
protected TimeSpan? _trimStart;
|
||||
protected TimeSpan? _trimEnd;
|
||||
|
||||
protected bool _progressReporterJoined = false;
|
||||
protected Action<double> _progressReporter;
|
||||
protected TimeSpan _progressReporterVideoDuration;
|
||||
|
||||
protected bool _cancellationTokenJoined = false;
|
||||
protected CancellationToken _cancellationToken;
|
||||
|
||||
protected MediaType _mediaType = MediaType.Original;
|
||||
|
||||
protected string _inputFile;
|
||||
protected string _outputFile;
|
||||
|
||||
protected FFOptions _options;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
internal FFmpegBuilder(IConfigurationService configurationService, ISettingsService settingsService)
|
||||
{
|
||||
_configurationService = configurationService;
|
||||
_settingsService = settingsService;
|
||||
|
||||
_options = new FFOptions
|
||||
{
|
||||
BinaryFolder = _settingsService.Data.Common.Processing.FFmpegLocation,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public FFmpegBuilder SetMediaType(MediaType mediaType)
|
||||
{
|
||||
_mediaType = mediaType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FFmpegBuilder TrimStart(TimeSpan start)
|
||||
{
|
||||
_trimStart = start;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FFmpegBuilder TrimEnd(TimeSpan end)
|
||||
{
|
||||
_trimEnd = end;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FFmpegBuilder JoinProgressReporter(Action<double> progressReporter, TimeSpan videoDuration)
|
||||
{
|
||||
_progressReporterJoined = true;
|
||||
_progressReporter = progressReporter;
|
||||
_progressReporterVideoDuration = videoDuration;
|
||||
return this;
|
||||
}
|
||||
|
||||
public FFmpegBuilder JoinCancellationToken(CancellationToken cancellationToken)
|
||||
{
|
||||
_cancellationTokenJoined = true;
|
||||
_cancellationToken = cancellationToken;
|
||||
return this;
|
||||
}
|
||||
|
||||
public async Task RunAsync(string inputFile, string outputFile)
|
||||
{
|
||||
_inputFile = inputFile;
|
||||
_outputFile = outputFile;
|
||||
|
||||
FFMpegArgumentProcessor ffmpegArguments = FFMpegArguments.FromFileInput(inputFile, true, async (options) => await BuildInputArgumentOptions(options))
|
||||
.OutputToFile(outputFile, true, async (options) => await BuildOutputArgumentOptions(options));
|
||||
|
||||
if (_cancellationTokenJoined)
|
||||
{
|
||||
ffmpegArguments = ffmpegArguments.CancellableThrough(_cancellationToken);
|
||||
}
|
||||
|
||||
if (_progressReporterJoined)
|
||||
{
|
||||
ffmpegArguments = ffmpegArguments.NotifyOnProgress(_progressReporter, _progressReporterVideoDuration);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await ffmpegArguments.ProcessAsynchronously(true, _options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private async Task BuildInputArgumentOptions(FFMpegArgumentOptions options)
|
||||
{
|
||||
options.UsingMultithreading(_settingsService.Data.Common.Processing.UseMultithreading);
|
||||
options.WithSpeedPreset((Speed)_settingsService.Data.Common.Processing.Speed);
|
||||
if (_settingsService.Data.Common.Processing.UseHardwareAcceleration)
|
||||
{
|
||||
options.WithHardwareAcceleration(HardwareAccelerationDevice.Auto);
|
||||
}
|
||||
|
||||
if (_trimStart is not null)
|
||||
{
|
||||
options.WithCustomArgument($"-ss {_trimStart}");
|
||||
}
|
||||
|
||||
if (_trimEnd is not null)
|
||||
{
|
||||
options.WithCustomArgument($"-to {_trimEnd}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task BuildOutputArgumentOptions(FFMpegArgumentOptions options)
|
||||
{
|
||||
IMediaAnalysis analysis = await FFProbe.AnalyseAsync(_inputFile, _options);
|
||||
string audioCodec = analysis.AudioStreams.First().CodecName;
|
||||
string videoCodec = analysis.VideoStreams.First().CodecName;
|
||||
|
||||
string extension = Path.GetExtension(_outputFile).Replace(".", string.Empty);
|
||||
Data.Configuration.Models.Muxer muxer = _configurationService.Common.Processing.Muxers.First(x => x.Extension == extension);
|
||||
|
||||
if (_mediaType != MediaType.OnlyAudio)
|
||||
{
|
||||
IEnumerable<string> availableCodecs = muxer.VideoCodecs;
|
||||
string selectedCodec = availableCodecs.Contains(videoCodec) ? "copy" : availableCodecs.First();
|
||||
options.WithCustomArgument($"-vcodec {selectedCodec}");
|
||||
}
|
||||
else
|
||||
{
|
||||
options.WithCustomArgument("-vn");
|
||||
}
|
||||
|
||||
if (_mediaType != MediaType.OnlyVideo)
|
||||
{
|
||||
IEnumerable<string> availableCodecs = muxer.AudioCodecs;
|
||||
string selectedCodec = availableCodecs.Contains(audioCodec) ? "copy" : availableCodecs.First();
|
||||
options.WithCustomArgument($"-acodec {selectedCodec}");
|
||||
}
|
||||
else
|
||||
{
|
||||
options.WithCustomArgument("-an");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FFMpegCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Services.Data.Configuration;
|
||||
using VDownload.Services.Data.Settings;
|
||||
|
||||
namespace VDownload.Services.Utility.FFmpeg
|
||||
{
|
||||
public interface IFFmpegService
|
||||
{
|
||||
FFmpegBuilder CreateBuilder();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class FFmpegService : IFFmpegService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
protected readonly IConfigurationService _configurationService;
|
||||
protected readonly ISettingsService _settingsService;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public FFmpegService(IConfigurationService configurationService, ISettingsService settingsService)
|
||||
{
|
||||
_configurationService = configurationService;
|
||||
_settingsService = settingsService;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public FFmpegBuilder CreateBuilder() => new FFmpegBuilder(_configurationService, _settingsService);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FFMpegCore" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\VDownload.Models\VDownload.Models.csproj" />
|
||||
<ProjectReference Include="..\..\VDownload.Services.Data\VDownload.Services.Data.Settings\VDownload.Services.Data.Settings.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json;
|
||||
using System.Xml.Serialization;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Collections;
|
||||
|
||||
namespace VDownload.Services.Utility.HttpClient
|
||||
{
|
||||
public interface IHttpClientService
|
||||
{
|
||||
Task<T?> SendRequestAsync<T>(HttpRequest request);
|
||||
Task<string> SendRequestAsync(HttpRequest request);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class HttpClientService : IHttpClientService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
private readonly System.Net.Http.HttpClient _httpClient;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public HttpClientService(System.Net.Http.HttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<T?> SendRequestAsync<T>(HttpRequest request) => JsonConvert.DeserializeObject<T>(await SendRequestAsync(request));
|
||||
public async Task<string> SendRequestAsync(HttpRequest request)
|
||||
{
|
||||
StringBuilder urlBuilder = new StringBuilder(request.Url);
|
||||
if (request.Query.Count > 0)
|
||||
{
|
||||
Dictionary<string, object> query = request.Query.ToDictionary();
|
||||
KeyValuePair<string, object> queryElement = query.ElementAt(0);
|
||||
query.Remove(queryElement.Key);
|
||||
|
||||
urlBuilder.Append($"?{queryElement.Key}={queryElement.Value}");
|
||||
|
||||
foreach (KeyValuePair<string, object> queryElementLoop in query)
|
||||
{
|
||||
urlBuilder.Append($"&{queryElementLoop.Key}={queryElementLoop.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
HttpMethod method = request.MethodType switch
|
||||
{
|
||||
HttpMethodType.GET => HttpMethod.Get,
|
||||
HttpMethodType.POST => HttpMethod.Post,
|
||||
HttpMethodType.PUT => HttpMethod.Put,
|
||||
HttpMethodType.PATCH => HttpMethod.Patch,
|
||||
HttpMethodType.DELETE => HttpMethod.Delete,
|
||||
};
|
||||
|
||||
HttpRequestMessage httpRequest = new HttpRequestMessage(method, urlBuilder.ToString());
|
||||
|
||||
if (request.Body is not null)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(request.Body);
|
||||
HttpContent content = new StringContent(json);
|
||||
content.Headers.ContentType.MediaType = "application/json";
|
||||
httpRequest.Content = content;
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, string> header in request.Headers)
|
||||
{
|
||||
httpRequest.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(httpRequest);
|
||||
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VDownload.Services.Utility.HttpClient
|
||||
{
|
||||
public enum HttpMethodType
|
||||
{
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
PATCH,
|
||||
DELETE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VDownload.Services.Utility.HttpClient
|
||||
{
|
||||
public class HttpRequest
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public HttpMethodType MethodType { get; private set; }
|
||||
public string Url { get; private set; }
|
||||
public Dictionary<string, string> Headers { get; private set; }
|
||||
public Dictionary<string, object> Query { get; private set; }
|
||||
public object? Body { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public HttpRequest(HttpMethodType methodType, string url)
|
||||
{
|
||||
MethodType = methodType;
|
||||
Url = url;
|
||||
Headers = new Dictionary<string, string>();
|
||||
Query = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VDownload.Services.Utility.HttpClient
|
||||
{
|
||||
public class Token
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public string Schema { get; private set; }
|
||||
public byte[] TokenValue { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public Token(string schema, byte[] tokenValue)
|
||||
{
|
||||
Schema = schema;
|
||||
TokenValue = tokenValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public override string ToString() => $"{Schema} {Encoding.UTF8.GetString(TokenValue)}";
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user