Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be66964981 | ||
|
|
3c2fffdb68 |
No files matched your search
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace MusicDownloader.Class.Downloader
|
||||
{
|
||||
/// <summary>
|
||||
/// YouTubeの認証・年齢制限解除用Cookie(cookies.txt)やブラウザCookieの検出・引数付与を行うヘルパークラス
|
||||
/// </summary>
|
||||
public static class CookieHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 利用可能な cookies.txt ファイルの絶対パスを探索して取得する
|
||||
/// </summary>
|
||||
/// <param name="outputBaseDir">音楽出力ベースディレクトリ(例: C:\MyMusic や /music)</param>
|
||||
/// <param name="youtubeFolder">楽曲集約ディレクトリ(例: C:\MyMusic\Youtube や /music/Youtube)</param>
|
||||
/// <returns>見つかったCookieファイルの絶対パス、存在しない場合は null</returns>
|
||||
public static string? FindCookiesFile(string? outputBaseDir = null, string? youtubeFolder = null)
|
||||
{
|
||||
// 1. 環境変数 YOUTUBE_COOKIES_PATH または COOKIES_PATH
|
||||
var envPath = Environment.GetEnvironmentVariable("YOUTUBE_COOKIES_PATH")
|
||||
?? Environment.GetEnvironmentVariable("COOKIES_PATH");
|
||||
if (!string.IsNullOrWhiteSpace(envPath) && File.Exists(envPath))
|
||||
{
|
||||
return Path.GetFullPath(envPath);
|
||||
}
|
||||
|
||||
// 2. 出力ベースディレクトリ直下(ホスト C:\MyMusic\cookies.txt ➔ コンテナ /music/cookies.txt)
|
||||
if (!string.IsNullOrWhiteSpace(outputBaseDir))
|
||||
{
|
||||
var p = Path.Combine(outputBaseDir, "cookies.txt");
|
||||
if (File.Exists(p)) return Path.GetFullPath(p);
|
||||
}
|
||||
|
||||
// 3. YouTube集約フォルダ直下(C:\MyMusic\Youtube\cookies.txt ➔ コンテナ /music/Youtube/cookies.txt)
|
||||
if (!string.IsNullOrWhiteSpace(youtubeFolder))
|
||||
{
|
||||
var p = Path.Combine(youtubeFolder, "cookies.txt");
|
||||
if (File.Exists(p)) return Path.GetFullPath(p);
|
||||
}
|
||||
|
||||
// 4. アプリケーション実行フォルダ直下(ローカル bin 配下またはコンテナ /app/cookies.txt)
|
||||
var appDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
var appCookie = Path.Combine(appDir, "cookies.txt");
|
||||
if (File.Exists(appCookie)) return Path.GetFullPath(appCookie);
|
||||
|
||||
// 5. Docker等で一般的にマウントされる /cookies/cookies.txt
|
||||
if (File.Exists("/cookies/cookies.txt")) return "/cookies/cookies.txt";
|
||||
|
||||
// 6. ログ出力先ボリューム(ホスト C:\feeds\cookies.txt ➔ コンテナ /feeds/cookies.txt)
|
||||
try
|
||||
{
|
||||
var feedsCookie = Path.Combine(CmnDefine.GetLogDir(), "cookies.txt");
|
||||
if (File.Exists(feedsCookie)) return Path.GetFullPath(feedsCookie);
|
||||
}
|
||||
catch { }
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ローカル実行時にブラウザから直接Cookieを取得するための指定名(chrome, firefox, edge等)を取得する
|
||||
/// </summary>
|
||||
public static string? GetCookiesBrowser()
|
||||
{
|
||||
var envBrowser = Environment.GetEnvironmentVariable("YOUTUBE_COOKIES_BROWSER")
|
||||
?? Environment.GetEnvironmentVariable("COOKIES_BROWSER");
|
||||
return string.IsNullOrWhiteSpace(envBrowser) ? null : envBrowser.Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// yt-dlp のコマンド引数リストに Cookie オプション(--cookies または --cookies-from-browser)を付与する
|
||||
/// </summary>
|
||||
/// <param name="args">付与対象の引数リスト</param>
|
||||
/// <param name="outputBaseDir">音楽出力ベースディレクトリ</param>
|
||||
/// <param name="youtubeFolder">楽曲集約ディレクトリ</param>
|
||||
/// <returns>Cookieオプションが付与されたかどうか</returns>
|
||||
public static bool ApplyCookieArgs(List<string> args, string? outputBaseDir = null, string? youtubeFolder = null)
|
||||
{
|
||||
var cookieFile = FindCookiesFile(outputBaseDir, youtubeFolder);
|
||||
if (!string.IsNullOrEmpty(cookieFile))
|
||||
{
|
||||
args.Add("--cookies");
|
||||
args.Add(cookieFile);
|
||||
return true;
|
||||
}
|
||||
|
||||
var browser = GetCookiesBrowser();
|
||||
if (!string.IsNullOrEmpty(browser))
|
||||
{
|
||||
args.Add("--cookies-from-browser");
|
||||
args.Add(browser);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 現在のCookie検出状況を説明する文字列を取得する(ログ出力・案内用)
|
||||
/// </summary>
|
||||
public static (bool HasCookie, string Description) GetCookieStatus(string? outputBaseDir = null, string? youtubeFolder = null)
|
||||
{
|
||||
var cookieFile = FindCookiesFile(outputBaseDir, youtubeFolder);
|
||||
if (!string.IsNullOrEmpty(cookieFile))
|
||||
{
|
||||
return (true, $"Cookieファイル検出: {cookieFile}");
|
||||
}
|
||||
|
||||
var browser = GetCookiesBrowser();
|
||||
if (!string.IsNullOrEmpty(browser))
|
||||
{
|
||||
return (true, $"ブラウザCookie指定検出: {browser}");
|
||||
}
|
||||
|
||||
var suggestedPath = !string.IsNullOrWhiteSpace(outputBaseDir)
|
||||
? Path.Combine(outputBaseDir, "cookies.txt")
|
||||
: "C:\\MyMusic\\cookies.txt または /app/cookies.txt";
|
||||
|
||||
return (false, $"未配置 (年齢制限動画を取得する場合は {suggestedPath} に cookies.txt を配置してください)");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MusicDownloader.Class.Downloader
|
||||
{
|
||||
/// <summary>
|
||||
/// Docker公開時・起動時の処理モード(最初から / 続きから)を制御する設定クラス
|
||||
/// </summary>
|
||||
public class RunConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 初回起動時の動作モード: "resume" (続きから / 差分) または "from_scratch" (最初から / 全曲)
|
||||
/// </summary>
|
||||
[JsonPropertyName("initial_mode")]
|
||||
public string InitialMode { get; set; } = "resume";
|
||||
|
||||
/// <summary>
|
||||
/// 読み込み元の設定ファイルパス
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string? LoadedPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 初回処理を続きから行う(差分ダウンロード)かどうかを判定する
|
||||
/// </summary>
|
||||
/// <returns>true: 続きから(差分), false: 最初から(全曲再取得)</returns>
|
||||
public bool IsResumeMode()
|
||||
{
|
||||
// 環境変数 INITIAL_DOWNLOAD_MODE による上書きチェック
|
||||
var envMode = Environment.GetEnvironmentVariable("INITIAL_DOWNLOAD_MODE");
|
||||
var modeToCheck = !string.IsNullOrWhiteSpace(envMode) ? envMode : InitialMode;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(modeToCheck))
|
||||
{
|
||||
return true; // デフォルトは続きから
|
||||
}
|
||||
|
||||
var val = modeToCheck.Trim().ToLowerInvariant();
|
||||
if (val is "from_scratch" or "scratch" or "reset" or "full" or "最初から" or "全曲" or "false")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 設定ファイル(run_config.json)を探索して読み込む
|
||||
/// </summary>
|
||||
/// <param name="outputDir">ベース出力先ディレクトリ</param>
|
||||
/// <returns>読み込まれた設定オブジェクト</returns>
|
||||
public static RunConfig Load(string? outputDir = null)
|
||||
{
|
||||
// 探索対象のパス候補
|
||||
var candidates = new List<string>();
|
||||
|
||||
// 1. 環境変数による指定パス
|
||||
var envPath = Environment.GetEnvironmentVariable("RUN_CONFIG_PATH");
|
||||
if (!string.IsNullOrWhiteSpace(envPath)) candidates.Add(envPath);
|
||||
|
||||
// 2. コンテナ内の /app/run_config.json または実行フォルダ直下
|
||||
var appDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
candidates.Add(Path.Combine(appDir, "run_config.json"));
|
||||
|
||||
// 3. /config/run_config.json
|
||||
candidates.Add("/config/run_config.json");
|
||||
|
||||
// 4. 出力フォルダ配下(/music/run_config.json 等)
|
||||
if (!string.IsNullOrWhiteSpace(outputDir))
|
||||
{
|
||||
candidates.Add(Path.Combine(outputDir, "run_config.json"));
|
||||
}
|
||||
|
||||
// 5. ホスト開発環境の既定パス(C:\VS2026\MusicDownloader\run_config.json)
|
||||
candidates.Add(@"C:\VS2026\MusicDownloader\run_config.json");
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
var json = File.ReadAllText(candidate);
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
AllowTrailingCommas = true
|
||||
};
|
||||
var config = JsonSerializer.Deserialize<RunConfig>(json, options);
|
||||
if (config != null)
|
||||
{
|
||||
config.LoadedPath = candidate;
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogManager.AddWarning($"設定ファイル '{candidate}' の読み込み中に例外: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 見つからなかった場合はデフォルト(続きから)
|
||||
return new RunConfig { InitialMode = "resume", LoadedPath = null };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,14 +145,19 @@ namespace MusicDownloader.Class.Downloader
|
||||
/// <summary>
|
||||
/// プレイリストURLからプレイリスト名および動画リストを取得する
|
||||
/// </summary>
|
||||
public async Task<YtPlaylistInfo?> GetPlaylistInfoAsync(string playlistUrl)
|
||||
public async Task<YtPlaylistInfo?> GetPlaylistInfoAsync(string playlistUrl, string? outputBaseDir = null)
|
||||
{
|
||||
LogManager.AddLog($"プレイリスト情報を取得中: {playlistUrl}");
|
||||
|
||||
try
|
||||
{
|
||||
var args = new List<string> { "--dump-single-json", "--flat-playlist", "--no-warnings" };
|
||||
// Cookieファイルが存在する場合は付与(年齢制限や限定公開動画のメタデータ取得用)
|
||||
CookieHelper.ApplyCookieArgs(args, outputBaseDir);
|
||||
args.Add(playlistUrl);
|
||||
|
||||
var result = await Cli.Wrap(_ytDlpPath)
|
||||
.WithArguments(new[] { "--dump-single-json", "--flat-playlist", "--no-warnings", playlistUrl })
|
||||
.WithArguments(args)
|
||||
.WithValidation(CommandResultValidation.None)
|
||||
.ExecuteBufferedAsync();
|
||||
|
||||
@@ -209,7 +214,7 @@ namespace MusicDownloader.Class.Downloader
|
||||
/// <param name="isIncremental">差分ダウンロードを行うかどうか (falseの場合は起動直後として全曲をダウンロード・最新化)</param>
|
||||
public async Task ProcessPlaylistAsync(string playlistUrl, string outputBaseDir, bool isIncremental = true)
|
||||
{
|
||||
var playlistInfo = await GetPlaylistInfoAsync(playlistUrl);
|
||||
var playlistInfo = await GetPlaylistInfoAsync(playlistUrl, outputBaseDir);
|
||||
var rawTitle = playlistInfo?.Title ?? "YouTube_Playlist";
|
||||
var sanitizedPlaylistName = CmnUtil.SanitizeFileName(rawTitle);
|
||||
|
||||
@@ -268,7 +273,7 @@ namespace MusicDownloader.Class.Downloader
|
||||
|
||||
// 単一動画URLまたはエントリがフラット取得できなかった場合、直接ダウンロード
|
||||
LogManager.AddLog($"プレイリストのエントリが見つからないか単一動画URLです。直接ダウンロードを試行します。");
|
||||
var singleTrack = await DownloadAndProcessSingleAsync(playlistUrl, 1, 1, stagingDir, youtubeFolder, sanitizedPlaylistName, null, downloadedTracks);
|
||||
var singleTrack = await DownloadAndProcessSingleAsync(playlistUrl, 1, 1, stagingDir, youtubeFolder, sanitizedPlaylistName, null, downloadedTracks, outputBaseDir);
|
||||
if (singleTrack != null)
|
||||
{
|
||||
currentPlaylistTracks.Add(singleTrack);
|
||||
@@ -310,7 +315,7 @@ namespace MusicDownloader.Class.Downloader
|
||||
|
||||
// 1曲の処理(ローカル作業フォルダでダウンロード・タグ付け・検証を完結させ、完成後にYoutubeフォルダへ転送)
|
||||
var trackInfo = await DownloadAndProcessSingleAsync(
|
||||
videoUrl, videoIndex, totalCount, stagingDir, youtubeFolder, sanitizedPlaylistName, entry, downloadedTracks);
|
||||
videoUrl, videoIndex, totalCount, stagingDir, youtubeFolder, sanitizedPlaylistName, entry, downloadedTracks, outputBaseDir);
|
||||
|
||||
if (trackInfo != null)
|
||||
{
|
||||
@@ -356,13 +361,14 @@ namespace MusicDownloader.Class.Downloader
|
||||
string youtubeFolder,
|
||||
string playlistName,
|
||||
YtPlaylistEntry? entryInfo,
|
||||
Dictionary<string, DownloadedTrackInfo> downloadedTracks)
|
||||
Dictionary<string, DownloadedTrackInfo> downloadedTracks,
|
||||
string? outputBaseDir = null)
|
||||
{
|
||||
var videoId = entryInfo?.Id ?? "";
|
||||
var archiveFile = Path.Combine(youtubeFolder, "archive.txt");
|
||||
|
||||
// ② 多段階フォールバックによるダウンロード実行(429字幕エラーや403Forbiddenエラーを自動回避)
|
||||
var isDownloadSuccess = await DownloadWithFallbackAsync(videoUrl, videoId, stagingDir, archiveFile);
|
||||
// ② 多段階フォールバックによるダウンロード実行(429字幕エラー、403Forbiddenエラー、年齢制限エラーを自動回避)
|
||||
var isDownloadSuccess = await DownloadWithFallbackAsync(videoUrl, videoId, stagingDir, archiveFile, outputBaseDir, youtubeFolder, entryInfo?.Title);
|
||||
if (!isDownloadSuccess)
|
||||
{
|
||||
LogManager.AddWarning($"[{index}/{totalCount}] すべてのフォールバックを試みましたがダウンロードできませんでした。");
|
||||
@@ -623,19 +629,25 @@ namespace MusicDownloader.Class.Downloader
|
||||
|
||||
/// <summary>
|
||||
/// ② 多段階フォールバックにより動画の音声を確実にダウンロードする
|
||||
/// - Cookie自動適用: cookies.txt による年齢制限解除・高画質取得
|
||||
/// - 429 Too Many Requests: 字幕ダウンロードを無効化して音声のみ取得
|
||||
/// - 403 Forbidden: YouTube AndroidクライアントAPIやiOSクライアントAPIへの切り替え
|
||||
/// - 403 Forbidden / 年齢制限: YouTube Android / Creator / TV / iOS クライアントAPIへの自動切り替え
|
||||
/// </summary>
|
||||
private async Task<bool> DownloadWithFallbackAsync(
|
||||
string videoUrl,
|
||||
string videoId,
|
||||
string stagingDir,
|
||||
string archiveFile)
|
||||
string archiveFile,
|
||||
string? outputBaseDir = null,
|
||||
string? youtubeFolder = null,
|
||||
string? videoTitle = null)
|
||||
{
|
||||
var outputTemplate = Path.Combine(stagingDir, "%(title)s.%(ext)s");
|
||||
|
||||
// 基本引数の生成
|
||||
List<string> GetBaseArgs() => new List<string>
|
||||
List<string> GetBaseArgs()
|
||||
{
|
||||
var baseArgs = new List<string>
|
||||
{
|
||||
"-f", "bestaudio/best",
|
||||
"--extract-audio",
|
||||
@@ -655,6 +667,12 @@ namespace MusicDownloader.Class.Downloader
|
||||
"--no-warnings"
|
||||
};
|
||||
|
||||
// CookieファイルまたはブラウザCookieの自動適用(年齢確認・制限解除用)
|
||||
CookieHelper.ApplyCookieArgs(baseArgs, outputBaseDir, youtubeFolder);
|
||||
|
||||
return baseArgs;
|
||||
}
|
||||
|
||||
// 試行1: 標準(429を避けるため英語en字幕は除外してja字幕のみ)
|
||||
var plan1 = GetBaseArgs();
|
||||
plan1.AddRange(new[] { "--write-subs", "--write-auto-subs", "--sub-lang", "ja", "--sub-format", "vtt", videoUrl });
|
||||
@@ -663,22 +681,34 @@ namespace MusicDownloader.Class.Downloader
|
||||
var plan2 = GetBaseArgs();
|
||||
plan2.Add(videoUrl);
|
||||
|
||||
// 試行3: AndroidクライアントAPI(403 Forbidden ストリーム拒否対策)
|
||||
// 試行3: AndroidクライアントAPI(403 Forbidden ストリーム拒否対策・一部年齢制限対策)
|
||||
var plan3 = GetBaseArgs();
|
||||
plan3.AddRange(new[] { "--extractor-args", "youtube:player_client=android,web", videoUrl });
|
||||
|
||||
// 試行4: iOSクライアントAPI & キャッシュ無効化(最終フォールバック)
|
||||
// 試行4: Android Creator & Mobile Webクライアント(年齢確認バイパス試行)
|
||||
var plan4 = GetBaseArgs();
|
||||
plan4.AddRange(new[] { "--extractor-args", "youtube:player_client=ios,web", "--no-cache-dir", videoUrl });
|
||||
plan4.AddRange(new[] { "--extractor-args", "youtube:player_client=android_creator,mweb", videoUrl });
|
||||
|
||||
// 試行5: TVクライアントAPI(年齢確認バイパス試行)
|
||||
var plan5 = GetBaseArgs();
|
||||
plan5.AddRange(new[] { "--extractor-args", "youtube:player_client=tv_embedded,tv", videoUrl });
|
||||
|
||||
// 試行6: iOSクライアントAPI & キャッシュ無効化(最終フォールバック)
|
||||
var plan6 = GetBaseArgs();
|
||||
plan6.AddRange(new[] { "--extractor-args", "youtube:player_client=ios,web", "--no-cache-dir", videoUrl });
|
||||
|
||||
var plans = new (string Name, List<string> Args)[]
|
||||
{
|
||||
("通常ダウンロード (日本語字幕あり)", plan1),
|
||||
("フォールバック1 (字幕取得オフ / 429対策)", plan2),
|
||||
("フォールバック2 (YouTube Androidクライアント / 403対策)", plan3),
|
||||
("フォールバック3 (YouTube iOSクライアント & キャッシュ無効化)", plan4)
|
||||
("フォールバック2 (YouTube Androidクライアント / 403・年齢確認対策)", plan3),
|
||||
("フォールバック3 (YouTube Creator・mwebクライアント / 年齢確認対策)", plan4),
|
||||
("フォールバック4 (YouTube TVクライアント / 年齢確認対策)", plan5),
|
||||
("フォールバック5 (YouTube iOSクライアント & キャッシュ無効化)", plan6)
|
||||
};
|
||||
|
||||
bool detectedAgeRestriction = false;
|
||||
|
||||
for (int i = 0; i < plans.Length; i++)
|
||||
{
|
||||
var (name, args) = plans[i];
|
||||
@@ -710,6 +740,11 @@ namespace MusicDownloader.Class.Downloader
|
||||
LogManager.AddWarning($"[{name}] 終了コード: {cmdResult.ExitCode}");
|
||||
if (!string.IsNullOrWhiteSpace(stdErr))
|
||||
{
|
||||
if (stdErr.Contains("confirm your age", StringComparison.OrdinalIgnoreCase) ||
|
||||
stdErr.Contains("Sign in to confirm", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
detectedAgeRestriction = true;
|
||||
}
|
||||
LogManager.AddWarning($"詳細: {stdErr.Trim()}");
|
||||
}
|
||||
|
||||
@@ -726,6 +761,13 @@ namespace MusicDownloader.Class.Downloader
|
||||
}
|
||||
}
|
||||
|
||||
if (detectedAgeRestriction)
|
||||
{
|
||||
var (_, cookieDesc) = CookieHelper.GetCookieStatus(outputBaseDir, youtubeFolder);
|
||||
LogManager.AddWarning($"[年齢制限エラー] 動画「{videoTitle ?? videoId}」はYouTubeの年齢確認が必要です。");
|
||||
LogManager.AddWarning($"[Cookie設定状況] {cookieDesc}");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
<Exec Command="powershell -Command "if (!(Test-Path 'C:\docker\MusicDownloader\app')) { New-Item -ItemType Directory -Force -Path 'C:\docker\MusicDownloader\app' }"" />
|
||||
<!-- 発行済みファイルを C:\docker\MusicDownloader\app に同期(Visual Studio発行プロファイル・CLI共通対応) -->
|
||||
<Exec Command="robocopy "$(PublishDir)." "C:\docker\MusicDownloader\app" /MIR /NP /NFL /NDL" IgnoreExitCode="true" />
|
||||
<!-- Dockerfile と compose.yaml を最新同期 -->
|
||||
<Exec Command="powershell -Command "Copy-Item -Path '$(SolutionDir)Dockerfile', '$(SolutionDir)compose.yaml' -Destination 'C:\docker\MusicDownloader\' -Force"" />
|
||||
<!-- Dockerfile と compose.yaml と run_config.json を最新同期 -->
|
||||
<Exec Command="powershell -Command "Copy-Item -Path '$(SolutionDir)Dockerfile', '$(SolutionDir)compose.yaml', '$(SolutionDir)run_config.json' -Destination 'C:\docker\MusicDownloader\' -Force"" />
|
||||
<!-- ローカルDocker Desktopでコンテナをビルド・起動(常駐) -->
|
||||
<Exec Command="docker compose -f C:\docker\MusicDownloader\compose.yaml up -d --build" />
|
||||
<Message Text="=== [Local Docker Deploy] All processes completed successfully! ===" Importance="high" />
|
||||
|
||||
@@ -100,10 +100,22 @@ LogManager.AddLog($"==================================================");
|
||||
// 出力先およびログ保存先ディレクトリの決定
|
||||
var outputDir = CmnDefine.GetOutputDir();
|
||||
var logDir = CmnDefine.GetLogDir();
|
||||
var youtubeDir = Path.Combine(outputDir, CmnDefine.YoutubeFolderName);
|
||||
LogManager.AddLog($"ベース出力先: {outputDir}");
|
||||
LogManager.AddLog($"楽曲集約フォルダ: {Path.Combine(outputDir, CmnDefine.YoutubeFolderName)}");
|
||||
LogManager.AddLog($"楽曲集約フォルダ: {youtubeDir}");
|
||||
LogManager.AddLog($"ログ保存先: {logDir}");
|
||||
|
||||
// YouTube認証・年齢制限解除用Cookie(cookies.txt)の確認
|
||||
var (hasCookie, cookieStatusDesc) = CookieHelper.GetCookieStatus(outputDir, youtubeDir);
|
||||
if (hasCookie)
|
||||
{
|
||||
LogManager.AddSuccess($"[Cookie設定] {cookieStatusDesc}");
|
||||
}
|
||||
else
|
||||
{
|
||||
LogManager.AddLog($"[Cookie設定] {cookieStatusDesc}");
|
||||
}
|
||||
|
||||
// 依存ツール(yt-dlp)の準備確認
|
||||
using var initClient = httpClientFactory.CreateClient("webClient");
|
||||
var ytDlpPath = await DependencyManager.EnsureYtDlpAsync(initClient);
|
||||
@@ -182,10 +194,22 @@ int loopCount = 0;
|
||||
bool isFirstRun = true;
|
||||
var playlistsFile = Path.Combine(outputDir, "playlists.txt");
|
||||
|
||||
// 【設定読み込み】起動時・Docker公開時の初回処理モード(最初から / 続きから)
|
||||
var runConfig = RunConfig.Load(outputDir);
|
||||
bool isResumeOnStartup = runConfig.IsResumeMode();
|
||||
|
||||
if (!string.IsNullOrEmpty(runConfig.LoadedPath))
|
||||
{
|
||||
LogManager.AddLog($"[起動設定検出] 設定ファイル: {runConfig.LoadedPath}");
|
||||
}
|
||||
LogManager.AddLog($"[起動時モード判定] 初回処理: {(isResumeOnStartup ? "「続きから行う (差分ダウンロード)」" : "「最初から行う (全曲再ダウンロード)」")}");
|
||||
|
||||
while (true)
|
||||
{
|
||||
loopCount++;
|
||||
bool isIncremental = !isFirstRun;
|
||||
// 初回実行時: 設定ファイルが resume なら差分ダウンロード (true)、from_scratch なら全曲ダウンロード (false)
|
||||
// 2回目以降 (loopCount > 1): 常に差分ダウンロード (true)
|
||||
bool isIncremental = isFirstRun ? isResumeOnStartup : true;
|
||||
var targetPlaylists = ResolveTargetPlaylists(outputDir);
|
||||
var processedInCycle = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@@ -193,13 +217,20 @@ while (true)
|
||||
LogManager.AddLog($"[第 {loopCount} 回実行] 対象プレイリスト総数: {targetPlaylists.Count} 件のチェックを開始します (モード: {(isIncremental ? "差分ダウンロード" : "全曲ダウンロード")})");
|
||||
LogManager.AddLog($"================================================================================");
|
||||
|
||||
// 初回起動時(コンテナ再起動含む)はダウンロード完了状態(archive.txt)をリセットして全曲ダウンロード
|
||||
// 初回起動時の初期化処理
|
||||
if (isFirstRun)
|
||||
{
|
||||
LogManager.AddLog($"[起動時初期化] 起動・再起動時のため、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。");
|
||||
if (isResumeOnStartup)
|
||||
{
|
||||
LogManager.AddSuccess($"[起動時モード: 続きから行う] 設定に基づき、既存のダウンロード履歴 (archive.txt) を保持して続きから差分取得します。");
|
||||
}
|
||||
else
|
||||
{
|
||||
LogManager.AddLog($"[起動時モード: 最初から行う] 設定に基づき、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。");
|
||||
var youtubeFolder = Path.Combine(outputDir, CmnDefine.YoutubeFolderName);
|
||||
downloader.ResetDownloadArchive(youtubeFolder);
|
||||
}
|
||||
}
|
||||
|
||||
for (int p = 0; p < targetPlaylists.Count; p++)
|
||||
{
|
||||
|
||||
@@ -20,11 +20,16 @@ services:
|
||||
- CHECK_INTERVAL_HOURS=2
|
||||
# プレイリストURLをコンテナ側から上書きしたい場合に指定
|
||||
# - PLAYLIST_URL=https://www.youtube.com/playlist?list=...
|
||||
# 年齢制限動画取得用のCookieファイルパスを明示指定する場合(未指定でも /music/cookies.txt 等から自動検出)
|
||||
# - YOUTUBE_COOKIES_PATH=/music/cookies.txt
|
||||
volumes:
|
||||
# YouTubeからダウンロードした音楽ファイルをホストの G:\マイドライブ\MyMusic に出力(ジャンクション C:\MyMusic 経由)
|
||||
# ※ 年齢制限動画を取得する場合、ホスト側の C:\MyMusic\cookies.txt に配置すればコンテナ内 (/music/cookies.txt) で自動検知されます
|
||||
- C:\MyMusic:/music
|
||||
# ホストのログディレクトリ C:\feeds\Log へ出力するためのマウント
|
||||
- C:\feeds:/feeds
|
||||
# 初回起動時の動作設定(最初から行う: from_scratch / 続きから行う: resume)
|
||||
- C:\VS2026\MusicDownloader\run_config.json:/app/run_config.json:ro
|
||||
networks:
|
||||
- common-network
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"// 説明": "Docker公開時・起動時の初回処理モード設定(最初から行う: 'from_scratch' / 続きから行う: 'resume')",
|
||||
"// 選択肢": "'resume' (続きから / 差分ダウンロード - 既存のarchive.txtを保持) または 'from_scratch' (最初から / 全曲再ダウンロード - 起動時にarchive.txtをリセット)",
|
||||
"initial_mode": "resume"
|
||||
}
|
||||
Reference in new issue
Block a user