複数プレイリスト対応
This commit is contained in:
1 parent
be6183f05e
commit
c0fdf1708a
8 files changed
+198
-48
No files matched your search
@@ -31,6 +31,32 @@ namespace MusicDownloader.Class.Downloader
|
||||
|
||||
[JsonPropertyName("url")]
|
||||
public string? Url { get; set; }
|
||||
|
||||
[JsonPropertyName("upload_date")]
|
||||
public string? UploadDate { get; set; }
|
||||
|
||||
[JsonPropertyName("release_year")]
|
||||
public uint? ReleaseYear { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 公開年またはリリース年を取得
|
||||
/// </summary>
|
||||
public uint? GetYear()
|
||||
{
|
||||
if (ReleaseYear.HasValue && ReleaseYear.Value > 0)
|
||||
{
|
||||
return ReleaseYear.Value;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(UploadDate) && UploadDate.Length >= 4)
|
||||
{
|
||||
if (uint.TryParse(UploadDate.Substring(0, 4), out var y) && y > 1900 && y < 2100)
|
||||
{
|
||||
return y;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,6 +70,9 @@ namespace MusicDownloader.Class.Downloader
|
||||
[JsonPropertyName("title")]
|
||||
public string? Title { get; set; }
|
||||
|
||||
[JsonPropertyName("uploader")]
|
||||
public string? Uploader { get; set; }
|
||||
|
||||
[JsonPropertyName("entries")]
|
||||
public List<YtPlaylistEntry>? Entries { get; set; }
|
||||
}
|
||||
@@ -107,24 +136,36 @@ namespace MusicDownloader.Class.Downloader
|
||||
|
||||
LogManager.AddLog($"保存先ディレクトリ: {targetFolder}");
|
||||
|
||||
var entries = playlistInfo?.Entries?.Where(e => !string.IsNullOrWhiteSpace(e.Id)).ToList();
|
||||
if (entries == null || entries.Count == 0)
|
||||
// 有効な動画エントリのみを抽出(削除済みや非公開動画は除外)
|
||||
var rawEntries = playlistInfo?.Entries ?? new List<YtPlaylistEntry>();
|
||||
var validEntries = rawEntries
|
||||
.Where(e => !string.IsNullOrWhiteSpace(e.Id) &&
|
||||
!string.IsNullOrWhiteSpace(e.Title) &&
|
||||
e.Title != "[Private video]" &&
|
||||
e.Title != "[Deleted video]")
|
||||
.ToList();
|
||||
|
||||
if (validEntries.Count == 0)
|
||||
{
|
||||
// 単一動画または取得できなかった場合、URLそのものを単一アイテムとしてダウンロード
|
||||
LogManager.AddLog($"プレイリストのエントリが見つからないか単一動画URLです。直接ダウンロードを試行します。");
|
||||
await DownloadAndProcessSingleAsync(playlistUrl, 1, 1, targetFolder, sanitizedPlaylistName, null);
|
||||
if (rawEntries.Count > 0)
|
||||
{
|
||||
LogManager.AddWarning($"プレイリストに有効な公開動画が見つかりませんでした (全 {rawEntries.Count} 件が非公開または削除)");
|
||||
return;
|
||||
}
|
||||
|
||||
LogManager.AddLog($"プレイリスト「{sanitizedPlaylistName}」: 合計 {entries.Count} 曲の処理を開始します。");
|
||||
// 単一動画URLまたはエントリがフラット取得できなかった場合、直接ダウンロード
|
||||
LogManager.AddLog($"プレイリストのエントリが見つからないか単一動画URLです。直接ダウンロードを試行します。");
|
||||
await DownloadAndProcessSingleAsync(playlistUrl, 1, 1, targetFolder, sanitizedPlaylistName, null, playlistInfo?.Uploader);
|
||||
return;
|
||||
}
|
||||
|
||||
var archiveFile = Path.Combine(targetFolder, "archive.txt");
|
||||
var totalCount = (uint)validEntries.Count;
|
||||
LogManager.AddLog($"プレイリスト「{sanitizedPlaylistName}」: 有効な全 {totalCount} 曲の処理を開始します。");
|
||||
|
||||
for (int i = 0; i < entries.Count; i++)
|
||||
for (int i = 0; i < validEntries.Count; i++)
|
||||
{
|
||||
var entry = entries[i];
|
||||
var entry = validEntries[i];
|
||||
var videoIndex = (uint)(i + 1);
|
||||
var totalCount = entries.Count;
|
||||
var videoUrl = !string.IsNullOrEmpty(entry.Url) && entry.Url.StartsWith("http")
|
||||
? entry.Url
|
||||
: $"https://www.youtube.com/watch?v={entry.Id}";
|
||||
@@ -132,10 +173,13 @@ namespace MusicDownloader.Class.Downloader
|
||||
LogManager.AddLog($"--------------------------------------------------------------------------------");
|
||||
LogManager.AddLog($"[{videoIndex}/{totalCount}] 処理開始: {entry.Title ?? entry.Id}");
|
||||
|
||||
await DownloadAndProcessSingleAsync(videoUrl, videoIndex, totalCount, targetFolder, sanitizedPlaylistName, entry);
|
||||
await DownloadAndProcessSingleAsync(videoUrl, videoIndex, totalCount, targetFolder, sanitizedPlaylistName, entry, playlistInfo?.Uploader);
|
||||
|
||||
// サーバー負荷軽減および確実性のための待機(2秒)
|
||||
await Task.Delay(2000);
|
||||
// サーバー負荷軽減および確実性のための待機(1.5秒)
|
||||
await Task.Delay(1500);
|
||||
|
||||
// 1曲ごとにメモリを速やかに解放(省メモリ化)
|
||||
GC.Collect(1, GCCollectionMode.Optimized, false);
|
||||
}
|
||||
|
||||
LogManager.AddSuccess($"================================================================================");
|
||||
@@ -147,10 +191,11 @@ namespace MusicDownloader.Class.Downloader
|
||||
private async Task DownloadAndProcessSingleAsync(
|
||||
string videoUrl,
|
||||
uint index,
|
||||
int totalCount,
|
||||
uint totalCount,
|
||||
string targetFolder,
|
||||
string playlistName,
|
||||
YtPlaylistEntry? entryInfo)
|
||||
YtPlaylistEntry? entryInfo,
|
||||
string? playlistUploader)
|
||||
{
|
||||
var archiveFile = Path.Combine(targetFolder, "archive.txt");
|
||||
var filePrefix = $"{index:D2} - ";
|
||||
@@ -235,15 +280,22 @@ namespace MusicDownloader.Class.Downloader
|
||||
// 歌詞処理 (.lrc作成および歌詞テキスト取得)
|
||||
var lyrics = await _lyricsManager.ProcessLyricsAsync(targetOpus, title, artist, entryInfo?.Duration, targetFolder, filePrefix);
|
||||
|
||||
// メタデータ (TagLibSharp) の書き込み
|
||||
// 公開年(リリース年)の取得
|
||||
var year = entryInfo?.GetYear();
|
||||
|
||||
// メタデータ (TagLibSharp) の書き込み(完全なメタデータ埋め込み)
|
||||
AudioTagManager.ApplyMetadata(
|
||||
opusPath: targetOpus,
|
||||
title: title,
|
||||
artist: artist,
|
||||
album: playlistName,
|
||||
trackNumber: index,
|
||||
trackCount: totalCount,
|
||||
year: year,
|
||||
lyrics: lyrics,
|
||||
thumbnailPath: thumbnailToUse);
|
||||
thumbnailPath: thumbnailToUse,
|
||||
comment: videoUrl,
|
||||
albumArtist: playlistUploader);
|
||||
|
||||
// プレイリスト代表カバー画像として cover.jpg が存在しなければコピー
|
||||
var albumCoverPath = Path.Combine(targetFolder, "cover.jpg");
|
||||
|
||||
@@ -17,8 +17,12 @@ namespace MusicDownloader.Class.Metadata
|
||||
string artist,
|
||||
string album,
|
||||
uint trackNumber,
|
||||
uint trackCount,
|
||||
uint? year,
|
||||
string? lyrics,
|
||||
string? thumbnailPath)
|
||||
string? thumbnailPath,
|
||||
string? comment = null,
|
||||
string? albumArtist = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -33,8 +37,22 @@ namespace MusicDownloader.Class.Metadata
|
||||
// 基本タグの設定
|
||||
file.Tag.Title = title;
|
||||
file.Tag.Performers = new[] { artist };
|
||||
file.Tag.AlbumArtists = new[] { !string.IsNullOrWhiteSpace(albumArtist) ? albumArtist : artist };
|
||||
file.Tag.Album = album;
|
||||
file.Tag.Track = trackNumber;
|
||||
file.Tag.TrackCount = trackCount;
|
||||
|
||||
// 公開年(リリース年)の設定
|
||||
if (year.HasValue && year.Value > 0)
|
||||
{
|
||||
file.Tag.Year = year.Value;
|
||||
}
|
||||
|
||||
// コメント(動画URL等)の設定
|
||||
if (!string.IsNullOrWhiteSpace(comment))
|
||||
{
|
||||
file.Tag.Comment = comment;
|
||||
}
|
||||
|
||||
// 歌詞の埋め込み (LYRICS / UNSYNCEDLYRICS タグ)
|
||||
if (!string.IsNullOrWhiteSpace(lyrics))
|
||||
@@ -42,7 +60,7 @@ namespace MusicDownloader.Class.Metadata
|
||||
file.Tag.Lyrics = lyrics;
|
||||
}
|
||||
|
||||
// カバーアートの埋め込み
|
||||
// カバーアートの埋め込み (METADATA_BLOCK_PICTURE)
|
||||
if (!string.IsNullOrWhiteSpace(thumbnailPath) && File.Exists(thumbnailPath))
|
||||
{
|
||||
try
|
||||
@@ -61,7 +79,8 @@ namespace MusicDownloader.Class.Metadata
|
||||
}
|
||||
|
||||
file.Save();
|
||||
LogManager.AddSuccess($"メタデータを埋め込みました: 「{artist} - {title}」 [Track {trackNumber}]");
|
||||
var yearStr = (year.HasValue && year.Value > 0) ? $" ({year.Value}年)" : "";
|
||||
LogManager.AddSuccess($"メタデータを埋め込みました: 「{artist} - {title}」{yearStr} [Track {trackNumber}/{trackCount}]");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
+94
-15
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -8,12 +10,19 @@ using MusicDownloader.Class.Downloader;
|
||||
using MusicDownloader.Class.Lyrics;
|
||||
|
||||
// =========================================================================
|
||||
// 【設定】ダウンロード対象のYouTubeプレイリストURLを指定してください
|
||||
// ※ 環境変数 PLAYLIST_URL が指定されている場合はそちらが最優先されます
|
||||
// 【設定】ダウンロード対象のYouTubeプレイリストURLリスト
|
||||
// ※ ここにURLを追加することで、複数のプレイリストを順次ダウンロードします
|
||||
// ※ また、出力先の playlists.txt にURLを記述することでも追加可能です
|
||||
// =========================================================================
|
||||
string defaultPlaylistUrl = "https://www.youtube.com/playlist?list=PL4o29bINVT4EG_y-k5jGoOu3-Am8Nvi10";
|
||||
List<string> defaultPlaylistUrls = new()
|
||||
{
|
||||
"https://www.youtube.com/playlist?list=PLrVu_CLXh58LDqEMjDWTCpEbAIAp8uZyu",
|
||||
"https://www.youtube.com/playlist?list=PLcCVx7FV8Uxqj-wuw3LT6wehaZxhFRuwM",
|
||||
"https://www.youtube.com/playlist?list=PLcCVx7FV8UxoH89kRYe7XlK7SNox7zsIN",
|
||||
"https://www.youtube.com/playlist?list=PLcCVx7FV8UxriKA5suDcZeBoyuK7DFjck"
|
||||
};
|
||||
|
||||
// プレイリストの定期差分チェックを行うかどうか(true: 定期ポーリング、false: 1回完了後に待機)
|
||||
// プレイリストの定期差分チェックを行うかどうか(true: 定期ポーリング、false: 1回完了後に待機/終了)
|
||||
bool isPeriodicCheck = false;
|
||||
TimeSpan checkInterval = TimeSpan.FromMinutes(60);
|
||||
|
||||
@@ -62,16 +71,8 @@ LogManager.AddLog($"==================================================");
|
||||
LogManager.AddLog($" MusicDownloader (Poweramp Optimized) 起動");
|
||||
LogManager.AddLog($"==================================================");
|
||||
|
||||
// プレイリストURLの決定(環境変数優先)
|
||||
var playlistUrl = Environment.GetEnvironmentVariable("PLAYLIST_URL");
|
||||
if (string.IsNullOrWhiteSpace(playlistUrl))
|
||||
{
|
||||
playlistUrl = defaultPlaylistUrl;
|
||||
}
|
||||
|
||||
// 出力先ディレクトリの決定
|
||||
var outputDir = CmnDefine.GetOutputDir();
|
||||
LogManager.AddLog($"対象プレイリスト: {playlistUrl}");
|
||||
LogManager.AddLog($"ベース出力先: {outputDir}");
|
||||
|
||||
// 依存ツール(yt-dlp)の準備確認
|
||||
@@ -81,24 +82,102 @@ LogManager.AddLog($"使用 yt-dlp パス: {ytDlpPath}");
|
||||
|
||||
var downloader = new YoutubeDownloader(ytDlpPath, lyricsManager);
|
||||
|
||||
// =========================================================================
|
||||
// ダウンロード対象プレイリストの解決
|
||||
// 1. 環境変数 PLAYLIST_URL (カンマ/改行区切り)
|
||||
// 2. 出力フォルダ内の playlists.txt
|
||||
// 3. Program.cs で定義された defaultPlaylistUrls
|
||||
// =========================================================================
|
||||
List<string> ResolveTargetPlaylists(string baseDir)
|
||||
{
|
||||
var targets = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// 1. 環境変数からの取得
|
||||
var envUrls = Environment.GetEnvironmentVariable("PLAYLIST_URL");
|
||||
if (!string.IsNullOrWhiteSpace(envUrls))
|
||||
{
|
||||
var split = envUrls.Split(new[] { ',', ';', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var u in split)
|
||||
{
|
||||
var trimmed = u.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(trimmed)) targets.Add(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. playlists.txt からの取得(存在する場合)
|
||||
var playlistsFile = Path.Combine(baseDir, "playlists.txt");
|
||||
if (File.Exists(playlistsFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
var lines = File.ReadAllLines(playlistsFile);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(trimmed) && !trimmed.StartsWith("#"))
|
||||
{
|
||||
targets.Add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogManager.AddWarning($"playlists.txt の読み込み中にエラー: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 存在しない場合は初期設定のURL一覧を playlists.txt として保存(今後の追加編集用)
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(baseDir);
|
||||
File.WriteAllLines(playlistsFile, defaultPlaylistUrls);
|
||||
LogManager.AddLog($"playlists.txt を初期生成しました: {playlistsFile}");
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
// 3. 環境変数もファイルも空の場合は既定リストを採用
|
||||
if (targets.Count == 0)
|
||||
{
|
||||
foreach (var u in defaultPlaylistUrls) targets.Add(u);
|
||||
}
|
||||
|
||||
return targets.ToList();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// ダウンロード処理ループ
|
||||
// =========================================================================
|
||||
while (true)
|
||||
{
|
||||
var targetPlaylists = ResolveTargetPlaylists(outputDir);
|
||||
LogManager.AddLog($"対象プレイリスト総数: {targetPlaylists.Count} 件");
|
||||
|
||||
for (int p = 0; p < targetPlaylists.Count; p++)
|
||||
{
|
||||
var playlistUrl = targetPlaylists[p];
|
||||
LogManager.AddLog($"================================================================================");
|
||||
LogManager.AddLog($"[プレイリスト {p + 1}/{targetPlaylists.Count}] 処理開始: {playlistUrl}");
|
||||
LogManager.AddLog($"================================================================================");
|
||||
|
||||
try
|
||||
{
|
||||
LogManager.AddLog($"プレイリストのダウンロード処理を開始します...");
|
||||
await downloader.ProcessPlaylistAsync(playlistUrl, outputDir);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogManager.AddError($"処理実行中にエラーが発生しました: {ex.Message}", ex);
|
||||
LogManager.AddError($"プレイリスト処理中にエラーが発生しました: {ex.Message}", ex);
|
||||
}
|
||||
|
||||
// プレイリスト間で不要メモリを完全に返却
|
||||
GC.Collect(2, GCCollectionMode.Aggressive, true, true);
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
if (!isPeriodicCheck)
|
||||
{
|
||||
LogManager.AddLog($"ダウンロード処理が終了しました。プログラムを終了します。");
|
||||
LogManager.AddLog($"全プレイリストのダウンロード処理が完了しました。プログラムを終了します。");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+be6183f05e9d9fbda85b7d3b7e659fc9b11a1bc1")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ build_property.EnforceExtendedAnalyzerRules =
|
||||
build_property.EntryPointFilePath =
|
||||
build_property._SupportedPlatformList = Linux,macOS,Windows
|
||||
build_property.RootNamespace = MusicDownloader
|
||||
build_property.ProjectDir = C:\VS2026\MusicDownloader\MusicDownloader\
|
||||
build_property.ProjectDir = c:\VS2026\MusicDownloader\MusicDownloader\
|
||||
build_property.EnableComHosting =
|
||||
build_property.EnableGeneratedComInterfaceComImportInterop =
|
||||
build_property.EffectiveAnalysisLevelStyle = 10.0
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"C:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj": {}
|
||||
"c:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"C:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj": {
|
||||
"c:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "C:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj",
|
||||
"projectUniqueName": "c:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj",
|
||||
"projectName": "MusicDownloader",
|
||||
"projectPath": "C:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj",
|
||||
"projectPath": "c:\\VS2026\\MusicDownloader\\MusicDownloader\\MusicDownloader.csproj",
|
||||
"packagesPath": "C:\\Users\\uduki\\.nuget\\packages\\",
|
||||
"outputPath": "C:\\VS2026\\MusicDownloader\\MusicDownloader\\obj\\",
|
||||
"outputPath": "c:\\VS2026\\MusicDownloader\\MusicDownloader\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"fallbackFolders": [
|
||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages",
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+be6183f05e9d9fbda85b7d3b7e659fc9b11a1bc1")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("MusicDownloader")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
+5
-5
@@ -5,17 +5,17 @@ services:
|
||||
container_name: MusicDownloader
|
||||
# 完了後に自動停止する場合は "no"、定期チェック常駐させる場合は "unless-stopped"
|
||||
restart: "no"
|
||||
mem_limit: 1024m
|
||||
memswap_limit: 1024m
|
||||
mem_limit: 512m
|
||||
memswap_limit: 512m
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
memory: 1024M
|
||||
memory: 512M
|
||||
environment:
|
||||
- TZ=Asia/Tokyo
|
||||
# .NET GC設定: Workstation GC強制、ヒープ制限70%
|
||||
# .NET GC設定: Workstation GC強制、ヒープ制限50%で省メモリ化
|
||||
- DOTNET_gcServer=0
|
||||
- DOTNET_GCHeapHardLimitPercent=70
|
||||
- DOTNET_GCHeapHardLimitPercent=50
|
||||
# プレイリストURLをコンテナ側から上書きしたい場合に指定
|
||||
# - PLAYLIST_URL=https://www.youtube.com/playlist?list=...
|
||||
volumes:
|
||||
|
||||
Reference in new issue
Block a user