Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script to insert a Media Pool item chunk by chunk on a timeline
- -- DaVinci Resolve Lua Script
- local resolve = Resolve()
- local projectManager = resolve:GetProjectManager()
- local project = projectManager:GetCurrentProject()
- local mediaPool = project:GetMediaPool()
- local timeline = project:GetCurrentTimeline()
- -- Main function
- function insertMediaByChunks(chunkDuration, videoTrack, audioTrack, startPosition)
- -- Default parameters
- chunkDuration = chunkDuration or 120 -- 5 seconds at 24fps by default
- videoTrack = videoTrack or 1
- audioTrack = audioTrack or 1
- startPosition = startPosition or 0
- -- Check if a timeline is open
- if not timeline then
- print("ERROR: No timeline open")
- return false
- end
- -- Get the first selected clip from the Media Pool
- local selectedClip = mediaPool:GetSelectedClips()[1]
- if not selectedClip then
- print("ERROR: No clip selected in Media Pool")
- return false
- end
- print("Selected clip: " .. selectedClip:GetName())
- -- Get clip properties
- local properties = selectedClip:GetClipProperty()
- local totalDuration = tonumber(properties["Frames"])
- local fps = tonumber(properties["FPS"])
- if not totalDuration or totalDuration <= 0 then
- print("ERROR: Unable to determine clip duration")
- return false
- end
- print(string.format("Total duration: %d frames (%.2f seconds at %.2f fps)",
- totalDuration, totalDuration/fps, fps))
- -- Calculate number of chunks
- local numChunks = 0
- if chunkDuration > 0 then
- numChunks = math.ceil(totalDuration / chunkDuration)
- print(string.format("Number of chunks to insert: %d", numChunks))
- else
- numChunks=math.abs(chunkDuration)
- chunkDuration=math.floor(totalDuration/numChunks)
- print(string.format("Chunk Duration: %d", chunkDuration))
- end
- -- Playback position on timeline (in frames)
- local timelinePosition = startPosition
- -- Insert each chunk
- for i = 0, numChunks - 1 do
- local startFrame = i * chunkDuration + 1
- local endFrame = math.min((i + 1) * chunkDuration, totalDuration)
- print(string.format("Chunk %d: frames %d to %d", i + 1, startFrame, endFrame))
- -- Create clip data for insertion
- local clipInfo = {
- mediaPoolItem = selectedClip,
- startFrame = startFrame,
- endFrame = endFrame,
- trackIndex = videoTrack,
- recordFrame = timelinePosition
- }
- -- Insert clip on timeline
- local success = mediaPool:AppendToTimeline({clipInfo})
- if success then
- print(string.format(" -> Chunk %d inserted successfully", i + 1))
- else
- print(string.format(" -> ERROR: Failed to insert chunk %d", i + 1))
- end
- -- Update position for next chunk
- -- timelinePosition = timelinePosition + (endFrame - startFrame + 1)
- timelinePosition = timelinePosition + (endFrame - startFrame)
- end
- print("Insertion completed!")
- return true
- end
- -- Execute the script
- -- Usage examples:
- -- 1. Basic usage (default parameters)
- -- insertMediaByChunks()
- -- 2. With custom duration (10 seconds at 24fps = 240 frames)
- -- insertMediaByChunks(240)
- -- 3. Specify then number of chunck instead of the duration
- -- insertMediaByChunks(-5)
- -- 4. With all parameters
- -- insertMediaByChunks(300, 2, 1, 100)
- -- -> Chunks of 300 frames, video track 2, audio track 1, start at frame 100
Advertisement
Add Comment
Please, Sign In to add comment