diff --git a/MusicDownloader/Class/Downloader/RunConfig.cs b/MusicDownloader/Class/Downloader/RunConfig.cs
new file mode 100644
index 0000000..3d07fa8
--- /dev/null
+++ b/MusicDownloader/Class/Downloader/RunConfig.cs
@@ -0,0 +1,110 @@
+using System;
+using System.IO;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace MusicDownloader.Class.Downloader
+{
+ ///
+ /// Docker公開時・起動時の処理モード(最初から / 続きから)を制御する設定クラス
+ ///
+ public class RunConfig
+ {
+ ///
+ /// 初回起動時の動作モード: "resume" (続きから / 差分) または "from_scratch" (最初から / 全曲)
+ ///
+ [JsonPropertyName("initial_mode")]
+ public string InitialMode { get; set; } = "resume";
+
+ ///
+ /// 読み込み元の設定ファイルパス
+ ///
+ [JsonIgnore]
+ public string? LoadedPath { get; set; }
+
+ ///
+ /// 初回処理を続きから行う(差分ダウンロード)かどうかを判定する
+ ///
+ /// true: 続きから(差分), false: 最初から(全曲再取得)
+ 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;
+ }
+
+ ///
+ /// 設定ファイル(run_config.json)を探索して読み込む
+ ///
+ /// ベース出力先ディレクトリ
+ /// 読み込まれた設定オブジェクト
+ public static RunConfig Load(string? outputDir = null)
+ {
+ // 探索対象のパス候補
+ var candidates = new List();
+
+ // 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(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 };
+ }
+ }
+}
diff --git a/MusicDownloader/MusicDownloader.csproj b/MusicDownloader/MusicDownloader.csproj
index 9b67175..f2f002b 100644
--- a/MusicDownloader/MusicDownloader.csproj
+++ b/MusicDownloader/MusicDownloader.csproj
@@ -33,8 +33,8 @@
-
-
+
+
diff --git a/MusicDownloader/Program.cs b/MusicDownloader/Program.cs
index 1767f4e..9397e87 100644
--- a/MusicDownloader/Program.cs
+++ b/MusicDownloader/Program.cs
@@ -194,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(StringComparer.OrdinalIgnoreCase);
@@ -205,12 +217,19 @@ while (true)
LogManager.AddLog($"[第 {loopCount} 回実行] 対象プレイリスト総数: {targetPlaylists.Count} 件のチェックを開始します (モード: {(isIncremental ? "差分ダウンロード" : "全曲ダウンロード")})");
LogManager.AddLog($"================================================================================");
- // 初回起動時(コンテナ再起動含む)はダウンロード完了状態(archive.txt)をリセットして全曲ダウンロード
+ // 初回起動時の初期化処理
if (isFirstRun)
{
- LogManager.AddLog($"[起動時初期化] 起動・再起動時のため、ダウンロード完了履歴 (archive.txt) をリセットして全曲ダウンロードを行います。");
- var youtubeFolder = Path.Combine(outputDir, CmnDefine.YoutubeFolderName);
- downloader.ResetDownloadArchive(youtubeFolder);
+ 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++)
diff --git a/compose.yaml b/compose.yaml
index f450089..75ab36f 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -28,6 +28,8 @@ services:
- 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
diff --git a/run_config.json b/run_config.json
new file mode 100644
index 0000000..43f5e6b
--- /dev/null
+++ b/run_config.json
@@ -0,0 +1,5 @@
+{
+ "// 説明": "Docker公開時・起動時の初回処理モード設定(最初から行う: 'from_scratch' / 続きから行う: 'resume')",
+ "// 選択肢": "'resume' (続きから / 差分ダウンロード - 既存のarchive.txtを保持) または 'from_scratch' (最初から / 全曲再ダウンロード - 起動時にarchive.txtをリセット)",
+ "initial_mode": "resume"
+}