Totox691

Davinci - Script to insert a Media Pool item chunk by chunk on a timeline

Oct 17th, 2025 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. -- Script to insert a Media Pool item chunk by chunk on a timeline
  2. -- DaVinci Resolve Lua Script
  3.  
  4. local resolve = Resolve()
  5. local projectManager = resolve:GetProjectManager()
  6. local project = projectManager:GetCurrentProject()
  7. local mediaPool = project:GetMediaPool()
  8. local timeline = project:GetCurrentTimeline()
  9.  
  10. -- Main function
  11. function insertMediaByChunks(chunkDuration, videoTrack, audioTrack, startPosition)
  12.     -- Default parameters
  13.     chunkDuration = chunkDuration or 120 -- 5 seconds at 24fps by default
  14.     videoTrack = videoTrack or 1
  15.     audioTrack = audioTrack or 1
  16.     startPosition = startPosition or 0
  17.    
  18.     -- Check if a timeline is open
  19.     if not timeline then
  20.         print("ERROR: No timeline open")
  21.         return false
  22.     end
  23.    
  24.     -- Get the first selected clip from the Media Pool
  25.     local selectedClip = mediaPool:GetSelectedClips()[1]
  26.    
  27.     if not selectedClip then
  28.         print("ERROR: No clip selected in Media Pool")
  29.         return false
  30.     end
  31.    
  32.     print("Selected clip: " .. selectedClip:GetName())
  33.    
  34.     -- Get clip properties
  35.     local properties = selectedClip:GetClipProperty()
  36.     local totalDuration = tonumber(properties["Frames"])
  37.     local fps = tonumber(properties["FPS"])
  38.    
  39.     if not totalDuration or totalDuration <= 0 then
  40.         print("ERROR: Unable to determine clip duration")
  41.         return false
  42.     end
  43.    
  44.     print(string.format("Total duration: %d frames (%.2f seconds at %.2f fps)",
  45.           totalDuration, totalDuration/fps, fps))
  46.    
  47.     -- Calculate number of chunks
  48.     local numChunks = 0
  49.     if chunkDuration > 0 then
  50.         numChunks = math.ceil(totalDuration / chunkDuration)
  51.         print(string.format("Number of chunks to insert: %d", numChunks))
  52.     else
  53.          numChunks=math.abs(chunkDuration)
  54.          chunkDuration=math.floor(totalDuration/numChunks)
  55.          print(string.format("Chunk Duration: %d", chunkDuration))
  56.     end
  57.    
  58.     -- Playback position on timeline (in frames)
  59.     local timelinePosition = startPosition
  60.    
  61.     -- Insert each chunk
  62.     for i = 0, numChunks - 1 do
  63.         local startFrame = i * chunkDuration + 1
  64.         local endFrame = math.min((i + 1) * chunkDuration, totalDuration)
  65.        
  66.         print(string.format("Chunk %d: frames %d to %d", i + 1, startFrame, endFrame))
  67.        
  68.         -- Create clip data for insertion
  69.         local clipInfo = {
  70.             mediaPoolItem = selectedClip,
  71.             startFrame = startFrame,
  72.             endFrame = endFrame,
  73.             trackIndex = videoTrack,
  74.             recordFrame = timelinePosition
  75.         }
  76.        
  77.         -- Insert clip on timeline
  78.         local success = mediaPool:AppendToTimeline({clipInfo})
  79.        
  80.         if success then
  81.             print(string.format("  -> Chunk %d inserted successfully", i + 1))
  82.         else
  83.             print(string.format("  -> ERROR: Failed to insert chunk %d", i + 1))
  84.         end
  85.        
  86.         -- Update position for next chunk
  87.         -- timelinePosition = timelinePosition + (endFrame - startFrame + 1)
  88.         timelinePosition = timelinePosition + (endFrame - startFrame)
  89.     end
  90.    
  91.     print("Insertion completed!")
  92.     return true
  93. end
  94.  
  95. -- Execute the script
  96. -- Usage examples:
  97.  
  98. -- 1. Basic usage (default parameters)
  99. -- insertMediaByChunks()
  100.  
  101. -- 2. With custom duration (10 seconds at 24fps = 240 frames)
  102. -- insertMediaByChunks(240)
  103.  
  104. -- 3. Specify then number of chunck instead of the duration
  105. -- insertMediaByChunks(-5)
  106.  
  107. -- 4. With all parameters
  108. -- insertMediaByChunks(300, 2, 1, 100)
  109. --   -> Chunks of 300 frames, video track 2, audio track 1, start at frame 100
Advertisement
Add Comment
Please, Sign In to add comment