-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeliner.py
129 lines (107 loc) · 4.08 KB
/
timeliner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import platform, os, sys
# Check arguments
if len(sys.argv) < 2:
print("Please provide a directory as an argument.")
print("Example: python timeliner.py /Users/john/Movies/MyMovie")
quit()
# Check if directory exists
if not os.path.isdir(sys.argv[1]):
print("ERROR: The directory you provided doesn't exist.")
quit()
# Check version
ver = platform.python_version_tuple()
if ver != ("2", "7", "18"):
print(
"ERROR: Timeliner should be ran using Python 2.7.18. You are using Python "
+ ".".join(ver)
+ "."
)
print("Install pyenv and run:")
print("> pyenv install 2.7.18")
print("> pyenv shell 2.7.18")
quit()
##############################################################################
# Project Settings
directory = sys.argv[1] # This is the directory containing the project folder
name = directory.split("/")[-1] # This is the name of the project
settings = {
"timelineFrameRate": 24,
"timelineResolutionWidth": 1920, # 1024
"timelineResolutionHeight": 1080, # 768
}
addBlanks = True # Should Timeliner add a transition media between each clip ?
blankFilename = "Black 5s.mp4" # This file should be in the Resources folder
##############################################################################
try:
import DaVinciResolveScript as dvr_script
except ImportError:
print("Please set the environment variables.")
print("Run the following commands in the terminal:")
if platform.system() == "Windows":
print(
"""
set RESOLVE_SCRIPT_API="%PROGRAMDATA%\\Blackmagic Design\\DaVinci Resolve\\Support\\Developer\\Scripting\\"
set RESOLVE_SCRIPT_LIB="C:\\Program Files\\Blackmagic Design\\DaVinci Resolve\\fusionscript.dll"
set PYTHONPATH="%PYTHONPATH%;%RESOLVE_SCRIPT_API%\\Modules\\"
"""
)
elif platform.system() == "Linux":
print(
"""
RESOLVE_SCRIPT_API="/opt/resolve/Developer/Scripting/"
RESOLVE_SCRIPT_LIB="/opt/resolve/libs/Fusion/fusionscript.so"
PYTHONPATH="$PYTHONPATH:$RESOLVE_SCRIPT_API/Modules/\""""
)
else:
print(
"""
export RESOLVE_SCRIPT_API="/Library/Application Support/Blackmagic Design/DaVinci Resolve/Developer/Scripting/"
export RESOLVE_SCRIPT_LIB="/Applications/DaVinci Resolve/DaVinci Resolve.app/Contents/Libraries/Fusion/fusionscript.so"
export PYTHONPATH="$PYTHONPATH:$RESOLVE_SCRIPT_API/Modules/\""""
)
quit()
# Boilerplate
resolve = dvr_script.scriptapp("Resolve")
try:
fusion = resolve.Fusion()
except:
print("[ERROR] DaVinci Resolve isn't running.")
print("Please launch DaVinci Resolve, open a new project and try again.")
quit()
projectManager = resolve.GetProjectManager()
# Create project
projectManager.CreateProject(name + " (Timeliner)")
project = projectManager.GetCurrentProject()
for setting in settings:
project.SetSetting(setting, settings[setting])
# Import Media
mediaPool = project.GetMediaPool()
mediaStorage = resolve.GetMediaStorage()
# Create Timeline
tl = mediaPool.CreateEmptyTimeline(name + " Timeline (Generated)")
timeline = project.GetTimelineByIndex(1)
project.SetCurrentTimeline(timeline)
# Sort files by prefix number
filenames = os.listdir(directory)
filenames = [
f for f in filenames if f[0] != "."
] # ignore files starting with . (such as .DS_STORE, etc)
sortedFilenames = sorted(filenames, key=lambda x: int(x.split(" ")[0]))
filePaths = [os.path.join(directory, filename) for filename in sortedFilenames]
# Get blank file to add between clips
blankFilePath = os.path.join(os.getcwd(), "Resources", blankFilename)
# Add clips to timeline, with blanks in between
if addBlanks:
blankClip = mediaStorage.AddItemsToMediaPool(blankFilePath)
for file in filePaths:
print(file)
clip = mediaStorage.AddItemsToMediaPool(file)
mediaPool.AppendToTimeline(clip)
if addBlanks:
if 1 in blankClip:
blankClip[1].SetClipColor("Pink")
else:
print(
"WARNING: Delete all files from media pool before running script. Your changes probably won't be saved."
)
mediaPool.AppendToTimeline(blankClip)