TwitchDownloader/dotnet/src/TwitchArchive.Core/Services/DownloaderService.cs

50 lines
2 KiB
C#

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace TwitchArchive.Core.Services
{
public class DownloaderService : IDownloaderService
{
private readonly IProcessRunner _runner;
public DownloaderService(IProcessRunner runner)
{
_runner = runner;
}
public async Task<bool> DownloadVodAsync(TwitchArchive.Core.Api.VodInfo vod, string outputPath, CancellationToken ct = default)
{
try
{
var bin = "TwitchDownloaderCLI"; // expect to be on PATH or configured elsewhere
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? ".");
var args = $"videodownload -u https://www.twitch.tv/videos/{(vod.Id.StartsWith("v") ? vod.Id.Substring(1) : vod.Id)} -q best -t 10 --ffmpeg-path ffmpeg --collision Rename -o \"{outputPath}\"";
var res = await _runner.RunAsync(new ProcessRunOptions { FileName = bin, Arguments = args, RedirectOutput = true }, ct).ConfigureAwait(false);
return res.ExitCode == 0;
}
catch
{
return false;
}
}
public async Task<bool> DownloadChatJsonAsync(string vodId, string jsonPath, CancellationToken ct = default)
{
try
{
var bin = "TwitchDownloaderCLI";
Directory.CreateDirectory(Path.GetDirectoryName(jsonPath) ?? ".");
if (vodId.StartsWith("v")) vodId = vodId.Substring(1);
var args = $"chatdownload --id {vodId} --embed-images --collision Rename -o \"{jsonPath}\"";
var res = await _runner.RunAsync(new ProcessRunOptions { FileName = bin, Arguments = args, RedirectOutput = true }, ct).ConfigureAwait(false);
return res.ExitCode == 0 && File.Exists(jsonPath);
}
catch
{
return false;
}
}
}
}