Compare commits

..

2 Commits

Author SHA1 Message Date
3d84beb640 yt-dlp process extracted 2023-04-10 15:25:47 -04:00
3958eee080 filter out distasteful characters. namely apostrophe.
not an illegal character in a file path, which yt-dlp would handle for me, but given how I send it off to ffmpeg I trip over this sometimes
2023-04-10 14:43:27 -04:00
6 changed files with 121 additions and 141 deletions

View File

@ -10,6 +10,12 @@ namespace ttrss_co_client.tasks
public override string TaskName => "convert"; public override string TaskName => "convert";
public override async Task<Tuple<TaskStatus, WorkOrder>> ActOn(WorkOrder wo) 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(); var article = (await TtrssClient.GetArticles(wo.articleId))?.FirstOrDefault();
if(article == null){ if(article == null){
Console.Error.WriteLine($"article {wo.articleId} couldn't be retrieved!"); Console.Error.WriteLine($"article {wo.articleId} couldn't be retrieved!");
@ -17,14 +23,14 @@ namespace ttrss_co_client.tasks
} }
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); 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(); conversionProc.WaitForExit();
sw.Stop(); 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"]); 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) public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
{ {
Console.WriteLine($" YT podcastify: {headline.link.ToString()}"); Console.WriteLine($" YT podcastify: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString(); var myGuid = Guid.NewGuid().ToString();
var ytdl = new YoutubeDLSharp.YoutubeDL(); var toReturn = new WorkOrder()
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))
{ {
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 toReturn.Phase2TaskList[3] = "filemovePublish";
{ toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/";
sw.Start();
var res = await ytdl.RunVideoDownload(headline.link.ToString());
sw.Stop();
var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}"; await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0) Console.WriteLine($" {headline.title}: download trigger label removed");
{
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");
var podcastTitle = headline.feed_title; return toReturn;
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;
}
} }
} }
} }

View File

@ -11,12 +11,12 @@ namespace ttrss_co_client.tasks
public override string TaskName => "filemovePublish"; public override string TaskName => "filemovePublish";
public override async Task<Tuple<TaskStatus, WorkOrder>>ActOn(WorkOrder workOrder) 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)) if(!Directory.Exists(targetDirectory))
{ {
Directory.CreateDirectory(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(); var article = (await TtrssClient.GetArticles(workOrder.articleId))?.FirstOrDefault();
await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] - copied", article.id); await TtrssClient.UpdateArticleNote($"{article.note}\n[{DateTime.Now.ToString("o")}] - copied", article.id);
return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder); return new Tuple<TaskStatus, WorkOrder>(TaskStatus.Done, workOrder);

View File

@ -72,10 +72,6 @@ namespace ttrss_co_client.tasks
{ {
contentSegments.Add(new Tuple<double, double>(previousEdge, junkSegment.segment[0])); contentSegments.Add(new Tuple<double, double>(previousEdge, junkSegment.segment[0]));
previousEdge = junkSegment.segment[1]; previousEdge = junkSegment.segment[1];
if(previousEdge < 0.5)
{
contentSegments.Clear();
}
} }
contentSegments.Add(new Tuple<double, double>(previousEdge, segments.First().videoDuration)); contentSegments.Add(new Tuple<double, double>(previousEdge, segments.First().videoDuration));
contentSegments = contentSegments.Except(contentSegments.Where(tup => tup.Item2 - tup.Item1 < 0.5)).ToList(); contentSegments = contentSegments.Except(contentSegments.Where(tup => tup.Item2 - tup.Item1 < 0.5)).ToList();

View File

@ -11,74 +11,34 @@ namespace ttrss_co_client.tasks
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle) public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
{ {
Console.WriteLine($" standard download: {headline.link.ToString()}"); Console.WriteLine($" standard download: {headline.link.ToString()}");
var myGuid = Guid.NewGuid().ToString(); var myGuid = Guid.NewGuid().ToString();
var ytdl = new YoutubeDLSharp.YoutubeDL(); var toReturn = new WorkOrder()
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))
{ {
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 toReturn.Phase2TaskList[3] = "filemovePublish";
{ toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/recent episodes/";
sw.Start();
var res = await ytdl.RunVideoDownload(headline.link.ToString());
sw.Stop();
var outputStr = $"download {(res.Success ? "success" : "fail")} in {sw.Elapsed}"; await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
if (res.ErrorOutput != null && res.ErrorOutput.Length > 0) Console.WriteLine($" {headline.title}: download trigger label removed");
{
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
{
var outputFilename = res.Data;
Console.WriteLine($"{headline.title} downloaded.");
var toReturn = new WorkOrder() return toReturn;
{
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, res.Data.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;
}
} }
} }
} }

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);
}
}
}