Plain Text Subtitle Exporter Lua Resolve Script

Get answers to your questions about color grading, editing and finishing with DaVinci Resolve.
  • Author
  • Message
Offline

DorinDXN

  • Posts: 174
  • Joined: Thu Aug 04, 2022 5:07 pm
  • Real Name: Dorin Godja

Plain Text Subtitle Exporter Lua Resolve Script

PostThu Oct 24, 2024 8:53 am

IMPORTANT!
Scripts with UI require Studio license since 19.1.
This script will work in the Free version of DaVinci Resolve only if DR is prior to 19.1


How to use the script.

Change the filepath content of this script then copy the script and paste it into
Menu: Workspace \ Console
then hit Enter.
The subtitle will be saved as plain text, with no time codes, to the subtitle.txt file in the specified path.
You can also adjust the newline characters;
the line will break only when a caption from the subtitle ends in one of those characters.

Code: Select all
-- Plain Text Subtitle Exporter Lua Resolve Script by DorinDXN
-- version 1.5
-- license CC0
--
--[[
Save this file as:
Windows
C:\ProgramData\Blackmagic Design\DaVinci Resolve\Fusion\Scripts\Utility\DXN Plain Text Subtitle Exporter.lua
Mac
/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Scripts/Utility/DXN Plain Text Subtitle Exporter.lua

then you can launch it using the menu:
Workspace \ Scripts \ DXN Plain Text Subtitle Exporter
 --]]

local filePath = "E:/Resolve/Forum/Subtitle/subtitle.txt"
local ui = fu.UIManager
local disp = bmd.UIDispatcher(ui)
local fileOk = false;

local function getFilePath()
    local win = disp:AddWindow({
        ID = "FileWin",
        WindowTitle = "Enter File Path",
        Geometry = { 100, 100, 600, 100 },
         WindowFlags = {
            Tool = true,
            MSWindowsFixedSizeDialogHint = true
        },
        ui:VGroup {
            ID = "root",
            ui:LineEdit {ID = "filePath", Text = filePath },
            ui:Button {ID = "okBtn", Text = "OK"},
        }
    })

    local itm = win:GetItems()
    function win.On.okBtn.Clicked(ev)
       filePath = itm.filePath.Text
      fileOk = true
      disp:ExitLoop()
    end
   function win.On.FileWin.Close(ev)
      disp:ExitLoop()
      fileOk = false
   end   

    win:Show()
    disp:RunLoop()
    win:Hide()

end

getFilePath()
if fileOk then
   local newLineChars = {".", "?", "!"} -- new line after these
   local replaceChars = {"\226\128\168", "\226\128\169"} -- will be replaced with space
   local resolve = app:GetResolve()
   local projectManager = resolve:GetProjectManager()
   local project = projectManager:GetCurrentProject()
   local timeline = project:GetCurrentTimeline()

   if timeline == nil then
      print("No Active Timeline")
      os.exit()
   end

   local subtitles = timeline:GetItemListInTrack("subtitle", 1)
   local file = io.open(filePath, "w")

   local function endsWithAny(str, chars)
      for _, char in ipairs(chars) do
         if str:sub(-1) == char then
            return true
         end
      end
      return false
   end

   local function replaceSpecialChars(str)
      for _, char in ipairs(replaceChars) do
         str = str:gsub(char, " ")
      end
      return str
   end

   for key, value in pairs(subtitles) do
      if key == "__flags" then
         break
      end
      local caption = replaceSpecialChars(value:GetName())
      file:write(caption .. " ")
      if endsWithAny(caption, newLineChars) then
         file:write("\n")
      end
   end

   file:close()
   print("Subtitles have been saved to " .. filePath)
else
   print("Save canceled")
end


cheers,
Dorin
DaVinci Resolve Anthem
https://www.youtube.com/watch?v=CcKC8R9ku50
DaVinci Resolve Clips
https://www.youtube.com/playlist?list=PLYgg-7sUX2Jh3dqNJ0AVBTIlDFtufrYyn
Playhead Lock Script
https://forum.blackmagicdesign.com/viewtopic.php?f=21&t=210163

Return to DaVinci Resolve

Who is online

Users browsing this forum: Bing [Bot], juanborras, Mads Johansen, renzhezhu and 249 guests