Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script DaVinci Resolve - Extraits aléatoires en Lua 5.1
- -- Ajoute des extraits de 10 secondes des clips sélectionnés
- -- dans un ordre aléatoire à la timeline avec point de départ aléatoire
- -- ========== OPTIONS ==========
- local EXTRACT_DURATION = 10 -- Durée de l'extrait en secondes
- local SKIP_SHORT_CLIPS = false -- true = ignorer les clips < 10s, false = utiliser leur durée complète
- local SHUFFLE_CLIPS = true -- true = ordre aléatoire, false = ordre de sélection
- -- ==============================
- -- Initialisation du générateur aléatoire
- math.randomseed(os.time())
- -- Fonction pour mélanger un tableau (algorithme Fisher-Yates)
- function shuffle_table(t)
- local n = #t
- for i = n, 2, -1 do
- local j = math.random(i)
- t[i], t[j] = t[j], t[i]
- end
- return t
- end
- -- Connexion à DaVinci Resolve
- local resolve = Resolve()
- if not resolve then
- print("Error: Unable to connect to DaVinci Resolve")
- return
- end
- -- Récupération du projet
- local projectManager = resolve:GetProjectManager()
- local project = projectManager:GetCurrentProject()
- if not project then
- print("Error: No project open")
- return
- end
- -- Récupération du Media Pool
- local mediaPool = project:GetMediaPool()
- -- Récupération des clips SÉLECTIONNÉS uniquement
- local selectedClips = mediaPool:GetSelectedClips()
- if not selectedClips or #selectedClips == 0 then
- print("Error: No clips selected in Media Pool")
- print("Please select the clips you want to use")
- return
- end
- -- Mélange aléatoire des clips si l'option est activée
- if SHUFFLE_CLIPS then
- selectedClips = shuffle_table(selectedClips)
- print("Mode: Random order")
- else
- print("Mode: Selection order")
- end
- -- Récupération de la timeline active
- local timeline = project:GetCurrentTimeline()
- if not timeline then
- print("Error: No active timeline")
- return
- end
- -- Paramètres
- local framerate = tonumber(timeline:GetSetting("timelineFrameRate"))
- local extractFrames = math.floor(EXTRACT_DURATION * framerate)
- print(string.format("=== Adding %d selected clips ===", #selectedClips))
- print(string.format("Extract duration: %d seconds", EXTRACT_DURATION))
- print(string.format("Framerate: %.2f fps", framerate))
- print(string.format("Order: %s", SHUFFLE_CLIPS and "Random" or "Selection"))
- if SKIP_SHORT_CLIPS then
- print("Option: Clips < 10s will be skipped")
- else
- print("Option: Clips < 10s will use full duration")
- end
- print("---")
- -- Ajout des clips à la timeline
- local addedCount = 0
- local skippedCount = 0
- local currentPosition = 0 -- Position actuelle sur la timeline en frames
- for i, clip in ipairs(selectedClips) do
- local clipName = clip:GetName()
- local clipProperties = clip:GetClipProperty()
- -- Durée du clip en frames
- local clipDuration = tonumber(clipProperties["Frames"])
- local clipDurationSec = clipDuration / framerate
- -- Vérifier si le clip est assez long
- if clipDuration < extractFrames then
- if SKIP_SHORT_CLIPS then
- print(string.format("Skipped clip (%.1fs): %s", clipDurationSec, clipName))
- skippedCount = skippedCount + 1
- else
- -- Utiliser toute la durée du clip
- local clipInfo = {
- mediaPoolItem = clip,
- startFrame = 0,
- endFrame = clipDuration - 1,
- recordFrame = currentPosition
- }
- local success = mediaPool:AppendToTimeline({clipInfo})
- if success then
- addedCount = addedCount + 1
- currentPosition = currentPosition + clipDuration
- print(string.format("[%d/%d] Added (%.1fs, full duration): %s", addedCount, #selectedClips, clipDurationSec, clipName))
- else
- print(string.format("ERROR adding clip: %s", clipName))
- end
- end
- else
- -- Le clip est assez long, choisir un point de départ aléatoire
- local maxStartFrame = clipDuration - extractFrames
- local randomStartFrame = math.random(0, maxStartFrame)
- local clipInfo = {
- mediaPoolItem = clip,
- startFrame = randomStartFrame,
- endFrame = randomStartFrame + extractFrames - 1,
- recordFrame = currentPosition
- }
- local success = mediaPool:AppendToTimeline({clipInfo})
- if success then
- addedCount = addedCount + 1
- currentPosition = currentPosition + extractFrames
- local startTimecode = randomStartFrame / framerate
- print(string.format("[%d/%d] Added (from %.1fs): %s", addedCount, #selectedClips, startTimecode, clipName))
- else
- print(string.format("ERROR adding clip: %s", clipName))
- end
- end
- end
- print("---")
- print(string.format("=== Done! %d clips added, %d clips skipped ===", addedCount, skippedCount))
Advertisement
Add Comment
Please, Sign In to add comment