Python - import clips into mediapool is very slow ?

Get answers to your questions about color grading, editing and finishing with DaVinci Resolve.
  • Author
  • Message
Offline

jensenni

  • Posts: 23
  • Joined: Wed Jan 27, 2021 12:26 am
  • Real Name: Jens Klein

Python - import clips into mediapool is very slow ?

PostTue Feb 09, 2021 5:54 pm

Hi, I am running a custom python script (v3.6.8) to import a list of clips into DavinciResolve(v15). The script works great but performs very slow while importing the clips. Resolve freezes for several minutes, depending on the number of clips to be imported.

Once imported the timeline is created really fast, so the problem seems to be the actual import where things are slowing down tremendously. The clips I am importing are *.dng file sequences from BMCC 2.5K.

Here is the path generated by the python script (example sequence):
Code: Select all
/Volumes/RAID/MEDIA/FW_A01_2020-12-01_1812_C0003/FW_A01_2020-12-01_1812_C0003_000000.dng.tra

Here's the part of the script for import:
Code: Select all
# Import clips from Search Result
    for oneClip in clipListRead:
        jens = oneClip.split(".tra")
        clips = resolve.GetMediaStorage().AddItemsToMediaPool(jens)  # <-- clip list goes here
        print(end='\n')  # <-- command line spacer:)
        foundM = 1 - len(jens)
        print('Done! I found --' + (str(foundM)) + ' matches---')
        # print("Timeline Clips: " + str(jens))


Is there a way to speed this up?

Thank you
Jens
Offline

Shrinivas Ramani

Blackmagic Design

  • Posts: 3030
  • Joined: Wed Sep 20, 2017 10:19 am

Re: Python - import clips into mediapool is very slow ?

PostWed Feb 10, 2021 2:18 am

Without much knowledge of how clipListRead is generated, I'd request that you a. print the value of oneClip, and b. the corresponding clips[0].GetName.

This would help determine if you're adding frames from the same DNG sequence multiple times.
Offline

jensenni

  • Posts: 23
  • Joined: Wed Jan 27, 2021 12:26 am
  • Real Name: Jens Klein

Re: Python - import clips into mediapool is very slow ?

PostWed Feb 10, 2021 6:17 am

Hi Shrinivas, I attached the script below as well as the print(s). This is my first script written in python, so I am sure there is a lot that can be shortened - or written differently altogether. Also, I can't figure out how to make a specific folder(bin) active in Resolve, with python.

CODE:
Code: Select all
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# PhraseFind v1.0 | Concept and written by Jens Klein | filmwerk.nyc
#
# This program searches a folder containing transcribed .txt files
# for phrases in an Actors' dialog.

# Initialize system
from python_get_resolve import GetResolve
import tkinter as tk
from tkinter import simpledialog as sd
from tkinter import *
import os.path
import glob
from os import listdir
import sys
import os
import logging

# Initialize Davinci Resolve (v15/16/17)
resolve = GetResolve()
projectManager = resolve.GetProjectManager()
project = projectManager.GetCurrentProject()
mediaPool = project.GetMediaPool()
rootFolder = mediaPool.GetRootFolder()
resolve_media_storage = resolve.GetMediaStorage()
currentFolder = mediaPool.GetCurrentFolder()
createFolder = projectManager.CreateFolder()

# Set MediaPath' & Vars
rootDir = '/Volumes/audio/TRANSCRIBE/OUT'
mediaDir = '/Volumes/RAID/DATA/MEDIA/'
sResult = '/Volumes/audio/TRANSCRIBE/'
sResultTXT = '/Volumes/audio/TRANSCRIBE/SearchPhrases/'
directoryName = rootDir

# UserInput via commandLine
searchPhrase = input("Enter keywords or phrase: ")
if not searchPhrase:
    print("You didn't type anything...")
    sys.exit()
else:
    pass

# # OR, get the Search GUI - needs a switch.
# root = tk.Tk()
# root.withdraw()
# root.option_add('*background', '#111111')
# root.option_add('*Entry*background', '#999999')
# searchPhrase = sd.askstring(
#     "DialogFinder v0.1     |     filmwerk.nyc  ",
#     "Type keywords, or an entire phrase, to search...you know what I mean :)", parent=root, )
# if not searchPhrase:
#     print("You didn't type anything...")
#     sys.exit()
# else:
#     pass

# Initialize (clear) our 'searchPhrase'.txt file
with open(sResultTXT + searchPhrase + ".txt", "w") as output:
    output.write("")

# CommandLine outputs
print(end='\n')  # <-- command line spacer:)
print("You searched for:  [ " + searchPhrase + " ]")
print('Matches found! Converting search results...')
print(end='\n')  # <-- command line spacer:)

# Do THE SEARCH based on user input, and create the clipList[]
os.chdir(rootDir)
for files in glob.glob("*.txt"):
    with open(files) as f:
        contents = f.read()
    if re.search(r'\b' + re.escape(searchPhrase) + r'\b', contents, re.IGNORECASE):
        sResult = files
        currentFile = sResult
        mainNameEnd = currentFile.find('.')
        newFileName = currentFile[:mainNameEnd] + '_000000.dng.tra'
        dirLoc = currentFile[:mainNameEnd]
        fullPathName = mediaDir + project.GetName() + '/' + "footage" + '/' + dirLoc + '/' + newFileName
        print("Converting Path Name: " + fullPathName)

        # Write the keyword(s) .txt file
        with open("/Volumes/audio/TRANSCRIBE/SearchPhrases/" + searchPhrase + ".txt", "a") as output:
            output.write(fullPathName)

# -------------------------------------------------------------------

# DaVinci Resolve Module

# Create Folders (bins) in DVR
# TODO: Still need to check if the bin folder 'TRANSCRIBE' exists in Resolve. If not, we need to create it.
mediaPool.SetCurrentFolder("TRANSCRIBE")
print(end='\n')  # <-- command line spacer:)
print("Conversion finished!  Active Bin --> " + currentFolder.GetName())
print('exporting to Davinci Resolve, please wait...')
transcribe = mediaPool.GetCurrentFolder()
mediaPool.AddSubFolder(transcribe, searchPhrase)

# Read the clipList .txt file
with open("/Volumes/audio/TRANSCRIBE/SearchPhrases/" + searchPhrase + ".txt", "r") as f:
    clipListRead = f.readlines()

    # Import clips from Search Result
    # We read the search results, and split into a list.
    for oneClip in clipListRead:
        jens = oneClip.split(".tra")
        clips = resolve.GetMediaStorage().AddItemsToMediaPool(jens)  # <-- We plugin clip list here
        print(end='\n')  # <-- command line spacer:)
        foundM = 1 - len(jens)
        print('Done! I found --' + (str(foundM)) + ' matches---')
        print("Timeline Clips: " + str(jens))

# Create DaVinci Resolve Timeline
# We replace --PHRASE SEARCH INPUT VALUE-- with the actual user search input.
timelineName = searchPhrase
timeline = mediaPool.CreateEmptyTimeline(timelineName)
if not timeline:
    print(end='\n')  # <-- command line spacer:)
    print("No need to create the timeline '" + timelineName + "'. Already exists! ")
    print(end='\n')  # <-- command line spacer:)
    print("SUCCESS: All dialog containing '..." + timelineName + "' imported to DVR!")
    print(end='\n')  # <-- command line spacer:)
    sys.exit()

# sort by name
clipNames = []
for clipIdx in clips:
    clipNames.append(clips[clipIdx].GetClipProperty("File Name")["File Name"])
indexes = sorted(range(len(clipNames)), key=lambda x: clipNames[x])

for clipIdx in range(len(indexes)):
    mediaPool.AppendToTimeline(clips[indexes[clipIdx] + 1])

# save project
projectManager.SaveProject()

# success message & quit
print("All dialog containing  [ " + searchPhrase + " ]  successfully imported to DVR. Timeline created!")

# Separators for clean commandLine
print(end='\n')  # <-- command line spacer:)
print(end='\n')  # <-- command line spacer:)
print(end='\n')  # <-- command line spacer:)
print(end='\n')  # <-- command line spacer:)

# END OF SCRIPT


PRINT:
Code: Select all
/Users/jens/PycharmProjects/transcribe/venv/bin/python /Users/jens/PycharmProjects/transcribe/transcribe_REV02.py

Script: python_get_resolve is present
Script: DaVinciResolveScript is present

System is ready!
* * *

Enter keywords or phrase: pull over now

You searched for:  [ pull over now ]
Matches found! Converting search results...

Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2002_C0127/FW_A01_2021-12-12_2002_C0127_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0129/FW_A01_2021-12-12_2003_C0129_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0130/FW_A01_2021-12-12_2003_C0130_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0131/FW_A01_2021-12-12_2003_C0131_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2005_C0132/FW_A01_2021-12-12_2005_C0132_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2005_C0134/FW_A01_2021-12-12_2005_C0134_000000.dng.tra
Converting Path Name: /Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2006_C0135/FW_A01_2021-12-12_2006_C0135_000000.dng.tra

Conversion finished!  Active Bin --> TRANSCRIBE
exporting to Davinci Resolve, please wait...

Done! I found ---7 matches---
Timeline Clips: ['/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2002_C0127/FW_A01_2021-12-12_2002_C0127_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0129/FW_A01_2021-12-12_2003_C0129_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0130/FW_A01_2021-12-12_2003_C0130_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2003_C0131/FW_A01_2021-12-12_2003_C0131_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2005_C0132/FW_A01_2021-12-12_2005_C0132_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2005_C0134/FW_A01_2021-12-12_2005_C0134_000000.dng', '/Volumes/RAID/DATA/MEDIA/TWO_CHAIRS/footage/FW_A01_2021-12-12_2006_C0135/FW_A01_2021-12-12_2006_C0135_000000.dng', '']


SUCCESS: All dialog containing  [ pull over now ]  successfully imported to DVR. Timeline created!"


Process finished with exit code 0

Return to DaVinci Resolve

Who is online

Users browsing this forum: dirk-pel, qsipher and 250 guests