Totox691

Davinci resolve - extract and suffle cilps

Dec 7th, 2025
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. -- Script DaVinci Resolve - Extraits aléatoires en Lua 5.1
  2. -- Ajoute des extraits de 10 secondes des clips sélectionnés
  3. -- dans un ordre aléatoire à la timeline avec point de départ aléatoire
  4.  
  5. -- ========== OPTIONS ==========
  6. local EXTRACT_DURATION = 10 -- Durée de l'extrait en secondes
  7. local SKIP_SHORT_CLIPS = false -- true = ignorer les clips < 10s, false = utiliser leur durée complète
  8. local SHUFFLE_CLIPS = true -- true = ordre aléatoire, false = ordre de sélection
  9. -- ==============================
  10.  
  11. -- Initialisation du générateur aléatoire
  12. math.randomseed(os.time())
  13.  
  14. -- Fonction pour mélanger un tableau (algorithme Fisher-Yates)
  15. function shuffle_table(t)
  16. local n = #t
  17. for i = n, 2, -1 do
  18. local j = math.random(i)
  19. t[i], t[j] = t[j], t[i]
  20. end
  21. return t
  22. end
  23.  
  24. -- Connexion à DaVinci Resolve
  25. local resolve = Resolve()
  26. if not resolve then
  27. print("Error: Unable to connect to DaVinci Resolve")
  28. return
  29. end
  30.  
  31. -- Récupération du projet
  32. local projectManager = resolve:GetProjectManager()
  33. local project = projectManager:GetCurrentProject()
  34. if not project then
  35. print("Error: No project open")
  36. return
  37. end
  38.  
  39. -- Récupération du Media Pool
  40. local mediaPool = project:GetMediaPool()
  41.  
  42. -- Récupération des clips SÉLECTIONNÉS uniquement
  43. local selectedClips = mediaPool:GetSelectedClips()
  44. if not selectedClips or #selectedClips == 0 then
  45. print("Error: No clips selected in Media Pool")
  46. print("Please select the clips you want to use")
  47. return
  48. end
  49.  
  50. -- Mélange aléatoire des clips si l'option est activée
  51. if SHUFFLE_CLIPS then
  52. selectedClips = shuffle_table(selectedClips)
  53. print("Mode: Random order")
  54. else
  55. print("Mode: Selection order")
  56. end
  57.  
  58. -- Récupération de la timeline active
  59. local timeline = project:GetCurrentTimeline()
  60. if not timeline then
  61. print("Error: No active timeline")
  62. return
  63. end
  64.  
  65. -- Paramètres
  66. local framerate = tonumber(timeline:GetSetting("timelineFrameRate"))
  67. local extractFrames = math.floor(EXTRACT_DURATION * framerate)
  68.  
  69. print(string.format("=== Adding %d selected clips ===", #selectedClips))
  70. print(string.format("Extract duration: %d seconds", EXTRACT_DURATION))
  71. print(string.format("Framerate: %.2f fps", framerate))
  72. print(string.format("Order: %s", SHUFFLE_CLIPS and "Random" or "Selection"))
  73. if SKIP_SHORT_CLIPS then
  74. print("Option: Clips < 10s will be skipped")
  75. else
  76. print("Option: Clips < 10s will use full duration")
  77. end
  78. print("---")
  79.  
  80. -- Ajout des clips à la timeline
  81. local addedCount = 0
  82. local skippedCount = 0
  83. local currentPosition = 0 -- Position actuelle sur la timeline en frames
  84.  
  85. for i, clip in ipairs(selectedClips) do
  86. local clipName = clip:GetName()
  87. local clipProperties = clip:GetClipProperty()
  88.  
  89. -- Durée du clip en frames
  90. local clipDuration = tonumber(clipProperties["Frames"])
  91. local clipDurationSec = clipDuration / framerate
  92.  
  93. -- Vérifier si le clip est assez long
  94. if clipDuration < extractFrames then
  95. if SKIP_SHORT_CLIPS then
  96. print(string.format("Skipped clip (%.1fs): %s", clipDurationSec, clipName))
  97. skippedCount = skippedCount + 1
  98. else
  99. -- Utiliser toute la durée du clip
  100. local clipInfo = {
  101. mediaPoolItem = clip,
  102. startFrame = 0,
  103. endFrame = clipDuration - 1,
  104. recordFrame = currentPosition
  105. }
  106.  
  107. local success = mediaPool:AppendToTimeline({clipInfo})
  108.  
  109. if success then
  110. addedCount = addedCount + 1
  111. currentPosition = currentPosition + clipDuration
  112. print(string.format("[%d/%d] Added (%.1fs, full duration): %s", addedCount, #selectedClips, clipDurationSec, clipName))
  113. else
  114. print(string.format("ERROR adding clip: %s", clipName))
  115. end
  116. end
  117. else
  118. -- Le clip est assez long, choisir un point de départ aléatoire
  119. local maxStartFrame = clipDuration - extractFrames
  120. local randomStartFrame = math.random(0, maxStartFrame)
  121.  
  122. local clipInfo = {
  123. mediaPoolItem = clip,
  124. startFrame = randomStartFrame,
  125. endFrame = randomStartFrame + extractFrames - 1,
  126. recordFrame = currentPosition
  127. }
  128.  
  129. local success = mediaPool:AppendToTimeline({clipInfo})
  130.  
  131. if success then
  132. addedCount = addedCount + 1
  133. currentPosition = currentPosition + extractFrames
  134. local startTimecode = randomStartFrame / framerate
  135. print(string.format("[%d/%d] Added (from %.1fs): %s", addedCount, #selectedClips, startTimecode, clipName))
  136. else
  137. print(string.format("ERROR adding clip: %s", clipName))
  138. end
  139. end
  140. end
  141.  
  142. print("---")
  143. print(string.format("=== Done! %d clips added, %d clips skipped ===", addedCount, skippedCount))
Advertisement
Add Comment
Please, Sign In to add comment