{
  "name": "音声合成advanced_v3_fix7",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -3920,
        80
      ],
      "id": "55c19059-0b46-4230-b85b-e400da29e840",
      "name": "Manual Trigger"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "n8n-test"
        },
        "sheetName": {
          "__rl": true,
          "value": YOUR_CANVA_A_GID,
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -3680,
        80
      ],
      "id": "b8725851-42c0-4f64-8980-264bac8346a5",
      "name": "Get Sheet A",
      "alwaysOutputData": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "n8n-test"
        },
        "sheetName": {
          "__rl": true,
          "value": YOUR_CANVA_B_GID,
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -3440,
        80
      ],
      "id": "b834cddb-039a-49cf-a328-855b2527b7d5",
      "name": "Get Sheet B",
      "alwaysOutputData": true,
      "executeOnce": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "n8n-test"
        },
        "sheetName": {
          "__rl": true,
          "value": YOUR_CANVA_C_GID,
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -3200,
        80
      ],
      "id": "4473c3e4-7d75-438b-8865-a75342b91165",
      "name": "Get Sheet C",
      "alwaysOutputData": true,
      "executeOnce": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "n8n-test"
        },
        "sheetName": {
          "__rl": true,
          "value": YOUR_CANVA_D_GID,
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -2960,
        80
      ],
      "id": "8d99b1b6-cb6e-4845-8702-23e1cd0eac1b",
      "name": "Get Sheet D",
      "alwaysOutputData": true,
      "executeOnce": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "n8n-test"
        },
        "sheetName": {
          "__rl": true,
          "value": YOUR_CANVA_E_GID,
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -2720,
        80
      ],
      "id": "995577b2-1f30-4820-91a0-a496a8af74d8",
      "name": "Get Sheet E",
      "alwaysOutputData": true,
      "executeOnce": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// 全シートのデータを取得してカテゴリを付与\nconst sheetA = $('Get Sheet A').all().map(item => ({ json: { ...item.json, category: 'A' } }));\nconst sheetB = $('Get Sheet B').all().map(item => ({ json: { ...item.json, category: 'B' } }));\nconst sheetC = $('Get Sheet C').all().map(item => ({ json: { ...item.json, category: 'C' } }));\nconst sheetD = $('Get Sheet D').all().map(item => ({ json: { ...item.json, category: 'D' } }));\nconst sheetE = $('Get Sheet E').all().map(item => ({ json: { ...item.json, category: 'E' } }));\n\n// 全データを結合\nconst allData = [...sheetA, ...sheetB, ...sheetC, ...sheetD, ...sheetE];\n\n// audio_status === 'NORMAL' でフィルタ\nconst filtered = allData.filter(item => item.json.audio_status === 'NORMAL');\n\nif (filtered.length === 0) {\n  return [{ json: { _empty: true, message: 'No items to process' } }];\n}\n\n// 年月日を生成\nconst now = new Date();\nconst yearMonthDay = now.getFullYear().toString() + \n  String(now.getMonth() + 1).padStart(2, '0') + \n  String(now.getDate()).padStart(2, '0');\n\n// 各アイテムにフォルダ名を付与\nreturn filtered.map(item => ({\n  json: {\n    ...item.json,\n    folder_name: `${yearMonthDay}投稿${item.json.category}`\n  }\n}));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -2480,
        80
      ],
      "id": "fd8ac732-b699-4826-abff-2ea54a4a5d4b",
      "name": "Merge All and Filter",
      "executeOnce": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "has-post-id",
              "leftValue": "={{ $json.post_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -2240,
        80
      ],
      "id": "fe34b09b-596b-4711-a4a4-e36704d13594",
      "name": "If Has Items"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -2000,
        80
      ],
      "id": "f8657e56-af8d-4305-947f-56a469abe7a2",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $json.folder_name }}",
        "filter": {
          "folderId": {
            "__rl": true,
            "value": "YOUR_PARENT_FOLDER_ID",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1760,
        80
      ],
      "id": "88bade6b-9623-4c50-ab5f-e923ca9c9817",
      "name": "Search Category Folder",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Loop Over Itemsから現在のアイテムを取得\nconst currentItem = $('Loop Over Items').item.json;\n\n// Search Category Folderからフォルダを検索\nconst folders = $input.all();\nconst categoryFolder = folders.find(f => f.json.name === currentItem.folder_name);\nconst categoryFolderId = categoryFolder ? categoryFolder.json.id : null;\n\nreturn [{\n  json: {\n    ...currentItem,\n    category_folder_id: categoryFolderId\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1520,
        80
      ],
      "id": "166757fb-c593-4d4f-9e90-da37c6360249",
      "name": "Prepare Item"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $json.post_id }}.mp4",
        "filter": {
          "folderId": {
            "__rl": true,
            "value": "={{ $json.category_folder_id }}",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1280,
        80
      ],
      "id": "f8b01640-3dc5-420f-8489-0e4a49c71c71",
      "name": "Search Video File",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "video-exists",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -1040,
        80
      ],
      "id": "3152bd71-55fe-471f-a26c-32b898b3685b",
      "name": "If Video Exists"
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Search Video File').item.json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -800,
        -16
      ],
      "id": "22a72351-6194-4b0f-a6e4-4c4d7ee7658c",
      "name": "Download Video",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Prepare Itemから現在のアイテムを取得\nconst item = $('Prepare Item').item.json;\nconst narration1 = item.narration_1 || '';\nconst narration2 = item.narration_2 || '';\nconst mergedNarration = narration1 + '\\n' + narration2;\n\nreturn [{\n  json: {\n    ...item,\n    narration: mergedNarration\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -560,
        -16
      ],
      "id": "14d19595-19d8-4561-ad70-0a7f111ee1ac",
      "name": "Merge Narrations"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.fish.audio/v1/tts",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.narration, reference_id: 'YOUR_VOICE_ID' }) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -320,
        -16
      ],
      "id": "3551516a-8fdf-4ada-8d00-4e4b6e103cfc",
      "name": "Generate Audio",
      "credentials": {
        "httpHeaderAuth": {
          "id": "YOUR_FISH_AUDIO_CREDENTIAL_ID",
          "name": "Fish Audio API"
        }
      }
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "/tmp/video.mp4",
        "dataPropertyName": "={{ $('Download Video').first().binary.data }}",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        -80,
        -16
      ],
      "id": "394ece8f-3576-47c7-a6be-717997a45250",
      "name": "Save Video"
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "/tmp/audio.mp3",
        "dataPropertyName": "={{ $('Generate Audio').first().binary.data }}",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        160,
        -16
      ],
      "id": "63ff0c77-6152-45d8-92c7-9bc3669f8aaf",
      "name": "Save Audio"
    },
    {
      "parameters": {
        "command": "=ffmpeg -y -i /tmp/video.mp4 -i /tmp/audio.mp3 -filter_complex '[1:a]adelay=3000|3000[delayed];[0:a][delayed]amix=inputs=2:duration=first[aout]' -map 0:v -map '[aout]' -c:v copy -c:a aac /tmp/output.mp4"
      },
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        400,
        -16
      ],
      "id": "b5e0d16e-5627-4422-963b-3f11f6fa4bff",
      "name": "Execute ffmpeg"
    },
    {
      "parameters": {
        "fileSelector": "/tmp/output.mp4",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        640,
        -16
      ],
      "id": "1775de25-28a0-4599-a820-e114c70ce780",
      "name": "Read Output"
    },
    {
      "parameters": {
        "name": "={{ $('Search Video File').item.json.name }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "={{ $('Prepare Item').item.json.category_folder_id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        880,
        -16
      ],
      "id": "669a8425-be9d-4f51-9de6-85264f24ffcc",
      "name": "Upload New Video",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "operation": "deleteFile",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Search Video File').item.json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1120,
        -16
      ],
      "id": "f5a38f70-0067-49f0-a3c1-6cd5767b46fe",
      "name": "Delete Original",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "YOUR_GAS_DEPLOY_URL",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"action\": \"updateAudioStatus\",\n  \"postId\": \"{{ $('Prepare Item').item.json.post_id }}\",\n  \"category\": \"{{ $('Prepare Item').item.json.category }}\",\n  \"status\": \"DONE\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1360,
        -16
      ],
      "id": "a3922382-d7e2-4d4a-ac2f-45e24fbfab00",
      "name": "Update audio_status"
    },
    {
      "parameters": {
        "jsCode": "// スキップログ\nconst item = $('Prepare Item').item.json;\nreturn [{\n  json: {\n    status: 'skipped',\n    reason: 'video_not_found',\n    post_id: item.post_id,\n    category: item.category\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -800,
        160
      ],
      "id": "c2b4d3d2-0668-4b7f-8bd1-84dc30e48b1c",
      "name": "Skip - No Video"
    }
  ],
  "pinData": {},
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Get Sheet A",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sheet A": {
      "main": [
        [
          {
            "node": "Get Sheet B",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sheet B": {
      "main": [
        [
          {
            "node": "Get Sheet C",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sheet C": {
      "main": [
        [
          {
            "node": "Get Sheet D",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sheet D": {
      "main": [
        [
          {
            "node": "Get Sheet E",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Sheet E": {
      "main": [
        [
          {
            "node": "Merge All and Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge All and Filter": {
      "main": [
        [
          {
            "node": "If Has Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Has Items": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Search Category Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Category Folder": {
      "main": [
        [
          {
            "node": "Prepare Item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Item": {
      "main": [
        [
          {
            "node": "Search Video File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Video File": {
      "main": [
        [
          {
            "node": "If Video Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Video Exists": {
      "main": [
        [
          {
            "node": "Download Video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Skip - No Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Skip - No Video": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Video": {
      "main": [
        [
          {
            "node": "Merge Narrations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Narrations": {
      "main": [
        [
          {
            "node": "Generate Audio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Audio": {
      "main": [
        [
          {
            "node": "Save Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Video": {
      "main": [
        [
          {
            "node": "Save Audio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Audio": {
      "main": [
        [
          {
            "node": "Execute ffmpeg",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute ffmpeg": {
      "main": [
        [
          {
            "node": "Read Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Output": {
      "main": [
        [
          {
            "node": "Upload New Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload New Video": {
      "main": [
        [
          {
            "node": "Delete Original",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Original": {
      "main": [
        [
          {
            "node": "Update audio_status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update audio_status": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "dfcb9e3e-84f2-4342-998a-7cfe00a66f4b",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "7b9aa171b14242b9606ae8a78c80997e8606678e3b8e440a23de3728e3221a90"
  },
  "id": "oNQh7lEsmIv2tfkH",
  "tags": []
}