yt-dlp process extracted

This commit is contained in:
Adam R Grey 2023-04-10 15:25:47 -04:00
parent 3958eee080
commit 3d84beb640
5 changed files with 121 additions and 147 deletions

View File

@ -10,6 +10,12 @@ namespace ttrss_co_client.tasks
public override string TaskName => "convert";
public override async Task<Tuple<TaskStatus, WorkOrder>> ActOn(WorkOrder wo)
{
var conversionFilenameTarget = wo.data["path"].Substring(0, wo.data["path"].LastIndexOf('.')) + wo.data["conversion-target"];
if(conversionFilenameTarget == wo.data["path"])
{
Console.WriteLine("no conversion needed");
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.ContinueNow, wo);
}
var article = (await TtrssClient.GetArticles(wo.articleId))?.FirstOrDefault();
if(article == null){
Console.Error.WriteLine($"article {wo.articleId} couldn't be retrieved!");
@ -17,14 +23,14 @@ namespace ttrss_co_client.tasks
}
var sw = new Stopwatch();
sw.Start();
var conversionProc = Process.Start("ffmpeg", $"-y -loglevel quiet -i \"{wo.data["path"]}\" \"{wo.data["conversion-target"]}\"");
var conversionProc = Process.Start("ffmpeg", $"-y -loglevel quiet -i \"{wo.data["path"]}\" \"{conversionFilenameTarget}\"");
conversionProc.WaitForExit();
sw.Stop();
if(File.Exists(wo.data["conversion-target"]))
if(File.Exists(conversionFilenameTarget))
{
Console.WriteLine($" converted {wo.data["path"]} -> {wo.data["conversion-target"]}");
Console.WriteLine($" converted {wo.data["path"]} -> {conversionFilenameTarget}");
File.Delete(wo.data["path"]);
wo.data["path"] = wo.data["conversion-target"];
wo.data["path"] =conversionFilenameTarget;
}

View File

@ -11,81 +11,43 @@ namespace ttrss_co_client.tasks
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
{
Console.WriteLine($" YT podcastify: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString();
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{myGuid}";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
if(!Path.Exists(ytdl.OutputFolder))
var toReturn = new WorkOrder()
{
Directory.CreateDirectory(ytdl.OutputFolder);
articleId = headline.id,
Phase2TaskList = new Dictionary<int, string>(),
data = new Dictionary<string, string>(),
guid = Guid.Parse(myGuid)
};
var podcastTitle = headline.feed_title;
var titlingLabel = labelsWRTArticle.FirstOrDefault(l => l.@checked && l.caption?.ToLower().StartsWith(Conf.PodcastTitlePrefix) == true);
if(titlingLabel != null)
{
podcastTitle = titlingLabel.caption.Substring(Conf.PodcastTitlePrefix.Length);
await TtrssClient.SetArticleLabel(titlingLabel.id, false, headline.id);
}
Console.WriteLine($"article {headline.id} - yt podcastifying; {podcastTitle}");
toReturn.Phase2TaskList[0] = "yt-dlp";
toReturn.data["ytdlp-link"] = headline.link.ToString();
toReturn.Phase2TaskList[1] = "convert";
toReturn.data["conversion-target"] = ".mp3";
if(headline.link.Host.EndsWith("youtube.com"))
{
toReturn.Phase2TaskList[2] = "sponsorblock";
}
try
{
sw.Start();
var res = await ytdl.RunVideoDownload(headline.link.ToString());
sw.Stop();
toReturn.Phase2TaskList[3] = "filemovePublish";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/";
var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}";
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
{
outputStr += "\n" + string.Join('\n', res.ErrorOutput);
}
Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}");
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
Console.WriteLine($" {headline.title}: download trigger label removed");
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
Console.WriteLine($" {headline.title}: download trigger label removed");
var podcastTitle = headline.feed_title;
var titlingLabel = labelsWRTArticle.FirstOrDefault(l => l.@checked && l.caption?.ToLower().StartsWith(Conf.PodcastTitlePrefix) == true);
if(titlingLabel != null)
{
await TtrssClient.SetArticleLabel(titlingLabel.id, false, headline.id);
podcastTitle = titlingLabel.caption.Substring(Conf.PodcastTitlePrefix.Length);
}
Console.WriteLine($"article {headline.id} - podcastifying; {podcastTitle}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - YT podcastify (dl) {(res.Success ? "success" : "failure")}; {string.Join('\n', res.ErrorOutput)}", headline.id);
if (!res.Success)
{
return null;
}
else
{
var outputFilename = res.Data;
Console.WriteLine($"{headline.title} downloaded.");
var toReturn = new WorkOrder()
{
articleId = headline.id,//<-- that way later tasks can update the note
Phase2TaskList = new Dictionary<int, string>(),
data = new Dictionary<string, string>(),
guid = Guid.Parse(myGuid)
};
toReturn.data["path"] = outputFilename;
if(!outputFilename.EndsWith(".mp3"))
{
Console.WriteLine($"{headline.title} needs conversion task.");
toReturn.Phase2TaskList[0] = "convert";
toReturn.data["conversion-target"] = outputFilename.Substring(0, res.Data.LastIndexOf('.')) + ".mp3";
}
toReturn.Phase2TaskList[1] = "sponsorblock";
toReturn.Phase2TaskList[2] = "filemovePublish";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/{Path.GetFileName(toReturn.data["conversion-target"])}";
return toReturn;
}
}
catch (Exception e)
{
Console.Error.WriteLine($"fatal error in yt podcastify DL for {headline.link}");
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", headline.id);
return null;
}
return toReturn;
}
}
}

View File

