Advertisement
DimOkGamer

Davinci Resolve - Copy Timecodes by Markers

Apr 22nd, 2022
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. -- How to install: https://gist.github.com/DmitriySalnikov/016648a478f668c48a0322e1051147ca#file-readme-md
  2. ------------------------------------------------------------------------------
  3. -- by Dmitriy Salnikov
  4. -- GitHub: https://github.com/DmitriySalnikov
  5. -- Youtube: https://www.youtube.com/channel/UCUPGXIVLD2jxML3L1qqkoEw
  6. --
  7. -- Config:
  8. --
  9. -- The first title is at 00:00 if the marker is not found
  10. firstTitle = "Intro"
  11.  
  12. -- Filter by marker color (true) or by name (false)
  13. isFilterByColor = true
  14.  
  15. -- Color filter for markers
  16. -- Available marker colors (ordered like in Davinci): Blue, Cyan, Green, Yellow,
  17. -- Red, Pink, Purple, Fuchsia, Rose, Lavender, Sky, Mint, Lemon, Sand, Cocoa, Cream
  18. colorFilter = "Cream"
  19.  
  20. -- Name filter for markers
  21. -- Any string that Davinci can accept as Name of marker is allowed
  22. nameFilter = "Timecode"
  23.  
  24. -- Enable (true) or disable (false) resulting dialog
  25. showCopyDialog = true
  26.  
  27. -- Enable (true) or disable (false) console output
  28. printToConsole = false
  29.  
  30. ------------------------------------------------------------------------------
  31. -- The Linux copy to clipboard command is "xclip"
  32. -- This requires a custom xclip tool install on Linux:
  33.  
  34. -- Debian/Ubuntu:
  35. -- sudo apt-get install xclip
  36.  
  37. -- Redhat/Centos/Fedora:
  38. -- yum install xclip
  39.  
  40. ------------------------------------------------------------------------------
  41. -- Initialization
  42. local platform = (FuPLATFORM_WINDOWS and "Windows") or
  43.                      (FuPLATFORM_MAC and "Mac") or
  44.                      (FuPLATFORM_LINUX and "Linux")
  45.  
  46. local result = ""
  47. local p = Resolve():GetProjectManager():GetCurrentProject()
  48. local t = p:GetCurrentTimeline()
  49. local m = t:GetMarkers()
  50. local timelineFPS = t:GetSetting("timelineFrameRate")
  51. local isLongerThanOneHour = (t:GetEndFrame() / (timelineFPS * 60.0 * 60.0) %
  52.                                 (60.0 * 60.0)) >= 1.0
  53. local exitCode = 0
  54.  
  55. -- Auto formatted by VS Code...
  56. ------------------------------------------------------------------------------
  57.  
  58. function FrameToText(frame)
  59.     local res = ""
  60.  
  61.     if isLongerThanOneHour then
  62.         res = res ..
  63.                   string.format("%02d", (frame / (timelineFPS * 60.0 * 60.0)) %
  64.                                     (60.0 * 60.0)) .. ":"
  65.     end
  66.     res = res .. string.format("%02d", (frame / (timelineFPS * 60.0)) % 60.0) ..
  67.               ":" .. string.format("%02d", (frame / timelineFPS) % 60.0)
  68.     return res
  69. end
  70.  
  71. -- https://gist.github.com/AndrewHazelden/b9909520490624305183f7c8f77368a2
  72. function CopyToClipboard(textString)
  73.     local outputDirectory = app:MapPath('Temp:/CopyTimecodebyMarkerstoClipboard/')
  74.     local clipboardTempFile = outputDirectory .. 'ClipboardText.txt'
  75.    
  76.     os.execute('mkdir "' .. outputDirectory .. '"')
  77.    
  78.     local outClipFile, err = io.open(clipboardTempFile, 'w')
  79.    
  80.     if err then
  81.         print("[Error Opening Clipboard Temporary File for Writing]")
  82.         return
  83.     end
  84.    
  85.     outClipFile:write(textString, '\n')
  86.     outClipFile:close()
  87.    
  88.     local command = ""
  89.     if platform == 'Windows' then
  90.         -- Windows requires powershell to support utf8 encoding
  91.         command = 'powershell -command "Get-Content -Encoding utf8 \'' ..
  92.                       clipboardTempFile .. '\' | Set-Clipboard"'
  93.     elseif platform == 'Mac' then
  94.         command = 'pbcopy < "' .. clipboardTempFile .. '"'
  95.     elseif platform == 'Linux' then
  96.         command = 'cat "' .. clipboardTempFile ..
  97.                       '" | xclip -selection clipboard &'
  98.     end
  99.     if printToConsole then
  100.         print('[Copy Text to Clipboard Command] ' .. command)
  101.         print('[Clipboard]\n' .. textString)
  102.     end
  103.     exitCode = os.execute(command)
  104. end
  105.  
  106. function CheckMarker(marker)
  107.     return (isFilterByColor and marker.color == colorFilter) or
  108.                (not isFilterByColor and marker.name == nameFilter)
  109. end
  110.  
  111. ------------------------------------------------------------------------------
  112.  
  113. -- First timecode
  114. if not (m[0] ~= nil and CheckMarker(m[0])) then
  115.     result = FrameToText(0) .. " " .. firstTitle .. "\n"
  116. end
  117.  
  118. -- Sort markers by time
  119. -- https://stackoverflow.com/a/26166560/8980874
  120. local tkeys = {}
  121. for k in pairs(m) do table.insert(tkeys, k) end
  122. table.sort(tkeys)
  123.  
  124. -- Generate resulting text
  125. for _, k in ipairs(tkeys) do
  126.     local val = m[k]
  127.     if CheckMarker(val) then
  128.         result = result .. FrameToText(k) .. " " ..
  129.                      (isFilterByColor and val.name or val.note) .. "\n"
  130.     end
  131. end
  132.  
  133. result = result:sub(1, -2)
  134. CopyToClipboard(result)
  135.  
  136. ------------------------------------------------------------------------------
  137. -- Resulting Dialog
  138. -- https://www.steakunderwater.com/wesuckless/viewtopic.php?f=6&t=1411
  139. if showCopyDialog then
  140.     local ui = fu.UIManager
  141.     local disp = bmd.UIDispatcher(ui)
  142.     local width, height = 550, 600
  143.  
  144.     local win = disp:AddWindow({
  145.         ID = 'CopyDialog',
  146.         WindowTitle = 'Copy Timecodes by Markers',
  147.         Geometry = {100, 100, width, height},
  148.         Spacing = 0,
  149.         Margin = 0,
  150.  
  151.         ui:VGroup{
  152.             ID = 'root',
  153.             ui:TextEdit{
  154.                 ID = 'TE',
  155.                 Weight = 0.5,
  156.                 HTML = [[<h1>Copy Timecodes by Markers</h1>
  157. <p><strong>Version 1.0</strong></p>
  158. <p>by Dmitriy Salnikov<br/>
  159. GitHub: <a href="https://github.com/DmitriySalnikov">https://github.com/DmitriySalnikov</a><br/>
  160. Youtube: <a href="https://www.youtube.com/channel/UCUPGXIVLD2jxML3L1qqkoEw">https://www.youtube.com/channel/UCUPGXIVLD2jxML3L1qqkoEw</a></p>]] ..
  161.                     ((exitCode == 0 or exitCode == true) and
  162.                         '<p>The resulting text has already been copied to the clipboard, if not, you can copy it yourself below.</p>' or
  163.                         '<p>The resulting text could not be copied to the clipboard automatically, but you can copy it by yourself below.</p>') ..
  164.                     '<p><strong>Result:</strong></p><hr/><br/><code>' ..
  165.                     result:gsub("\n", "<br/>") .. '</code>',
  166.                 ReadOnly = true
  167.             }
  168.         }
  169.     })
  170.  
  171.     function win.On.CopyDialog.Close(ev) disp:ExitLoop() end
  172.  
  173.     win:Show()
  174.     disp:RunLoop()
  175.     win:Hide()
  176. end
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement