Last active
May 31, 2024 05:01
-
-
Save basuke/908ed2b0f41d9d995f7955d3cebfb9ae to your computer and use it in GitHub Desktop.
A python script to create a DaVinci Resolve project with today's date and import media files into default timeline.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import datetime | |
from glob import glob | |
from argparse import ArgumentParser | |
# settings | |
projectNamePrefix = "vlog-" | |
presetName = "Default" # You have to define this preset by your self. <---- | |
mediaDirectories = [ | |
r"G:\Vlog\ZV1-64GB-1", # Change these directories to your locations. <---- | |
r"G:\Vlog\GoPro Black 7", | |
r"G:\Vlog\WebCam HD", | |
r"G:\Vlog\Captured Screen", | |
] | |
mediaExtensions = [ | |
"mp4", # in lowercase | |
"mov", | |
] | |
markerColor = "Yellow" | |
verbose = False | |
# customization point | |
def modifyProject(proj): | |
""" | |
This function is called with created project. | |
media: Project | |
""" | |
# first thing first. set preset of the project | |
proj.SetPreset(presetName) | |
def modifyMedia(media): | |
""" | |
This function is called with each media added to media pool. | |
media: MediaPoolItem | |
""" | |
# props = media.GetClipProperty() | |
# print(props) | |
pass | |
def modifyClip(clip): | |
""" | |
This function is called with each media added to timeline. | |
It is a good chance to set clip color, set LUT. | |
media: TimelineItem | |
""" | |
clip.SetLUT(1, os.path.join(RESOLVE_LUT_DIR, r"Sony SLog2 to Rec709.ilut")) | |
#define paths for Resolve components | |
RESOLVE_SUPPORT_DIR = os.path.join(*[ | |
os.environ["PROGRAMDATA"], | |
"Blackmagic Design", | |
"DaVinci Resolve", | |
"Support", | |
]) | |
RESOLVE_LUT_DIR = os.path.join(*[ | |
RESOLVE_SUPPORT_DIR, | |
"LUT", | |
]) | |
RESOLVE_SCRIPT_API = os.path.join(*[ | |
RESOLVE_SUPPORT_DIR, | |
"Developer", | |
"Scripting", | |
]) | |
RESOLVE_SCRIPT_LIB = os.path.join(*[ | |
os.environ["ProgramFiles"], | |
"Blackmagic Design", | |
"DaVinci Resolve", | |
"fusionscript.dll", | |
]) | |
# actions | |
def createProject(memo=None, dates=None): | |
today = datetime.date.today().isoformat() | |
name = projectNamePrefix + today | |
if memo: | |
name += '-%s' % memo | |
proj = projectManager.CreateProject(name) | |
if proj: | |
modifyProject(proj) | |
mediaPool = proj.GetMediaPool() | |
timeline = mediaPool.CreateEmptyTimeline(today) | |
# add media files by date | |
if dates is None: | |
dates = [today] | |
clipCount = 0 | |
for date in dates: | |
for path, mediaList in getMediasOfTheDate(date): | |
mediaList = mediaStorage.AddItemListToMediaPool(mediaList) | |
for media in mediaList: | |
modifyMedia(media) | |
pos = timeline.GetEndFrame() - timeline.GetStartFrame() | |
timeline.AddMarker(pos, markerColor, path, "", 1) | |
mediaPool.AppendToTimeline(mediaList) | |
clips = timeline.GetItemListInTrack('video', 1) | |
if clips: | |
clipStart = clipCount | |
clipCount += len(mediaList) | |
for clip in clips[clipStart:clipCount]: | |
modifyClip(clip) | |
return proj | |
# utilities | |
def isMediaFile(name): | |
_, ext = os.path.splitext(name) | |
return ext and ext[0] == '.' and ext[1:].lower() in mediaExtensions | |
def getMediaFiles(directory): | |
def visit(result, path, names): | |
for name in names: | |
if isMediaFile(name): | |
result.append(os.path.join(path, name)) | |
result = [] | |
os.path.walk(directory, visit, result) | |
return result | |
def getMediasOfTheDate(date): | |
pattern = date + "*" | |
for directory in mediaDirectories: | |
for path in glob(os.path.join(directory, pattern)): | |
if os.path.isdir(path): | |
files = getMediaFiles(path) | |
if files: | |
yield (path, files) | |
# define environment variables | |
os.environ["RESOLVE_SCRIPT_API"] = RESOLVE_SCRIPT_API | |
os.environ["RESOLVE_SCRIPT_LIB"] = RESOLVE_SCRIPT_LIB | |
# setup python path | |
sys.path.append(os.path.join(RESOLVE_SCRIPT_API, 'Modules')) | |
# now it's ready to import module | |
import DaVinciResolveScript as dvrs | |
# major objects | |
resolve = dvrs.scriptapp("Resolve") | |
if not resolve: | |
print("Please launch DaVinci Resolve first.") | |
sys.exit() | |
projectManager = resolve.GetProjectManager() | |
mediaStorage = resolve.GetMediaStorage() | |
fusion = resolve.Fusion() | |
# command line options | |
parser = ArgumentParser(description="Create DaVinci Resolve project and import media files") | |
parser.add_argument("-d", "--date", dest="dates", action="append", help="Default is today. Multiple date is okay.") | |
parser.add_argument("--verbose", action="store_const", const=True, default=False) | |
parser.add_argument("memo", nargs='?', default="") | |
args = parser.parse_args() | |
verbose = args.verbose | |
projectManager.CloseProject(projectManager.GetCurrentProject()) | |
createProject(memo=args.memo, dates=args.dates) | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very helpful. Thank you so much for sharing.