@ -11,12 +11,12 @@ namespace ttrss_co_client.tasks
public override string TaskName => "filemovePublish";
public override async Task<Tuple<TaskStatus, WorkOrder>>ActOn(WorkOrder workOrder)
{
var targetDirectory = Path.GetDirectoryName(workOrder.data["publish-target"]);
var targetDirectory = workOrder.data["publish-target"];
if(!Directory.Exists(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}
File.Move(workOrder.data["path"], workOrder.data["publish-target"], true);
File.Move(workOrder.data["path"], Path.Combine(workOrder.data["publish-target"], Path.GetFileName(workOrder.data["path"])), true);
var article = (await TtrssClient.GetArticles(workOrder.articleId))?.FirstOrDefault();
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] - copied", article.id);
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder);

View File

@ -11,84 +11,34 @@ namespace ttrss_co_client.tasks
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
{
Console.WriteLine($" standard download: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString();
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{myGuid}";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
var sw = new Stopwatch();
if(!Path.Exists(ytdl.OutputFolder))
var toReturn = new WorkOrder()
{
Directory.CreateDirectory(ytdl.OutputFolder);
articleId = headline.id,
Phase2TaskList = new Dictionary<int, string>(),
data = new Dictionary<string, string>(),
guid = Guid.Parse(myGuid)
};
toReturn.Phase2TaskList[0] = "yt-dlp";
toReturn.data["ytdlp-link"] = headline.link.ToString();
toReturn.Phase2TaskList[1] = "convert";
toReturn.data["conversion-target"] = ".mp4";
if(headline.link.Host.EndsWith("youtube.com"))
{
toReturn.Phase2TaskList[2] = "sponsorblock";
}
try
{
sw.Start();
var res = await ytdl.RunVideoDownload(headline.link.ToString());
sw.Stop();
toReturn.Phase2TaskList[3] = "filemovePublish";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/recent episodes/";
var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}";
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0)
{
outputStr += "\n" + string.Join('\n', res.ErrorOutput);
}
Console.WriteLine($" {headline.link} -> {res.Data}\n{outputStr}");
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
Console.WriteLine($" {headline.title}: label removed");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - standard dl {(res.Success ? "success" : "failure")}; {string.Join('\n', res.ErrorOutput)}", headline.id);
if (!res.Success)
{
return null;
}
else
{
Console.WriteLine($"{headline.title} downloaded.");
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
Console.WriteLine($" {headline.title}: download trigger label removed");
var outputFilename = res.Data;
foreach(char c in "'\"")
{
outputFilename = outputFilename.Replace(c, '_');
}
if(outputFilename != res.Data)
{
File.Move(res.Data, outputFilename);
}
var toReturn = new WorkOrder()
{
articleId = headline.id,//<-- that way later tasks can update the note
Phase2TaskList = new Dictionary<int, string>(),
data = new Dictionary<string, string>(),
guid = Guid.Parse(myGuid)
};
toReturn.data["path"] = outputFilename;
if(!outputFilename.EndsWith(".mp4"))
{
Console.WriteLine($"{headline.title} needs conversion task.");
toReturn.Phase2TaskList[0] = "convert";
toReturn.data["conversion-target"] = outputFilename.Substring(0, outputFilename.LastIndexOf('.')) + ".mp4";
}
if(headline.link.Host.EndsWith("youtube.com"))
{
toReturn.Phase2TaskList[1] = "sponsorblock";
}
toReturn.Phase2TaskList[2] = "filemovePublish";
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/recent episodes/{Path.GetFileName(toReturn.data["conversion-target"])}";
return toReturn;
}
}
catch (Exception e)
{
Console.Error.WriteLine($"fatal error in standard DL for {headline.link}");
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", headline.id);
return null;
}
return toReturn;
}
}
}

56
tasks/Ytdlp.cs Normal file
View File

@ -0,0 +1,56 @@
using System;
using System.Text;
using Newtonsoft.Json;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using ttrss_co_client.ttrss;
using ttrss_co_client.ttrss.datastructures;
namespace ttrss_co_client.tasks
{
///<summary>Move to output</summary>
public class Ytdlp : Phase2Task
{
public override string TaskName => "yt-dlp";
public override async Task<Tuple<TaskStatus, WorkOrder>>ActOn(WorkOrder workOrder)
{
var article = (await TtrssClient.GetArticles(workOrder.articleId)).FirstOrDefault();
var sw = new Stopwatch();
var ytdl = new YoutubeDLSharp.YoutubeDL();
ytdl.YoutubeDLPath = "yt-dlp";
ytdl.FFmpegPath = "ffmpeg";
ytdl.OutputFolder = $"{Conf.WorkingDirectory}/{workOrder.guid}";
ytdl.OutputFileTemplate = "%(upload_date)s - %(title)s - [%(id)s].%(ext)s";
if(!Path.Exists(ytdl.OutputFolder))
{
Directory.CreateDirectory(ytdl.OutputFolder);
}
sw.Start();
var res = await ytdl.RunVideoDownload(workOrder.data["ytdlp-link"]);
sw.Stop();
if (!res.Success)
{
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] yt-dlp fatal error. \n{string.Join('\n', res.ErrorOutput)}", article.id);
return null;
}
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] yt-dlp {(res.Success ? "success" : "fail")} in {sw.Elapsed}", article.id);
var outputFilename = res.Data;
foreach(char c in "'\"")
{
outputFilename = outputFilename.Replace(c, '_');
}
if(outputFilename != res.Data)
{
File.Move(res.Data, outputFilename);
}
workOrder.data["path"] = outputFilename;
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.ContinueNow, workOrder);
}
}
}