{
  "kind": "dsl",
  "metadata": {
    "title": "Export Sequence Markers To SRT",
    "descriptionMarkdown": "Exports active-sequence markers as subtitle entries in an `.srt` file. You can choose whether the subtitle text comes from the marker name, the marker comment, or both. Markers whose resolved subtitle text is empty are skipped, and markers with zero duration are skipped because they cannot produce a valid subtitle range."
  },
  "dsl": {
    "version": 0,
    "entry": "b1",
    "blocks": {
      "b1": {
        "type": "setVar",
        "inputs": {
          "name": "totalMarkerCount",
          "value": 0
        },
        "next": "b2"
      },
      "b2": {
        "type": "setVar",
        "inputs": {
          "name": "skippedEmptyTextCount",
          "value": 0
        },
        "next": "b3"
      },
      "b3": {
        "type": "setVar",
        "inputs": {
          "name": "skippedZeroDurationCount",
          "value": 0
        },
        "next": "b4"
      },
      "b4": {
        "type": "setVar",
        "inputs": {
          "name": "exportableMarkerCount",
          "value": 0
        },
        "next": "b4a"
      },
      "b4a": {
        "type": "setVar",
        "inputs": {
          "name": "promptFields",
          "value": []
        },
        "next": "b4b"
      },
      "b4b": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "SELECT",
              "label": "Subtitle text source",
              "var": "textSourceMode",
              "required": true,
              "options": [
                "Marker name",
                "Marker comment",
                "Marker name + comment"
              ],
              "defaultValue": "Marker name"
            }
          }
        },
        "next": "b4c"
      },
      "b4c": {
        "type": "uiPromptForm",
        "inputs": {
          "title": "SRT Export Options",
          "message": "Choose which marker text should be written into each subtitle entry.",
          "fields": {
            "var": "promptFields"
          },
          "cancelBehavior": "STOP"
        },
        "next": "b5"
      },
      "b5": {
        "type": "prForEachMarker",
        "inputs": {
          "owner": "::SEQ\\ACTIVE",
          "order": "ASC",
          "markerVar": "markerRef",
          "indexVar": "i"
        },
        "body": [
          {
            "type": "setVar",
            "inputs": {
              "name": "totalMarkerCount",
              "value": {
                "type": "mathArithmetic",
                "inputs": {
                  "op": "ADD",
                  "a": {
                    "var": "totalMarkerCount"
                  },
                  "b": 1
                }
              }
            }
          },
          {
            "type": "setVar",
            "inputs": {
              "name": "markerName",
              "value": {
                "type": "prGetMarkerProperty",
                "inputs": {
                  "marker": {
                    "var": "markerRef"
                  },
                  "property": "NAME"
                }
              }
            }
          },
          {
            "type": "setVar",
            "inputs": {
              "name": "markerComment",
              "value": {
                "type": "prGetMarkerProperty",
                "inputs": {
                  "marker": {
                    "var": "markerRef"
                  },
                  "property": "COMMENTS"
                }
              }
            }
          },
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "EQ",
                      "a": {
                        "var": "textSourceMode"
                      },
                      "b": "Marker comment"
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "markerText",
                        "value": {
                          "var": "markerComment"
                        }
                      }
                    }
                  ]
                },
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "EQ",
                      "a": {
                        "var": "textSourceMode"
                      },
                      "b": "Marker name + comment"
                    }
                  },
                  "body": [
                    {
                      "type": "if",
                      "inputs": {
                        "branches": [
                          {
                            "condition": {
                              "type": "textIsEmpty",
                              "inputs": {
                                "value": {
                                  "var": "markerName"
                                }
                              }
                            },
                            "body": [
                              {
                                "type": "setVar",
                                "inputs": {
                                  "name": "markerText",
                                  "value": {
                                    "var": "markerComment"
                                  }
                                }
                              }
                            ]
                          },
                          {
                            "condition": {
                              "type": "textIsEmpty",
                              "inputs": {
                                "value": {
                                  "var": "markerComment"
                                }
                              }
                            },
                            "body": [
                              {
                                "type": "setVar",
                                "inputs": {
                                  "name": "markerText",
                                  "value": {
                                    "var": "markerName"
                                  }
                                }
                              }
                            ]
                          }
                        ],
                        "elseBody": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerText",
                              "value": {
                                "type": "textJoin",
                                "inputs": {
                                  "items": [
                                    {
                                      "var": "markerName"
                                    },
                                    "\n",
                                    {
                                      "var": "markerComment"
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "markerText",
                    "value": {
                      "var": "markerName"
                    }
                  }
                }
              ]
            }
          },
          {
            "type": "setVar",
            "inputs": {
              "name": "markerDurationSeconds",
              "value": {
                "type": "prGetMarkerProperty",
                "inputs": {
                  "marker": {
                    "var": "markerRef"
                  },
                  "property": "DURATION_SECONDS"
                }
              }
            }
          },
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "textIsEmpty",
                    "inputs": {
                      "value": {
                        "var": "markerText"
                      }
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedEmptyTextCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedEmptyTextCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                },
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "LTE",
                      "a": {
                        "var": "markerDurationSeconds"
                      },
                      "b": 0
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedZeroDurationCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedZeroDurationCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "exportableMarkerCount",
                    "value": {
                      "type": "mathArithmetic",
                      "inputs": {
                        "op": "ADD",
                        "a": {
                          "var": "exportableMarkerCount"
                        },
                        "b": 1
                      }
                    }
                  }
                }
              ]
            }
          }
        ],
        "next": "b6"
      },
      "b6": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "totalMarkerCount"
                  },
                  "b": 0
                }
              },
              "body": [
                {
                  "type": "uiShowMessage",
                  "inputs": {
                    "message": "No sequence markers were found in the active sequence, so no SRT file was exported. If you expected clip markers, note that this example exports only sequence markers."
                  }
                }
              ]
            },
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "exportableMarkerCount"
                  },
                  "b": 0
                }
              },
              "body": [
                {
                  "type": "uiShowMessage",
                  "inputs": {
                    "message": {
                      "type": "textJoin",
                      "inputs": {
                        "items": [
                          "No SRT file was exported. Found ",
                          {
                            "var": "totalMarkerCount"
                          },
                          " sequence marker(s), but skipped ",
                          {
                            "var": "skippedEmptyTextCount"
                          },
                          " with empty resolved subtitle text and ",
                          {
                            "var": "skippedZeroDurationCount"
                          },
                          " with zero duration."
                        ]
                      }
                    }
                  }
                }
              ]
            }
          ],
          "elseBody": [
            {
              "type": "setVar",
              "inputs": {
                "name": "exportPath",
                "value": {
                  "type": "saveFileDialog",
                  "inputs": {
                    "suggestedName": "captions.srt",
                    "types": [
                      "srt"
                    ],
                    "cancelBehavior": "STOP",
                    "initialDomain": "USER_DOCUMENTS"
                  }
                }
              }
            },
            {
              "type": "setVar",
              "inputs": {
                "name": "exportedMarkerCount",
                "value": 0
              }
            },
            {
              "type": "prForEachMarker",
              "inputs": {
                "owner": "::SEQ\\ACTIVE",
                "order": "ASC",
                "markerVar": "markerRef",
                "indexVar": "i"
              },
              "body": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "markerName",
                    "value": {
                      "type": "prGetMarkerProperty",
                      "inputs": {
                        "marker": {
                          "var": "markerRef"
                        },
                        "property": "NAME"
                      }
                    }
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "markerComment",
                    "value": {
                      "type": "prGetMarkerProperty",
                      "inputs": {
                        "marker": {
                          "var": "markerRef"
                        },
                        "property": "COMMENTS"
                      }
                    }
                  }
                },
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "var": "textSourceMode"
                            },
                            "b": "Marker comment"
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerText",
                              "value": {
                                "var": "markerComment"
                              }
                            }
                          }
                        ]
                      },
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "var": "textSourceMode"
                            },
                            "b": "Marker name + comment"
                          }
                        },
                        "body": [
                          {
                            "type": "if",
                            "inputs": {
                              "branches": [
                                {
                                  "condition": {
                                    "type": "textIsEmpty",
                                    "inputs": {
                                      "value": {
                                        "var": "markerName"
                                      }
                                    }
                                  },
                                  "body": [
                                    {
                                      "type": "setVar",
                                      "inputs": {
                                        "name": "markerText",
                                        "value": {
                                          "var": "markerComment"
                                        }
                                      }
                                    }
                                  ]
                                },
                                {
                                  "condition": {
                                    "type": "textIsEmpty",
                                    "inputs": {
                                      "value": {
                                        "var": "markerComment"
                                      }
                                    }
                                  },
                                  "body": [
                                    {
                                      "type": "setVar",
                                      "inputs": {
                                        "name": "markerText",
                                        "value": {
                                          "var": "markerName"
                                        }
                                      }
                                    }
                                  ]
                                }
                              ],
                              "elseBody": [
                                {
                                  "type": "setVar",
                                  "inputs": {
                                    "name": "markerText",
                                    "value": {
                                      "type": "textJoin",
                                      "inputs": {
                                        "items": [
                                          {
                                            "var": "markerName"
                                          },
                                          "\n",
                                          {
                                            "var": "markerComment"
                                          }
                                        ]
                                      }
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    ],
                    "elseBody": [
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "markerText",
                          "value": {
                            "var": "markerName"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "markerDurationSeconds",
                    "value": {
                      "type": "prGetMarkerProperty",
                      "inputs": {
                        "marker": {
                          "var": "markerRef"
                        },
                        "property": "DURATION_SECONDS"
                      }
                    }
                  }
                },
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicOperation",
                          "inputs": {
                            "op": "AND",
                            "a": {
                              "type": "logicNegate",
                              "inputs": {
                                "value": {
                                  "type": "textIsEmpty",
                                  "inputs": {
                                    "value": {
                                      "var": "markerText"
                                    }
                                  }
                                }
                              }
                            },
                            "b": {
                              "type": "logicCompare",
                              "inputs": {
                                "op": "GT",
                                "a": {
                                  "var": "markerDurationSeconds"
                                },
                                "b": 0
                              }
                            }
                          }
                        },
                        "body": [
                          {
                            "type": "srtAddEntry",
                            "inputs": {
                              "path": {
                                "var": "exportPath"
                              },
                              "start": {
                                "type": "prFormatTime",
                                "inputs": {
                                  "timeSeconds": {
                                    "type": "prGetMarkerProperty",
                                    "inputs": {
                                      "marker": {
                                        "var": "markerRef"
                                      },
                                      "property": "START_SECONDS"
                                    }
                                  },
                                  "format": "HH_MM_SS_MS"
                                }
                              },
                              "end": {
                                "type": "prFormatTime",
                                "inputs": {
                                  "timeSeconds": {
                                    "type": "prGetMarkerProperty",
                                    "inputs": {
                                      "marker": {
                                        "var": "markerRef"
                                      },
                                      "property": "END_SECONDS"
                                    }
                                  },
                                  "format": "HH_MM_SS_MS"
                                }
                              },
                              "text": {
                                "var": "markerText"
                              },
                              "append": {
                                "type": "logicCompare",
                                "inputs": {
                                  "op": "GT",
                                  "a": {
                                    "var": "exportedMarkerCount"
                                  },
                                  "b": 0
                                }
                              }
                            }
                          },
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "exportedMarkerCount",
                              "value": {
                                "type": "mathArithmetic",
                                "inputs": {
                                  "op": "ADD",
                                  "a": {
                                    "var": "exportedMarkerCount"
                                  },
                                  "b": 1
                                }
                              }
                            }
                          }
                        ]
                      }
                    ]
                  }
                }
              ]
            },
            {
              "type": "if",
              "inputs": {
                "branches": [
                  {
                    "condition": {
                      "type": "logicOperation",
                      "inputs": {
                        "op": "OR",
                        "a": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "GT",
                            "a": {
                              "var": "skippedEmptyTextCount"
                            },
                            "b": 0
                          }
                        },
                        "b": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "GT",
                            "a": {
                              "var": "skippedZeroDurationCount"
                            },
                            "b": 0
                          }
                        }
                      }
                    },
                    "body": [
                      {
                        "type": "uiShowMessage",
                        "inputs": {
                          "message": {
                            "type": "textJoin",
                            "inputs": {
                              "items": [
                                "Exported ",
                                {
                                  "var": "exportedMarkerCount"
                                },
                                " subtitle(s) to ",
                                {
                                  "var": "exportPath"
                                },
                                ". Skipped ",
                                {
                                  "var": "skippedEmptyTextCount"
                                },
                                " marker(s) with empty text and ",
                                {
                                  "var": "skippedZeroDurationCount"
                                },
                                " marker(s) with zero duration."
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                ],
                "elseBody": [
                  {
                    "type": "uiShowMessage",
                    "inputs": {
                      "message": {
                        "type": "textJoin",
                        "inputs": {
                          "items": [
                            "Exported ",
                            {
                              "var": "exportedMarkerCount"
                            },
                            " subtitle(s) to ",
                            {
                              "var": "exportPath"
                            }
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          ]
        },
        "next": null
      }
    }
  }
}
