-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCompileMovieClips.py
executable file
·86 lines (71 loc) · 3.82 KB
/
CompileMovieClips.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
#================ CompileMovieClips.py ===================
# Load a series of movie clips and concatenate them into a
# block of continuous or intermittent presentations.
import bpy
import numpy as np
import mathutils as mu
import math
import socket
import os
import glob
from sys import platform
from random import shuffle
MovieDir = "/Volumes/Seagate Backup 1/Stimuli/Movies/MonkeyThieves/MacaqueClips/FacialDeformities/"
InputFormat = ".mp4"
#=========== Render settings
NoPermutations = 1 # How many blocks/ output movies to generate
DurPerClip = 1.5 # Individual clip duration (seconds)
InterClipInterval = 1.0 # Duration of blank interval between clips
BlockDuration = 20 # Duration of full concatenated block (seconds)
BackgroundRGB = (0.5, 0.5, 0.5) # Set background color (if not full screen)
AudioOn = 0 # Encode original audio?
AddPhotodiode = 0 # Add photodiode marker for stimulus onsets?
#=========== Check input files
fileList = [ ]
for file in os.listdir(MovieDir):
if file.endswith(InputFormat):
fileList.append(os.path.join(MovieDir , file))
NoClips = int(BlockDuration/(DurPerClip+InterClipInterval))
if (len(fileList) < NoClips):
print("ERROR: number of %s files in %s (%d) is less than the number required (%d)!\n" % (InputFormat, MovieDir, len(fileList), NoClips))
ClipOrder = [i for i in range(NoClips)]
shuffle(ClipOrder) # Pseudorandomly shuffle order of clips
#===========
S = bpy.context.scene
if not S.sequence_editor: # If sequence editor doesn't exist...
SE = S.sequence_editor_create() # Create sequence editor
else:
SE = bpy.context.scene.sequence_editor
#S.render.image_settings.file_format = 'H264' # Set render format
S.render.image_settings.file_format = 'FFMPEG' # Set render format
S.render.ffmpeg.audio_codec = 'MP3'
S.render.ffmpeg.format = 'MPEG4'
S.render.ffmpeg.codec = 'H264'
S.render.ffmpeg.constant_rate_factor = 'PERC_LOSSLESS'
#======= Set output parameters to match original video clips
S.render.resolution_x = 1920
S.render.resolution_y = 1080
S.render.resolution_percentage = 100
S.render.fps = 30
S.render.fps_base = 1.0
S.frame_start = 1
MovieChannel = 1 # Which video sequence editor channel to put movie clips on
TotalFrames = BlockDuration*S.render.fps
#=========== Load all files
for b in range(NoPermutations):
shuffle(ClipOrder) # Pseudo-randomly shuffle order of clips
CurrentFrame = 0
for c in range(NoClips):
ClipName = "Clip %d" % c
ClipFile = fileList[ClipOrder[c]]
CurrentFrame = CurrentFrame+(S.render.fps*(DurPerClip+InterClipInterval))
SE.sequences.new_movie(ClipName, ClipFile, b+2, CurrentFrame)
SE.sequences_all[ClipName].frame_final_duration = S.render.fps*DurPerClip
#======== Add background and foreground images
Bkg = SE.sequences.new_effect('Background', 'COLOR', 1, 1, TotalFrames) # Background
SE.sequences_all["Background"].color = (BackgroundRGB);
#Frg = SE.sequences.new_image('Foreground', DepthArrayIm, 3, 1) # Foreground
#SE.sequences_all["Foreground"].blend_type = 'ALPHA_OVER'
#SE.sequences_all["Foreground"].frame_final_end = len(FrameOrder)+1
#======== Render to movie file
#bpy.ops.render.render(animation = True) # Render animation